图像转换为位图会使背景变黑
问题说明
我需要将图像转换为位图.
I need to convert an Image to a bitmap.
最初以字节形式读取 gif,然后将其转换为图像.
initially a gif was read in as bytes and then converted to an Image.
但是当我尝试将图像转换为位图时,显示在我的图片框中的图形在以前是白色时变成了黑色背景.
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.
代码如下:
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 文件格式.这将保持透明度.
You can use the PNG file format instead. That will preserve the transparency.
如果你真的需要它使用位图文件格式,你必须先让它不透明.新建一个相同大小的位图,使用Graphics.FromImage
方法获取一个图形对象绘制在图片上,使用Clear
方法填充背景颜色,使用 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.
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tangicbfi
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20 -
复制文件夹/文件而不修改属性?
it1352 07-15