Fre*_*mas 5 .net c# transparency image-processing gif
我正在尝试使用.net Bitmap类编辑和保存图像.一些像素是透明的,并且在某些情况下它们会转换为黑色.如果我像这样保存相同的图像:
image.Save("copy1.png", System.Drawing.Imaging.ImageFormat.Png);
image.Save("copy2.gif", System.Drawing.Imaging.ImageFormat.Gif);
image.Save("copy3.gif");
Run Code Online (Sandbox Code Playgroud)
(图像原本是gif)第一个和第三个是正确保留透明度,但中间一个将所有透明像素设置为黑色.我不确定我做错了什么,AFAIK最后两行应该是等价的.
这是我正在谈论的示例程序:
using System.Drawing;
using System.Net;
namespace TestGif
{
class Program
{
static void Main(string[] args)
{
Bitmap bitmap = new Bitmap(WebRequest.Create(
"http://rlis.com/images/column/ie_icon.gif")
.GetResponse()
.GetResponseStream());
int width = bitmap.Width;
int height = bitmap.Height;
Bitmap copy = new Bitmap(width, height);
var graphics = Graphics.FromImage(copy);
graphics.DrawImage(bitmap, new Point(0, 0));
copy.Save("copy1.png", System.Drawing.Imaging.ImageFormat.Png);
copy.Save("copy2.gif", System.Drawing.Imaging.ImageFormat.Gif);
copy.Save("copy3.gif");
}
}
}
Run Code Online (Sandbox Code Playgroud)
你的最后一行
copy.Save("copy3.gif");
Run Code Online (Sandbox Code Playgroud)
不保存为 gif 文件,而是保存为 png,因为扩展名不足以指定保存格式。要制作透明的 gif,请使用类似的东西
Color c = copy.GetPixel(0, 0);
copy.MakeTransparent(c);
Run Code Online (Sandbox Code Playgroud)
您的代码正在创建一个新的位图,可能会丢失原始的 gif 信息。
归档时间: |
|
查看次数: |
2423 次 |
最近记录: |