<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.2.2" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: Generating a transparent GIF image using C#</title>
	<link>http://www.codedblog.com/2007/08/28/generating-a-transparent-gif-image-using-c/</link>
	<description>C#, ASP.NET, Google, Remoting, AJAX, Silverlight, Web Development</description>
	<pubDate>Wed, 09 Jul 2008 01:36:55 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.2</generator>

	<item>
		<title>By: victor</title>
		<link>http://www.codedblog.com/2007/08/28/generating-a-transparent-gif-image-using-c/#comment-2439</link>
		<author>victor</author>
		<pubDate>Thu, 03 Jul 2008 17:28:53 +0000</pubDate>
		<guid>http://www.codedblog.com/2007/08/28/generating-a-transparent-gif-image-using-c/#comment-2439</guid>
		<description>Here is the code w/o syntax errors.
//
 ///   
 /// Returns a transparent background GIF image from the specified Bitmap.  
 ///   
 /// The Bitmap to make transparent.  
 /// The Color to make transparent.  
 /// New Bitmap containing a transparent background gif.  
public Bitmap MakeTransparentGif(Bitmap bitmap, Color color)
{
	byte R = color.R;
	byte G = color.G;
	byte B = color.B;
	MemoryStream fin = new MemoryStream();
	bitmap.Save(fin, System.Drawing.Imaging.ImageFormat.Gif);
	MemoryStream fout = new MemoryStream((int)fin.Length);
	int count = 0;
	byte[] buf = new byte[256];
	byte transparentIdx = 0;
	fin.Seek(0, SeekOrigin.Begin);
	//header  
	count = fin.Read(buf, 0, 13);
	if ((buf[0] != 71) &#124;&#124; (buf[1] != 73) &#124;&#124; (buf[2] != 70)) return null; //GIF  
	fout.Write(buf, 0, 13);
	int i = 0;
	if ((buf[10] &#38; 0x80) &#62; 0)
	{
		i = 1 &#60; ((buf[10] &#38; 7) + 1) == 256 ? 256 : 0;
	}
	for (; i != 0; i--)
	{
		fin.Read(buf, 0, 3);
		if ((buf[0] == R) &#38;&#38; (buf[1] == G) &#38;&#38; (buf[2] == B))
		{
			transparentIdx = (byte)(256 - i);
		}
		fout.Write(buf, 0, 3);
	}
	bool gcePresent = false;
	while (true)
	{
		fin.Read(buf, 0, 1);
		fout.Write(buf, 0, 1);
		if (buf[0] != 0x21) break;
		fin.Read(buf, 0, 1);
		fout.Write(buf, 0, 1);
		gcePresent = (buf[0] == 0xf9);
		while (true)
		{
			fin.Read(buf, 0, 1);
			fout.Write(buf, 0, 1);
			if (buf[0] == 0) break;
			count = buf[0];
			if (fin.Read(buf, 0, count) != count) return null;
			if (gcePresent)
			{
				if (count == 4)
				{
					buf[0] &#124;= 0x01;
					buf[3] = transparentIdx;
				}
			}
			fout.Write(buf, 0, count);
		}
	}
	while (count &#62; 0)
	{
		count = fin.Read(buf, 0, 1);
		fout.Write(buf, 0, 1);
	}
	fin.Close();
	fout.Flush();
	return new Bitmap(fout);
}  
//</description>
		<content:encoded><![CDATA[<p>Here is the code w/o syntax errors.<br />
//<br />
 ///<br />
 /// Returns a transparent background GIF image from the specified Bitmap.<br />
 ///<br />
 /// The Bitmap to make transparent.<br />
 /// The Color to make transparent.<br />
 /// New Bitmap containing a transparent background gif.  <br />
public Bitmap MakeTransparentGif(Bitmap bitmap, Color color)<br />
{<br />
byte R = color.R;<br />
byte G = color.G;<br />
byte B = color.B;<br />
MemoryStream fin = new MemoryStream();<br />
bitmap.Save(fin, System.Drawing.Imaging.ImageFormat.Gif);<br />
MemoryStream fout = new MemoryStream((int)fin.Length);<br />
int count = 0;<br />
byte[] buf = new byte[256];<br />
byte transparentIdx = 0;<br />
fin.Seek(0, SeekOrigin.Begin);<br />
//header  <br />
count = fin.Read(buf, 0, 13);<br />
if ((buf[0] != 71) || (buf[1] != 73) || (buf[2] != 70)) return null; //GIF  <br />
fout.Write(buf, 0, 13);<br />
int i = 0;<br />
if ((buf[10] &#038; 0&#215;80) > 0)<br />
{<br />
i = 1 < ((buf[10] &#038; 7) + 1) == 256 ? 256 : 0;<br />
}<br />
for (; i != 0; i&#8212;)<br />
{<br />
fin.Read(buf, 0, 3);<br />
if ((buf[0] == R) &#038;& (buf[1] == G) &#038;& (buf[2] == B))<br />
{<br />
transparentIdx = (byte)(256 &#8211; i);<br />
}<br />
fout.Write(buf, 0, 3);<br />
}<br />
bool gcePresent = false;<br />
while (true)<br />
{<br />
fin.Read(buf, 0, 1);<br />
fout.Write(buf, 0, 1);<br />
if (buf[0] != 0&#215;21) break;<br />
fin.Read(buf, 0, 1);<br />
fout.Write(buf, 0, 1);<br />
gcePresent = (buf[0] == 0xf9);<br />
while (true)<br />
{<br />
fin.Read(buf, 0, 1);<br />
fout.Write(buf, 0, 1);<br />
if (buf[0] == 0) break;<br />
count = buf[0];<br />
if (fin.Read(buf, 0, count) != count) return null;<br />
if (gcePresent)<br />
{<br />
if (count == 4)<br />
{<br />
buf[0] |= 0&#215;01;<br />
buf[3] = transparentIdx;<br />
}<br />
}<br />
fout.Write(buf, 0, count);<br />
}<br />
}<br />
while (count > 0)<br />
{<br />
count = fin.Read(buf, 0, 1);<br />
fout.Write(buf, 0, 1);<br />
}<br />
fin.Close();<br />
fout.Flush();<br />
return new Bitmap(fout);<br />
}  <br />
//</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: janeks</title>
		<link>http://www.codedblog.com/2007/08/28/generating-a-transparent-gif-image-using-c/#comment-2403</link>
		<author>janeks</author>
		<pubDate>Thu, 12 Jun 2008 21:38:40 +0000</pubDate>
		<guid>http://www.codedblog.com/2007/08/28/generating-a-transparent-gif-image-using-c/#comment-2403</guid>
		<description>I'm wondering wouldn't be easer to convert GIF to PNG, then use .NET function foradding transparency, then convert PNG back to the GIF?

Or it's not working?</description>
		<content:encoded><![CDATA[<p>I&#8217;m wondering wouldn&#8217;t be easer to convert GIF to PNG, then use .NET function foradding transparency, then convert PNG back to the GIF?</p>
<p>Or it&#8217;s not working?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joe</title>
		<link>http://www.codedblog.com/2007/08/28/generating-a-transparent-gif-image-using-c/#comment-2386</link>
		<author>Joe</author>
		<pubDate>Sun, 25 May 2008 20:54:20 +0000</pubDate>
		<guid>http://www.codedblog.com/2007/08/28/generating-a-transparent-gif-image-using-c/#comment-2386</guid>
		<description>Could someone please post the code without any syntax errors.
Thanks</description>
		<content:encoded><![CDATA[<p>Could someone please post the code without any syntax errors.<br />
Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kim</title>
		<link>http://www.codedblog.com/2007/08/28/generating-a-transparent-gif-image-using-c/#comment-2221</link>
		<author>Kim</author>
		<pubDate>Fri, 07 Mar 2008 10:56:17 +0000</pubDate>
		<guid>http://www.codedblog.com/2007/08/28/generating-a-transparent-gif-image-using-c/#comment-2221</guid>
		<description>Dear Andrei,

Thank you very much! Your article give me a very usedly help.
In order to resovle this issue,I had searched all forums,BBS,Tech. Blog in China almost,but can't get a best solution yet,so much as I had make a decision to give up it.

Thanks!

Best regards,
Kim</description>
		<content:encoded><![CDATA[<p>Dear Andrei,</p>
<p>Thank you very much! Your article give me a very usedly help.<br />
In order to resovle this issue,I had searched all forums,BBS,Tech. Blog in China almost,but can&#8217;t get a best solution yet,so much as I had make a decision to give up it.</p>
<p>Thanks!</p>
<p>Best regards,<br />
Kim</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Csiga</title>
		<link>http://www.codedblog.com/2007/08/28/generating-a-transparent-gif-image-using-c/#comment-2059</link>
		<author>Csiga</author>
		<pubDate>Wed, 06 Feb 2008 16:16:30 +0000</pubDate>
		<guid>http://www.codedblog.com/2007/08/28/generating-a-transparent-gif-image-using-c/#comment-2059</guid>
		<description>Dear Andrei,

This is excellent! You save me a lot of time, not to mention those hours what I spent searching for such a &#34;converter&#34;. I'm developing a web application, where I use a lot of transparent images, but I have to use the png format (because of the Graphics.FromImage function - isn't able to populate Graphics object from image with indexed format, like gif). Of course, our wonderful browser (let's call it &#34;ie6.0&#34;), doesn't support transparent png, only with alphaimageloader, but it isn't so fast. With this function, I can create gif-s for mozilla and for explorer too from the png files I create. This is the solution for me! So, you rock! :)

Thanks a lot!

Best regards,
Csiga</description>
		<content:encoded><![CDATA[<p>Dear Andrei,</p>
<p>This is excellent! You save me a lot of time, not to mention those hours what I spent searching for such a &quot;converter&quot;. I&#8217;m developing a web application, where I use a lot of transparent images, but I have to use the png format (because of the Graphics.FromImage function &#8211; isn&#8217;t able to populate Graphics object from image with indexed format, like gif). Of course, our wonderful browser (let&#8217;s call it &quot;ie6.0&quot;), doesn&#8217;t support transparent png, only with alphaimageloader, but it isn&#8217;t so fast. With this function, I can create gif-s for mozilla and for explorer too from the png files I create. This is the solution for me! So, you rock! <img src='http://www.codedblog.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Thanks a lot!</p>
<p>Best regards,<br />
Csiga</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vladimir</title>
		<link>http://www.codedblog.com/2007/08/28/generating-a-transparent-gif-image-using-c/#comment-142</link>
		<author>Vladimir</author>
		<pubDate>Thu, 18 Oct 2007 09:56:20 +0000</pubDate>
		<guid>http://www.codedblog.com/2007/08/28/generating-a-transparent-gif-image-using-c/#comment-142</guid>
		<description>Hi Andrei,

Great Posting thank you very much for sharing this info.
Although the code published has some syntaxes problems and a extra  almost at the beginning.
Referring to this topic, I think you are generating GIFs because you want to display transparent images on the a browser and IE6 is not a good player with PNG.
An alternative would be to let .NET generate a PNG8 that's well accepted by  IE4 onwards!
But I couldn't find a why to make the .NET framework do it, do you know if that's possible?
 
Best regards,

Vladimir</description>
		<content:encoded><![CDATA[<p>Hi Andrei,</p>
<p>Great Posting thank you very much for sharing this info.<br />
Although the code published has some syntaxes problems and a extra  almost at the beginning.<br />
Referring to this topic, I think you are generating GIFs because you want to display transparent images on the a browser and IE6 is not a good player with PNG.<br />
An alternative would be to let .NET generate a PNG8 that&#8217;s well accepted by  IE4 onwards!<br />
But I couldn&#8217;t find a why to make the .NET framework do it, do you know if that&#8217;s possible?</p>
<p>Best regards,</p>
<p>Vladimir</p>
]]></content:encoded>
	</item>
</channel>
</rss>
