将BitmapImage转换为Bitmap,反之亦然(Converting BitmapImage to Bitmap and vice versa)

系统教程 行业动态 更新时间:2024-06-14 16:59:17
将BitmapImage转换为Bitmap,反之亦然(Converting BitmapImage to Bitmap and vice versa)

我在C#中有BitmapImage。 我需要做图像操作。 例如灰度调整,在图像上添加文本等

我在Stackoverflow中找到了接受Bitmap并返回Bitmap的灰度调用功能。

所以我需要将BitmapImage转换为Bitmap,然后进行操作和转换。

我该怎么做? 这是最好的方法吗?

I have BitmapImage in C#. I need to do operations on image. For example grayscaling, adding text on image, etc.

I have found function in stackoverflow for grayscaling which accepts Bitmap and returns Bitmap.

So I need to convert BitmapImage to Bitmap, do operation and convert back.

How can I do this? Is this best way?

最满意答案

没有必要使用外国图书馆。

将BitmapImage转换为位图:

private Bitmap BitmapImage2Bitmap(BitmapImage bitmapImage)
{
    // BitmapImage bitmapImage = new BitmapImage(new Uri("../Images/test.png", UriKind.Relative));

    using(MemoryStream outStream = new MemoryStream())
    {
        BitmapEncoder enc = new BmpBitmapEncoder();
        enc.Frames.Add(BitmapFrame.Create(bitmapImage));
        enc.Save(outStream);
        System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(outStream);

        return new Bitmap(bitmap);
    }
}
 

要将Bitmap转换为BitmapImage:

[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);

private BitmapImage Bitmap2BitmapImage(Bitmap bitmap)
{
    IntPtr hBitmap = bitmap.GetHbitmap();
    BitmapImage retval;

    try
    {
        retval = (BitmapImage)Imaging.CreateBitmapSourceFromHBitmap(
                     hBitmap,
                     IntPtr.Zero,
                     Int32Rect.Empty,
                     BitmapSizeOptions.FromEmptyOptions());
    }
    finally
    {
        DeleteObject(hBitmap);
    }

    return retval;
}

There is no need to use foreign libraries.

Convert a BitmapImage to Bitmap:

private Bitmap BitmapImage2Bitmap(BitmapImage bitmapImage)
{
    // BitmapImage bitmapImage = new BitmapImage(new Uri("../Images/test.png", UriKind.Relative));

    using(MemoryStream outStream = new MemoryStream())
    {
        BitmapEncoder enc = new BmpBitmapEncoder();
        enc.Frames.Add(BitmapFrame.Create(bitmapImage));
        enc.Save(outStream);
        System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(outStream);

        return new Bitmap(bitmap);
    }
}
 

To convert the Bitmap back to a BitmapImage:

[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);

private BitmapImage Bitmap2BitmapImage(Bitmap bitmap)
{
    IntPtr hBitmap = bitmap.GetHbitmap();
    BitmapImage retval;

    try
    {
        retval = (BitmapImage)Imaging.CreateBitmapSourceFromHBitmap(
                     hBitmap,
                     IntPtr.Zero,
                     Int32Rect.Empty,
                     BitmapSizeOptions.FromEmptyOptions());
    }
    finally
    {
        DeleteObject(hBitmap);
    }

    return retval;
}

                    
                     
          

更多推荐

本文发布于:2023-04-16 14:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/26c0ff568e1bea1a36672cbbc570d050.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   Bitmap   BitmapImage   versa   vice

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!