有没有一种很好的方法来转换BitmapSource和Bitmap?

时间:2010-02-17 20:59:20

标签: c# .net wpf bitmap bitmapsource

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

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可以在Bitmaps上运行。

6 个答案:

答案 0 :(得分:61)

使用Bitmap.LockBits可以不使用不安全的代码,并将BitmapSource笔直的像素复制到Bitmap

Bitmap GetBitmap(BitmapSource source) {
  Bitmap bmp = new Bitmap(
    source.PixelWidth,
    source.PixelHeight,
    PixelFormat.Format32bppPArgb);
  BitmapData data = bmp.LockBits(
    new Rectangle(Point.Empty, bmp.Size),
    ImageLockMode.WriteOnly,
    PixelFormat.Format32bppPArgb);
  source.CopyPixels(
    Int32Rect.Empty,
    data.Scan0,
    data.Height * data.Stride,
    data.Stride);
  bmp.UnlockBits(data);
  return bmp;
}

答案 1 :(得分:29)

您可以使用以下两种方法:

public static BitmapSource ConvertBitmap(Bitmap source)
{
    return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                  source.GetHbitmap(),
                  IntPtr.Zero,
                  Int32Rect.Empty,
                  BitmapSizeOptions.FromEmptyOptions());
}

public static Bitmap BitmapFromSource(BitmapSource bitmapsource)
{
    Bitmap bitmap;
    using (var outStream = new MemoryStream())
    {
        BitmapEncoder enc = new BmpBitmapEncoder();
        enc.Frames.Add(BitmapFrame.Create(bitmapsource));
        enc.Save(outStream);
        bitmap = new Bitmap(outStream);
    }
    return bitmap;
}

它非常适合我。

答案 2 :(得分:1)

这是你想要的吗?

Bitmap bmp = System.Drawing.Image.FromHbitmap(pBits);

答案 3 :(得分:1)

这里是一个代码,用于为资源字典中的任何位图资源设置透明背景(而不是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;
                }
            }

        }

    }

答案 4 :(得分:1)

这比光更整洁,更快:

  return Imaging.CreateBitmapSourceFromHBitmap( bitmap.GetHbitmap(), IntPtr.Zero,
      Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions() );

答案 5 :(得分:0)

您可以在两个命名空间之间共享pixeldata。你不必转换。

使用SharedBitmapSource。 https://stackoverflow.com/a/32841840/690656

相关问题