简体   繁体   English

C# - 将WPF Image.source转换为System.Drawing.Bitmap

[英]C# - Convert WPF Image.source to a System.Drawing.Bitmap

I've found loads of people converting a BitmapSource to a Bitmap , but what about ImageSource to Bitmap ? 我发现很多人将BitmapSource转换为Bitmap ,但是ImageSourceBitmap呢? I am making an imaging program and I need to extract bitmaps from the image displayed in the Image element. 我正在制作一个成像程序,我需要从Image元素中显示的图像中提取位图。 Does anyone know how to do this? 有谁知道如何做到这一点?

EDIT 1: 编辑1:

This is a function for converting the BitmapImage to a Bitmap . 这是将BitmapImage转换为Bitmap Remember to set the 'unsafe' option in the compiler preferences. 请记住在编译器首选项中设置“unsafe”选项。

public static System.Drawing.Bitmap BitmapSourceToBitmap(BitmapSource srs)
{
    System.Drawing.Bitmap btm = null;

    int width = srs.PixelWidth;

    int height = srs.PixelHeight;

    int stride = width * ((srs.Format.BitsPerPixel + 7) / 8);

    byte[] bits = new byte[height * stride];

    srs.CopyPixels(bits, stride, 0);

    unsafe
    {
        fixed (byte* pB = bits)
        {
            IntPtr ptr = new IntPtr(pB);

            btm = new System.Drawing.Bitmap(width, height, stride, System.Drawing.Imaging.PixelFormat.Format1bppIndexed, ptr);
        }
    }
    return btm;
}

Next is now to get a BitmapImage : 接下来是获取BitmapImage

RenderTargetBitmap targetBitmap = new RenderTargetBitmap(
    (int)inkCanvas1.ActualWidth,
    (int)inkCanvas1.ActualHeight,
    96d, 96d,
    PixelFormats.Default);

targetBitmap.Render(inkCanvas1);

MemoryStream mse = new MemoryStream();
System.Windows.Media.Imaging.BmpBitmapEncoder mem = new BmpBitmapEncoder();
mem.Frames.Add(BitmapFrame.Create(targetBitmap));
mem.Save(mse);

mse.Position = 0;
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = mse;
bi.EndInit();

Next is to convert it: 接下来是转换它:

Bitmap b = new Bitmap(BitmapSourceToBitmap(bi));

Actually you don't need to use unsafe code. 实际上你不需要使用不安全的代码。 There's an overload of CopyPixels that accepts an IntPtr: CopyPixels的重载接受IntPtr:

public static System.Drawing.Bitmap BitmapSourceToBitmap2(BitmapSource srs)
{
    int width = srs.PixelWidth;
    int height = srs.PixelHeight;
    int stride = width * ((srs.Format.BitsPerPixel + 7) / 8);
    IntPtr ptr = IntPtr.Zero;
    try
    {
        ptr = Marshal.AllocHGlobal(height * stride);
        srs.CopyPixels(new Int32Rect(0, 0, width, height), ptr, height * stride, stride);
        using (var btm = new System.Drawing.Bitmap(width, height, stride, System.Drawing.Imaging.PixelFormat.Format1bppIndexed, ptr))
        {
            // Clone the bitmap so that we can dispose it and
            // release the unmanaged memory at ptr
            return new System.Drawing.Bitmap(btm);
        }
    }
    finally
    {
        if (ptr != IntPtr.Zero)
            Marshal.FreeHGlobal(ptr);
    }
}

That example worked for me: 这个例子对我有用:

    public static Bitmap ConvertToBitmap(BitmapSource bitmapSource)
    {
        var width = bitmapSource.PixelWidth;
        var height = bitmapSource.PixelHeight;
        var stride = width * ((bitmapSource.Format.BitsPerPixel + 7) / 8);
        var memoryBlockPointer = Marshal.AllocHGlobal(height * stride);
        bitmapSource.CopyPixels(new Int32Rect(0, 0, width, height), memoryBlockPointer, height * stride, stride);
        var bitmap = new Bitmap(width, height, stride, PixelFormat.Format32bppPArgb, memoryBlockPointer);
        return bitmap;
    }

Are your ImageSource not a BitmapSource? 你的ImageSource不是BitmapSource吗? If your loading the images from files they should be. 如果您从文件加载图像应该是。

Reply to your comment: 回复你的评论:

Sounds like they should be BitmapSource then, BitmapSource is a subtype of ImageSource. 听起来他们应该是BitmapSource然后,BitmapSource是ImageSource的子类型。 Cast the ImageSource to BitmapSource and follow one of those blogposts. 将ImageSource转换为BitmapSource并按照其中一个博客文章进行操作。

You don't need a BitmapSourceToBitmap method at all. 您根本不需要BitmapSourceToBitmap方法。 Just do the following after creating your memory stream: 创建内存流后,请执行以下操作:

mem.Position = 0;  
Bitmap b = new Bitmap(mem);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM