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;
                 }
             }
         }

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值