图像转换为位图变为黑色背景 [英] Converting Image to bitmap turns background black

查看:213
本文介绍了图像转换为位图变为黑色背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to convert an Image to a bitmap.

最初是一个GIF读取作为字节,然后转换成一个图像。

initially a gif was read in as bytes and then converted to an Image.

但是当我尝试将图像转换为位图,我在PictureBox中显示图形时有它使用的是白色黑色背景。

But when I try convert the image to a bit map, the graphic displaying in my picturebox has a black background when it used to be white.

下面是code:

    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;

有人能解释为什么背景是怎么回事黑,我怎么能阻止它这样做。

Can some one explain why the background is going black and how i can stop it doing this.

感谢

推荐答案

不要保存为位图文件。该文件格式不支持透明度,因此图像会保存缺乏透明度。

Don't save as a bitmap file. The file format doesn't support transparency, so the image will be saved without transparency.

您可以使用PNG文件格式来代替。这将preserve的透明度。

You can use the PNG file format instead. That will preserve the transparency.

如果你真的需要它使用位图文件格式,你必须让非透明的第一。创建具有相同大小的新位图,使用 Graphics.FromImage 方法得到一个图形对象绘制图像,使用清除的方法来与你想要的背景色填充,使用的DrawImage 方法绘制的背景上你的形象,然后保存该位图。

If you really need it to use the bitmap file format, you have to make it non-transparent first. Create a new bitmap with the same size, use the Graphics.FromImage method to get a graphics object to draw on the image, use the Clear method to fill it with the background color that you want, use the DrawImage method to draw your image on top of the background, and then save that bitmap.

这篇关于图像转换为位图变为黑色背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆