bitmap 和 BitmapSource之间互转

1  From bitmap  to BitmapSource

         #region  System.Drawing.Bitmap To BitmapSource
         [DllImport("gdi32.dll")]
         private static extern bool DeleteObject(IntPtr hObject);

         public static BitmapSource CreateBitmapSourceFromBitmap(Bitmap bitmap)
         {
             if (bitmap == null)
                 throw new ArgumentNullException("bitmap");

             lock (bitmap)
             {
                 IntPtr hBitmap = bitmap.GetHbitmap();

                 try
                 {
                     return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                         hBitmap,
                         IntPtr.Zero,
                         Int32Rect.Empty,
                         BitmapSizeOptions.FromEmptyOptions());
                 }
                 finally
                 {
                     DeleteObject(hBitmap);
                 }
             }
         }
         #endregion
 2 From BitmapSource to Bitmap

     //BitmapSource to System.Drawing.Bitmap
	 private System.Drawing.Bitmap GetBitmap(System.Windows.Media.Imaging.BitmapSource bitmapSource)
         {
             //Stream stm = File.Open("Waterfall.jpg", FileMode.Open, FileAccess.Read))
             //// Since we're not specifying a System.Windows.Media.Imaging.BitmapCacheOption, the pixel format
             //// will be System.Windows.Media.PixelFormats.Pbgra32.
             //System.Windows.Media.Imaging.BitmapSource bitmapSource = System.Windows.Media.Imaging.BitmapFrame.Create(
             //    stm, 
             //    System.Windows.Media.Imaging.BitmapCreateOptions.None, 
             //    System.Windows.Media.Imaging.BitmapCacheOption.OnLoad);
             // Scale the image so that it will display similarly to the WPF Image.
             double newWidthRatio = CurrentPhoto.Width / (double)bitmapSource.PixelWidth;
             double newHeightRatio = ((CurrentPhoto.Width * bitmapSource.PixelHeight) / (double)bitmapSource.PixelWidth) / (double)bitmapSource.PixelHeight;

             System.Windows.Media.Imaging.BitmapSource transformedBitmapSource = new System.Windows.Media.Imaging.TransformedBitmap(
                 bitmapSource,
                 new System.Windows.Media.ScaleTransform(newWidthRatio, newHeightRatio));

             int width = transformedBitmapSource.PixelWidth;
             int height = transformedBitmapSource.PixelHeight;
             int stride = width * ((transformedBitmapSource.Format.BitsPerPixel + 7) / 8);

             byte[] bits = new byte[height * stride];

             transformedBitmapSource.CopyPixels(bits, stride, 0);
             unsafe
             {
                 fixed (byte* pBits = bits)
                 {
                     IntPtr ptr = new IntPtr(pBits);

                     System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(
                         width,
                         height,
                         stride,
                         System.Drawing.Imaging.PixelFormat.Format32bppPArgb,
                         ptr);

                     return bitmap;
                 }
             }
         }

注:以上函数,在项目中均验证可行。代码也是从网上搜索所得,在此整理记录下。



你可能感兴趣的:(C#)