有没有一种在BitmapSource和Bitmap之间转换的好方法?

据我所知,从BitmapSource转换为Bitmap的唯一方法是通过不安全的代码...像这样(来自Lesters WPF博客):


myBitmapSource.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;

  }

}

要做相反的事情:


System.Windows.Media.Imaging.BitmapSource bitmapSource =

  System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(

    bitmap.GetHbitmap(),

    IntPtr.Zero,

    Int32Rect.Empty,

    System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

框架中有没有更简单的方法?它不在其中的原因是什么(如果没有)?我认为这是相当有用的。


我需要它的原因是因为我使用AForge在WPF应用程序中执行某些图像操作。WPF希望显示BitmapSource / ImageSource,但AForge可以在Bitmap上使用。


暮色呼如
浏览 1536回答 3
3回答

湖上湖

这里的代码为资源词典中的任何位图资源(而不是Windows.Forms时代经常使用的Resources.resx)设置透明背景。我在InitializeComponent()-methode之前调用此方法。上面的melvas在文章中提到了方法'ConvertBitmap(Bitmap source)'和BitmapFromSource(BitmapSource bitmapsource)。private void SetBitmapResourcesTransparent()    {        Image img;        BitmapSource bmpSource;        System.Drawing.Bitmap bmp;        foreach (ResourceDictionary resdict in Application.Current.Resources.MergedDictionaries)        {            foreach (DictionaryEntry dictEntry in resdict)            {                // search for bitmap resource                if ((img = dictEntry.Value as Image) is Image                     && (bmpSource = img.Source as BitmapSource) is BitmapSource                    && (bmp = BitmapFromSource(bmpSource)) != null)                {                    // make bitmap transparent and assign it back to ressource                    bmp.MakeTransparent(System.Drawing.Color.Magenta);                    bmpSource = ConvertBitmap(bmp);                    img.Source = bmpSource;                }            }        }    }
打开App,查看更多内容
随时随地看视频慕课网APP