据我所知,从 BitmapSource 转换为 Bitmap 的唯一方法是通过不安全的代码......像这样(来自 莱斯特斯 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.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;
}

其他提示

您可以只使用这两种方法:

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

它非常适合我。

这是您要找的吗?

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

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

        }

    }

这很简洁,而且比光还快:

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

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

使用共享位图源。 https://stackoverflow.com/a/32841840/690656

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top