首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >C# -将WPF Image.source转换为System.Drawing.Bitmap

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

Stack Overflow用户
提问于 2011-04-17 05:15:59
回答 4查看 45.9K关注 0票数 19

我发现很多人将BitmapSource转换为Bitmap,但是ImageSource转换为Bitmap呢?我正在制作一个成像程序,我需要从Image元素中显示的图像中提取位图。有人知道怎么做吗?

编辑1:

这是用于将BitmapImage转换为Bitmap的函数。记住要在编译器首选项中设置“unsafe”选项。

代码语言:javascript
复制
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;
}

下一步是获取一个BitmapImage

代码语言:javascript
复制
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();

下一步是转换它:

代码语言:javascript
复制
Bitmap b = new Bitmap(BitmapSourceToBitmap(bi));
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-04-19 06:07:07

实际上,你不需要使用不安全的代码。有一个接受IntPtr的CopyPixels重载:

代码语言:javascript
复制
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);
    }
}
票数 24
EN

Stack Overflow用户

发布于 2013-09-24 22:01:52

这个例子对我很有效:

代码语言:javascript
复制
    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;
    }
票数 3
EN

Stack Overflow用户

发布于 2011-04-17 05:46:15

你的ImageSource不是BitmapSource吗?如果你从文件中加载图像,它们应该是。

回复您的评论:

听起来它们应该是BitmapSource,那么,BitmapSource是ImageSource的一个子类型。将ImageSource转换为BitmapSource,并关注其中一个博客帖子。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5689674

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档