【C#/WPF】图像数据格式转换时,透明度丢失的问题

简介: 原文:【C#/WPF】图像数据格式转换时,透明度丢失的问题 问题:工作中涉及到图像的数据类型转换,经常转着转着发现,到了哪一步图像的透明度丢失了! 例如,Bitmap转BitmapImage的经典代码如下: ...
原文: 【C#/WPF】图像数据格式转换时,透明度丢失的问题

问题:工作中涉及到图像的数据类型转换,经常转着转着发现,到了哪一步图像的透明度丢失了!


例如,Bitmap转BitmapImage的经典代码如下:

public static BitmapImage BitmapToBitmapImage(System.Drawing.Bitmap bitmap)
{
    using (MemoryStream stream = new MemoryStream())
    {
        bitmap.Save(stream, ImageFormat.Bmp);

        stream.Position = 0;
        BitmapImage result = new BitmapImage();
        result.BeginInit();
        // According to MSDN, "The default OnDemand cache option retains access to the stream until the image is needed."
        // Force the bitmap to load right now so we can dispose the stream.
        result.CacheOption = BitmapCacheOption.OnLoad;
        result.StreamSource = stream;
        result.EndInit();
        result.Freeze();

        return result;
    }
}

使用时发现,如果一张图片四周是透明的,那么转出来的BitmapImage图像四周透明部分会被自动填充为黑色的!解决办法在于修改Bitmap保存时选择的格式,把Bmp改为Png即可。

bitmap.Save(stream, ImageFormat.Png);

同样,类似的经验教训还有如下,在图片数据格式转换时,常常要注意是否保留有α通道透明度数据,
比如在ImageSource转为System.Drawing.Bitmap的方法:

public static System.Drawing.Bitmap ImageSourceToBitmap(ImageSource imageSource)
{
    BitmapSource m = (BitmapSource)imageSource;

    System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(m.PixelWidth, m.PixelHeight, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);

    System.Drawing.Imaging.BitmapData data = bmp.LockBits(
    new System.Drawing.Rectangle(System.Drawing.Point.Empty, bmp.Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);

    m.CopyPixels(Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride);
    bmp.UnlockBits(data);

    return bmp;
}

要非常细心,看清楚选择的格式

System.Drawing.Imaging.PixelFormat.Format32bppPArgb; // 带有α通道的
System.Drawing.Imaging.PixelFormat.Format32bppRgb;   // 不带α通道的
目录
相关文章
|
1月前
|
监控 API 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK获取每张图像的微秒时间和FrameID功能(C#)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK获取每张图像的微秒时间和FrameID功能(C#)
64 0
|
1月前
|
存储 传感器 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK修改图像像素格式Mono8或者Mono10(C#)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK修改图像像素格式Mono8或者Mono10(C#)
83 0
|
1月前
|
存储 数据处理 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK实现相机的高速图像保存(C#)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK实现相机的高速图像保存(C#)
48 0
|
1月前
|
监控 算法 开发工具
Baumer工业相机堡盟工业相机如何联合NEOAPI SDK和OpenCV实现获取图像并对图像进行边缘检测(C#)
Baumer工业相机堡盟工业相机如何联合NEOAPI SDK和OpenCV实现获取图像并对图像进行边缘检测(C#)
50 1
|
1月前
|
存储 监控 开发工具
Baumer工业相机堡盟工业相机如何联合NEOAPI SDK和OpenCV实现相机图像转换为AVI视频格式(C#)
Baumer工业相机堡盟工业相机如何联合NEOAPI SDK和OpenCV实现相机图像转换为AVI视频格式(C#)
38 0
|
1月前
|
数据采集 API 开发工具
Baumer工业相机堡盟工业相机如何通过BGAPI SDK实现Raw格式的图像保存(C#)
Baumer工业相机堡盟工业相机如何通过BGAPI SDK实现Raw格式的图像保存(C#)
43 0
|
1月前
|
存储 传感器 监控
Baumer工业相机堡盟工业相机如何通过BGAPISDK将相机图像高速保存到电脑内存(C#)
Baumer工业相机堡盟工业相机如何通过BGAPISDK将相机图像高速保存到电脑内存(C#)
66 0
|
1月前
|
存储 数据处理 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK实现相机图像转换由Mono10转换为Mono8(C#)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK实现相机图像转换由Mono10转换为Mono8(C#)
46 0
|
13天前
|
C#
WPF/C#:程序关闭的三种模式
WPF/C#:程序关闭的三种模式
14 3
|
19天前
|
并行计算 算法 C#
C# Mandelbrot和Julia分形图像生成程序更新到2010-9-14版 支持多线程计算 多核处理器
此文档是一个关于分形图像生成器的介绍,作者分享了个人开发的M-J算法集成及色彩创新,包括源代码和历史版本。作者欢迎有兴趣的读者留言交流,并提供了邮箱(delacroix_xu@sina.com)以分享资源。文中还展示了程序的发展历程,如增加了真彩色效果、圈选放大、历史记录等功能,并分享了几幅精美的分形图像。此外,还提到了程序的新特性,如导入ini文件批量输出图像和更新一批图片的功能。文档末尾附有多张程序生成的高分辨率分形图像示例。