简体   繁体   English

为什么WebImage.Resize剥离出PNG透明度

[英]Why does WebImage.Resize strip out PNG transparency

Using WebImage from the MVC3/WebMatrix release. 使用MVC3 / WebMatrix版本中的WebImage Loading the following image from a file path: 从文件路径加载以下图像:

示例图像 - or - - 要么 - 在此输入图像描述

Running the following code against it: 针对它运行以下代码:

return new WebImage(path)
       .Resize(120, 265)
       .GetBytes("Png");

Results in an image with the transparency stripped out and black used in place: 在透明度被剥离并且使用黑色的图像中的结果:

涂黑透明度

The same happens with RotateRight(), RotateLeft() and FlipHorizantal() However if I don't call the .Resize() method: RotateRight(),RotateLeft()和FlipHorizantal()也是如此。但是,如果我不调用.Resize()方法:

return new WebImage(path)
        .GetBytes("Png");

The image is returned without an issue. 图像返回没有问题。

Is your image 8-bit (indexed color)? 你的图像是8位(索引颜色)? When 8-bit images are resized, they are often converted from indexed color to RGB in order to make the result look smoother. 调整8位图像的大小时,它们通常会从索引颜色转换为RGB,以使结果看起来更平滑。 Resizing images without changing the color table rarely ends well. 在不更改颜色表的情况下调整图像大小很少结束。

If the image is already RGB (24-bit or more), it's a little more complex: What should the resizer do for the edges of an image, on the border between "transparent" and "not transparent"? 如果图像已经是RGB(24位或更多),那就有点复杂了:在“透明”和“不透明”之间的边界上,缩放器应该对图像的边缘做什么? Many resizing algorithms will do something resembling "averaging the two colors" at a border (again, to make the image look smoother), but most have no concept of transparency, let alone PNG's ability to have multiple levels of translucency. 许多调整大小算法将在边界处执行类似于“平均两种颜色”的操作(同样,为了使图像看起来更平滑),但大多数没有透明度的概念,更不用说PNG具有多层次半透明的能力。

So, the short answer is "it's probably a bug" -- not a full implementation of PNG's features. 所以,简短的回答是“它可能是一个错误” - 而不是PNG功能的完整实现。

The solution may not exist, but I'd try: 解决方案可能不存在,但我会尝试:

  1. Stay in 8-bit color if that is an option, or convert ahead of time to 32-bit. 如果这是一个选项,请保持8位颜色,或提前转换为32位。
  2. Get the color, or color index, of the transparent color. 获取透明色的颜色或颜色索引。 After resizing, force that value to be transparent. 调整大小后,强制该值透明。 Note that due to the aforementioned "smoothing", the edges between transparent and non-transparent may not be that color, and will probably look like some blend of black and whatever color you actually want. 请注意,由于前面提到的“平滑”,透明和不透明之间的边缘可能不是那种颜色,并且可能看起来像黑色和您实际想要的任何颜色的混合。
  3. If nothing else, you may need to use an external library which gives you more control. 如果不出意外,您可能需要使用外部库来提供更多控制。

您是否尝试使用方法Write()而不是GetBytes()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM