gpt4 book ai didi

c# - 将图像转换为位图会使背景变黑

转载 作者:太空狗 更新时间:2023-10-29 17:34:37 25 4
gpt4 key购买 nike

我需要将图像转换为位图。

最初 gif 以字节形式读入,然后转换为图像。

但是当我尝试将图像转换为位图时,我的图片框中显示的图形以前是白色的,但现在却变成了黑色背景。

代码如下:

    var image = (System.Drawing.Image)value;
// Winforms Image we want to get the WPF Image from...
var bitmap = new System.Windows.Media.Imaging.BitmapImage();
bitmap.BeginInit();
MemoryStream memoryStream = new MemoryStream();
// Save to a memory stream...
image.Save(memoryStream, ImageFormat.Bmp);
// Rewind the stream...
memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
bitmap.StreamSource = memoryStream;
bitmap.EndInit();
return bitmap;

谁能解释一下为什么背景变黑了,我该如何阻止它。

谢谢

最佳答案

不要另存为位图文件。文件格式不支持透明度,因此图像将在不透明的情况下保存。

您可以改用 PNG 文件格式。这将保持透明度。

如果你真的需要它使用位图文件格式,你必须先让它不透明。新建一个相同大小的位图,使用Graphics.FromImage方法得到一个图形对象绘制在图像上,使用Clear方法填充背景颜色,使用 DrawImage 方法在背景上绘制图像,然后保存该位图。

关于c# - 将图像转换为位图会使背景变黑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4067448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com