为什么透明像素在我的图像中显示为黑色?
我正在将图像字节数组保存为缩略图。问题是我的图像中透明背景颜色是黑色。
下面是我的代码:
MemoryStream memoryStream = new MemoryStream(pbytImageByteArray);
System.Drawing.Image imgImageSource = System.Drawing.Image.FromStream(memoryStream);
double dblOrgnWidth = imgImageSource.Width;
double dblOrgnHeight = imgImageSource.Height;
double dblRatio = (dblOrgnWidth / dblOrgnHeight) * 100;
double dblScaledWidth = pintWidth;
double dblScaledHeight = 0;
dblScaledHeight = (dblScaledWidth / dblRatio) * 100;
System.Drawing.Bitmap bitmapImage = new System.Drawing.Bitmap(System.Convert.ToInt32(dblScaledWidth), System.Convert.ToInt32(dblScaledHeight));
bitmapImage.SetResolution(imgImageSource.HorizontalResolution, imgImageSource.VerticalResolution);
System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmapImage);
graphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
ImageAttributes imageAttributes = new ImageAttributes();
graphics.DrawImage(imgImageSource, new System.Drawing.Rectangle(0, 0, System.Convert.ToInt32(dblScaledWidth), System.Convert.ToInt32(dblScaledHeight)), 0, 0, System.Convert.ToInt32(dblOrgnWidth), System.Convert.ToInt32(dblOrgnHeight), System.Drawing.GraphicsUnit.Pixel);
MemoryStream outputMemoryStream = new MemoryStream();
bitmapImage.Save(outputMemoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
bitmapImage.GetThumbnailImage(System.Convert.ToInt32(dblScaledWidth), System.Convert.ToInt32(dblScaledHeight), null, IntPtr.Zero);
imgImageSource.Dispose();
bitmapImage.Dispose();
graphics.Dispose();
return outputMemoryStream.ToArray();
I am saving an image bytes array as a thumbnail. The problem is that the transparent background color is black in my image.
Below is my code:
MemoryStream memoryStream = new MemoryStream(pbytImageByteArray);
System.Drawing.Image imgImageSource = System.Drawing.Image.FromStream(memoryStream);
double dblOrgnWidth = imgImageSource.Width;
double dblOrgnHeight = imgImageSource.Height;
double dblRatio = (dblOrgnWidth / dblOrgnHeight) * 100;
double dblScaledWidth = pintWidth;
double dblScaledHeight = 0;
dblScaledHeight = (dblScaledWidth / dblRatio) * 100;
System.Drawing.Bitmap bitmapImage = new System.Drawing.Bitmap(System.Convert.ToInt32(dblScaledWidth), System.Convert.ToInt32(dblScaledHeight));
bitmapImage.SetResolution(imgImageSource.HorizontalResolution, imgImageSource.VerticalResolution);
System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmapImage);
graphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
ImageAttributes imageAttributes = new ImageAttributes();
graphics.DrawImage(imgImageSource, new System.Drawing.Rectangle(0, 0, System.Convert.ToInt32(dblScaledWidth), System.Convert.ToInt32(dblScaledHeight)), 0, 0, System.Convert.ToInt32(dblOrgnWidth), System.Convert.ToInt32(dblOrgnHeight), System.Drawing.GraphicsUnit.Pixel);
MemoryStream outputMemoryStream = new MemoryStream();
bitmapImage.Save(outputMemoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
bitmapImage.GetThumbnailImage(System.Convert.ToInt32(dblScaledWidth), System.Convert.ToInt32(dblScaledHeight), null, IntPtr.Zero);
imgImageSource.Dispose();
bitmapImage.Dispose();
graphics.Dispose();
return outputMemoryStream.ToArray();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JPEG 不支持透明度。另存为 PNG。
或者,如果您知道其背景颜色,则可以将透明像素设置为该颜色。如果您使用半透明像素,则必须将像素与该颜色混合。
这是一篇解释 alpha 混合的文章:
http://www.c -sharpcorner.com/UploadFile/mahesh/DrawTransparentImageUsingAB10102005010514AM/DrawTransparentImageUsingAB.aspx
如果您对此商业解决方案感兴趣(免责声明:我在 Atalasoft 工作),DotImage Photo 有一个类
FlattenAlphaCommand
,只需几行代码即可完成此操作。JPEG doesn't support transparency. Save as a PNG.
Alternatively, if you know the background color that this will be on, you could set the transparent pixels to that color. If you are using semi-transparent pixels, then you would have to blend the pixels with that color.
Here is an article that explains alpha blending:
http://www.c-sharpcorner.com/UploadFile/mahesh/DrawTransparentImageUsingAB10102005010514AM/DrawTransparentImageUsingAB.aspx
If you are interested in a commercial solution for that (Disclaimer: I work for Atalasoft), DotImage Photo has a class,
FlattenAlphaCommand
, that can do this in a couple of lines of code.