47

据我所知,从 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 在位图上工作。

4

6 回答 6

71

可以不使用不安全代码通过使用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;
}
于 2010-05-24T13:35:32.410 回答
40

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

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

它非常适合我。

于 2014-02-21T10:31:39.130 回答
1

这是你要找的吗?

Bitmap bmp = System.Drawing.Image.FromHbitmap(pBits);
于 2010-02-17T22:23:34.477 回答
1

这里有一个代码为资源字典中的任何位图资源设置透明背景(不是 Windows.Forms 时代经常使用的 Resources.resx)。我在 InitializeComponent() 之前调用此方法 - 方法。上面 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;
                }
            }

        }

    }
于 2014-11-18T11:35:37.340 回答
1

这比光还快:

  return Imaging.CreateBitmapSourceFromHBitmap( bitmap.GetHbitmap(), IntPtr.Zero,
      Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions() );
于 2015-06-10T12:12:04.527 回答
0

您可以在两个命名空间之间共享像素数据。你不必转换。

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

于 2016-06-23T13:12:07.990 回答