Reputation: 944
I am trying to edit and save an image using the .net Bitmap class. Some of the pixels are transparent, and they get converted to black under certain circumstances. If I save the same image like this:
image.Save("copy1.png", System.Drawing.Imaging.ImageFormat.Png);
image.Save("copy2.gif", System.Drawing.Imaging.ImageFormat.Gif);
image.Save("copy3.gif");
(Image being originally a gif) the first and third are correct retaining the transparency, but the middle one sets all the transparent pixels to black. I'm not sure what I am doing wrong, AFAIK the last two lines should be equivalent.
Here is a sample program of what I am talking about:
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");
}
}
}
Upvotes: 5
Views: 2508
Reputation: 5594
Your last line
copy.Save("copy3.gif");
does not save as gif file, but as png, since the extension is not sufficient to specify the saving format. To make a transparent gif, use something like
Color c = copy.GetPixel(0, 0);
copy.MakeTransparent(c);
Your code is creating a new bitmap, possibly losing the original gif informations.
Upvotes: 3