Answered by:
Drawing a GIF with System.Drawing

Question
-
User1942128046 posted
Hi everyone,
I'm using some asp.net pages to output an files of content type GIF. Including a path to this aspx in the 'src' attribute of an 'img' tag in the html markup, will display the gif on any page, including html. So I have a page called 'displayImage.html', and the markup includes this code:
<img src="generarLogo.aspx" alt="Logo" >
This works fine. My problem is the quality of the logo. It seems like it's displaying a gif with a reduced amount of colors, because I see some of the colors dotted, creating a bad quality look.
My C# code would be something like this:
Image originalImg = Image.FromFile(Server.MapPath("logo.gif"));
Bitmap image1 = new Bitmap(originalImg);this.Response.Clear();
this.Response.ContentType = "image/gif";
image1.Save(this.Response.OutputStream, ImageFormat.Gif);I have also tried adding a Palette with this method:
image1.Palette = GetColorPalette(256);
protected ColorPalette GetColorPalette(uint nColors)
{
// Assume monochrome image. (2 colors)
PixelFormat bitscolordepth = PixelFormat.Format1bppIndexed;
ColorPalette palette; // The Palette we are stealing
Bitmap bitmap; // The source of the stolen palette
// Determine number of colors.
if (nColors > 2)
bitscolordepth = PixelFormat.Format4bppIndexed;
if (nColors > 16)
bitscolordepth = PixelFormat.Format8bppIndexed;
// Make a new Bitmap object to get its Palette.
bitmap = new Bitmap(1, 1, bitscolordepth);
palette = bitmap.Palette;
bitmap.Dispose();
return palette;
}But this doesn't improve the quality of my GIF image. I got this little method from msdn documentation.
Monday, December 28, 2009 5:51 AM
Answers
-
User-1659704165 posted
Hi,
public void CreateGif()
{
//Variable declaration
StringCollection stringCollection = new StringCollection();
MemoryStream memoryStream;
BinaryWriter binaryWriter;
System.Drawing.Image image;
Byte[] buf1;
Byte[] buf2;
Byte[] buf3;
//Variable declaration
// stringCollection = a_StringCollection_containing_images;
stringCollection.Add(@"C:\Ok.gif");
// stringCollection.Add(@"C:\ok.gif");
Response.ContentType = "Image/gif";
memoryStream = new MemoryStream();
buf2 = new Byte[19];
buf3 = new Byte[8];
buf2[0] = 33; //extension introducer
buf2[1] = 255; //application extension
buf2[2] = 11; //size of block
buf2[3] = 78; //N
buf2[4] = 69; //E
buf2[5] = 84; //T
buf2[6] = 83; //S
buf2[7] = 67; //C
buf2[8] = 65; //A
buf2[9] = 80; //P
buf2[10] = 69; //E
buf2[11] = 50; //2
buf2[12] = 46; //.
buf2[13] = 48; //0
buf2[14] = 3; //Size of block
buf2[15] = 1; //
buf2[16] = 0; //
buf2[17] = 0; //
buf2[18] = 0; //Block terminator
buf3[0] = 33; //Extension introducer
buf3[1] = 249; //Graphic control extension
buf3[2] = 4; //Size of block
buf3[3] = 9; //Flags: reserved, disposal method, user input, transparent color
buf3[4] = 10; //Delay time low byte
buf3[5] = 3; //Delay time high byte
buf3[6] = 255; //Transparent color index
buf3[7] = 0; //Block terminator
binaryWriter = new BinaryWriter(Response.OutputStream);
for (int picCount = 0; picCount < stringCollection.Count; picCount++)
{
image = Bitmap.FromFile(stringCollection[picCount]);
image.Save(memoryStream, ImageFormat.Gif);
buf1 = memoryStream.ToArray();
if (picCount == 0)
{
//only write these the first time....
binaryWriter.Write(buf1, 0, 781); //Header & global color table
binaryWriter.Write(buf2, 0, 19); //Application extension
}
binaryWriter.Write(buf3, 0, 8); //Graphic extension
binaryWriter.Write(buf1, 789, buf1.Length - 790); //Image data
if (picCount == stringCollection.Count - 1)
{
//only write this one the last time....
binaryWriter.Write(";"); //Image terminator
}
memoryStream.SetLength(0);
}
binaryWriter.Close();
Response.End();
}
OR
http://bloggingabout.net/blogs/rick/archive/2005/05/10/3830.aspx
chk the above link- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, December 28, 2009 6:28 AM
All replies
-
User-1659704165 posted
Hi,
public void CreateGif()
{
//Variable declaration
StringCollection stringCollection = new StringCollection();
MemoryStream memoryStream;
BinaryWriter binaryWriter;
System.Drawing.Image image;
Byte[] buf1;
Byte[] buf2;
Byte[] buf3;
//Variable declaration
// stringCollection = a_StringCollection_containing_images;
stringCollection.Add(@"C:\Ok.gif");
// stringCollection.Add(@"C:\ok.gif");
Response.ContentType = "Image/gif";
memoryStream = new MemoryStream();
buf2 = new Byte[19];
buf3 = new Byte[8];
buf2[0] = 33; //extension introducer
buf2[1] = 255; //application extension
buf2[2] = 11; //size of block
buf2[3] = 78; //N
buf2[4] = 69; //E
buf2[5] = 84; //T
buf2[6] = 83; //S
buf2[7] = 67; //C
buf2[8] = 65; //A
buf2[9] = 80; //P
buf2[10] = 69; //E
buf2[11] = 50; //2
buf2[12] = 46; //.
buf2[13] = 48; //0
buf2[14] = 3; //Size of block
buf2[15] = 1; //
buf2[16] = 0; //
buf2[17] = 0; //
buf2[18] = 0; //Block terminator
buf3[0] = 33; //Extension introducer
buf3[1] = 249; //Graphic control extension
buf3[2] = 4; //Size of block
buf3[3] = 9; //Flags: reserved, disposal method, user input, transparent color
buf3[4] = 10; //Delay time low byte
buf3[5] = 3; //Delay time high byte
buf3[6] = 255; //Transparent color index
buf3[7] = 0; //Block terminator
binaryWriter = new BinaryWriter(Response.OutputStream);
for (int picCount = 0; picCount < stringCollection.Count; picCount++)
{
image = Bitmap.FromFile(stringCollection[picCount]);
image.Save(memoryStream, ImageFormat.Gif);
buf1 = memoryStream.ToArray();
if (picCount == 0)
{
//only write these the first time....
binaryWriter.Write(buf1, 0, 781); //Header & global color table
binaryWriter.Write(buf2, 0, 19); //Application extension
}
binaryWriter.Write(buf3, 0, 8); //Graphic extension
binaryWriter.Write(buf1, 789, buf1.Length - 790); //Image data
if (picCount == stringCollection.Count - 1)
{
//only write this one the last time....
binaryWriter.Write(";"); //Image terminator
}
memoryStream.SetLength(0);
}
binaryWriter.Close();
Response.End();
}
OR
http://bloggingabout.net/blogs/rick/archive/2005/05/10/3830.aspx
chk the above link- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, December 28, 2009 6:28 AM -
User1942128046 posted
Thanks qwe123kids, I will try this. Not as straight forward as I initially thought.. .
Tuesday, December 29, 2009 3:12 AM