寫給自己的wpf 總結,在wpf中,畫圖的時候,方法總結一二



       public void loaded_Image()
        {
            
            Console.WriteLine("ltbufffer的大小 " + ltBuffer.Count);
            if (ltBuffer.Count>640)
            {
                //System.Windows.MessageBox.Show("show");
                Bitmap b = new Bitmap(16, 40);
                MemoryStream m = new MemoryStream(); //緩衝區
                //鎖定位圖
                BitmapData bbb = b.LockBits(new System.Drawing.Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                IntPtr ptr = bbb.Scan0; //找到位圖首地址
                byte[] byteData = new byte[b.Width * b.Height * 3];//存放位圖
                System.Runtime.InteropServices.Marshal.Copy(ptr, byteData, 0, b.Width * b.Height * 3); //給RGB賦值           

                Console.WriteLine("get_strData 的大小:" + get_strData.Length + "----" + ltBuffer.Count);//錯誤異常的原因,ltbuffer的大小還沒有獲取到            
                for (int i = 16 * 40 - 1; i >= 0; i--)
                {
                   double dscal;
                    //加一個短路判斷
                    //在循環的時候,過濾掉T 替換掉t

                   if (ltBuffer[40 * 16 - 1 - i].ToString() == "T" || ltBuffer[40 * 16 - 1 - i].ToString() == "Z" || ltBuffer[40 * 16 - 1 - i].ToString() == "S"   )
                   {
                       continue;
                    

                   }/* double.TryParse(ltBuffer[639 - i], out num);*/
                   try
                   {
                     //  if (ltBuffer[16 * 40 - 1 - i].Contains("T")) ltBuffer[16 * 40 - 1 - i] = "84";

                       if (double.Parse(ltBuffer[16 * 40 - 1 - i]) > 145)
                       {
                           dscal = 1;
                       }
                       else
                       {
                           if (double.Parse(ltBuffer[16 * 40 - 1 - i]) < 16)
                           {
                               dscal = 0;
                           }
                           else
                           {
                               dscal = (double.Parse(ltBuffer[16 * 40 - 1 - i]) - 16) / 255.0;
                           }
                       }


                       if (dscal > 1)
                       {
                           dscal = 1;
                       }
                       byteData[i * 3 + 0] = (byte)(255 - 255 * dscal);//B
                       byteData[i * 3 + 1] = (byte)(255 - 255 * dscal);//G
                       byteData[i * 3 + 2] = (byte)(255 - 255 * dscal);//R
                   }
                   catch
                   {
                       System.Windows.MessageBox.Show("image_picture2 : "+ltBuffer[16 * 40 - 1 - i] + " "+(639-i));
                       Console.WriteLine();
                     
                   }
                }
                //  放回位圖
                System.Runtime.InteropServices.Marshal.Copy(byteData, 0, ptr, b.Width * b.Height * 3);
                //釋放位圖   
                b.UnlockBits(bbb);
                //保存位圖
                b.Save(m, ImageFormat.Bmp);              

                RotateTransform rotateTransform = new RotateTransform(180, 80, 200);//180度
    
                #region 11  可用  OK   方法一:  
                //MemoryStream ms = new MemoryStream();
                //b.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                //BitmapImage bitmapImage = new BitmapImage();
                //bitmapImage.BeginInit();
                //bitmapImage.StreamSource = new MemoryStream(ms.ToArray());
                //bitmapImage.EndInit();
                ////以下代碼,進行位圖的顯示操作 ,
                //System.Drawing.Image bba = System.Drawing.Image.FromStream(m);
                //image1.Source = bitmapImage;//將圖片顯示到Image控件上
                #endregion

                #region 22  可用  OK  方法二

                //System.Drawing.Image bbc = System.Drawing.Image.FromStream(m);
                //System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(bbc);
                //IntPtr hBitmap = bmp.GetHbitmap();
                //System.Windows.Media.ImageSource WpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                //this.Dispatcher.Invoke(new Action(delegate
                //{
                //    //這裏寫代碼   
                //    this.image1.Source = WpfBitmap;
                //}));
                #endregion


                 #region 44  可用 OK  方法三

                Bitmap bitmap = b;
                IntPtr ip = bitmap.GetHbitmap();//從GDI+ Bitmap創建GDI位圖對象

                System.Windows.Media.Imaging.BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty,
                System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
                this.Dispatcher.Invoke(new Action(delegate
                {
                    //這裏寫代碼   
                    image1.Source = bitmapSource;

                }));
                #endregion

                #region 33 可用  OK   也可以用
               
            //    System.Drawing.Image bmp = System.Drawing.Image.FromStream(m);
            //    System.IO.MemoryStream ms = new System.IO.MemoryStream();
            //    bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
            //    System.Windows.Media.Imaging.BitmapImage bi = new System.Windows.Media.Imaging.BitmapImage();
            //    bi.BeginInit();
            //    bi.StreamSource = new MemoryStream(ms.ToArray());
            //    bi.EndInit();
            ////    Visual.VisualYSnappingGuidelines = "true";
            //    this.Dispatcher.Invoke(new Action(delegate
            //    {
            //        //這裏寫代碼 
            //        image1.Source = bi;

            //    }));

                SetValue(RenderOptions.BitmapScalingModeProperty, BitmapScalingMode.HighQuality);
                image1.RenderTransform = rotateTransform;//圖片控件旋轉
                //image1.RenderSize = new System.Windows.Size(16,40);
             //   ms.Close();
                Console.WriteLine("+++++++++++++++++++++++++++++我是繪製圖像,我應該發生在獲取數據之後");
                #endregion
                Console.WriteLine("+++++++++++++++++++++++++++++over");

              

                #region  55  有點問題,stream 處參數無效化

                // image1.Source = BitmapToBitmapImage(AddHeader(StreamToBytes(m)));
                //  image1.Source = BitmapToBitmapImage(AddHeader(byteData));

                #endregion
                ltBuffer.Clear();
               change_loaded = true;
            }
            //else if (ltBuffer.Count > 640)
            //{
            //    Console.WriteLine("我被清理了");
            //    ltBuffer.Clear();
            //}
            #endregion

        }
        public Bitmap AddHeader(byte[] imageDataDetails)
        {
            Bitmap bitmap = null;
            int length = imageDataDetails.GetLength(0);
            using (MemoryStream stream = new MemoryStream(length + 14))//爲頭騰出14個長度的空間
            {
                byte[] buffer = new byte[13];
                buffer[0] = 0x42;//Bitmap 固定常數
                buffer[1] = 0x4d;//Bitmap 固定常數
                stream.Write(buffer, 0, 2);//先寫入頭的前兩個字節

                //把我們之前獲得的數據流的長度轉換成字節,
                //這個是用來告訴“頭”我們的實際圖像數據有多大
                byte[] bytes = BitConverter.GetBytes(length);
                stream.Write(bytes, 0, 4);//把這個長度寫入頭中去
                buffer[0] = 0;
                buffer[1] = 0;
                buffer[2] = 0;
                buffer[3] = 0;
                stream.Write(buffer, 0, 4);//在寫入4個字節長度的數據到頭中去
                int num2 = 0x36;//Bitmap 固定常數
                bytes = BitConverter.GetBytes(num2);
                stream.Write(bytes, 0, 4);//在寫入最後4個字節的長度
                stream.GetBuffer();
                stream.Write(imageDataDetails, 0, length);//把實際的圖像數據全部追加到頭的後面
                bitmap = new Bitmap(stream);//用內存流構造出一幅bitmap的圖片
                bitmap.RotateFlip(RotateFlipType.Rotate180FlipX);
                stream.Close();
                return bitmap;//最後就得到了我們想要的圖片了

            }

        }
        private BitmapImage BitmapToBitmapImage(System.Drawing.Bitmap bitmap)
        {
            BitmapImage bitmapImage = new BitmapImage();
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                bitmap.Save(ms, bitmap.RawFormat);
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = ms;
                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                bitmapImage.EndInit();
                bitmapImage.Freeze();
            }
            return bitmapImage;
        }
        public byte[] StreamToBytes(Stream stream)
        {
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);

            // 設置當前流的位置爲流的開始
            stream.Seek(0, SeekOrigin.Begin);
            return bytes;
        }

        //獲取到數據流的數據,將之轉換爲相應的圖片
        public static BitmapImage ByteArrayToBitmapImage(byte[] byteArray)
        {
            BitmapImage bmp = null;
            try
            {
                bmp = new BitmapImage();
                bmp.BeginInit();
                bmp.StreamSource = new MemoryStream(byteArray);
                bmp.EndInit();
            }
            catch
            {
                bmp = null;
            }
            return bmp;
        }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章