<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Generating a transparent GIF image using C#</title>
	<atom:link href="http://www.codedblog.com/2007/08/28/generating-a-transparent-gif-image-using-c/feed/" rel="self" type="application/rss+xml" />
	<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>
	<lastBuildDate>Sat, 02 Oct 2010 13:04:48 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
	<item>
		<title>By: coder</title>
		<link>http://www.codedblog.com/2007/08/28/generating-a-transparent-gif-image-using-c/comment-page-1/#comment-16246</link>
		<dc:creator>coder</dc:creator>
		<pubDate>Fri, 13 Aug 2010 23:12:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.codedblog.com/?p=5#comment-16246</guid>
		<description>Thanks again, the source code you posted is very helpful! :-)</description>
		<content:encoded><![CDATA[<p>Thanks again, the source code you posted is very helpful! <img src='http://www.codedblog.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: O_O</title>
		<link>http://www.codedblog.com/2007/08/28/generating-a-transparent-gif-image-using-c/comment-page-1/#comment-14257</link>
		<dc:creator>O_O</dc:creator>
		<pubDate>Tue, 01 Jun 2010 22:18:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.codedblog.com/?p=5#comment-14257</guid>
		<description>to add:
this algorithm recognizes only colors with the same rgb.</description>
		<content:encoded><![CDATA[<p>to add:<br />
this algorithm recognizes only colors with the same rgb.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: O_O</title>
		<link>http://www.codedblog.com/2007/08/28/generating-a-transparent-gif-image-using-c/comment-page-1/#comment-14256</link>
		<dc:creator>O_O</dc:creator>
		<pubDate>Tue, 01 Jun 2010 21:21:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.codedblog.com/?p=5#comment-14256</guid>
		<description>this code doesn&#039;t work. it doesn&#039;t pick a transparent color in any way that uses at least a bit inteligence, so it&#039;s quite useless... -.-</description>
		<content:encoded><![CDATA[<p>this code doesn&#8217;t work. it doesn&#8217;t pick a transparent color in any way that uses at least a bit inteligence, so it&#8217;s quite useless&#8230; .</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Reader</title>
		<link>http://www.codedblog.com/2007/08/28/generating-a-transparent-gif-image-using-c/comment-page-1/#comment-8011</link>
		<dc:creator>Reader</dc:creator>
		<pubDate>Tue, 11 Aug 2009 16:47:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.codedblog.com/?p=5#comment-8011</guid>
		<description>Complete code is useless, and only usefull for those whom have a fix requirements.</description>
		<content:encoded><![CDATA[<p>Complete code is useless, and only usefull for those whom have a fix requirements.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Salman</title>
		<link>http://www.codedblog.com/2007/08/28/generating-a-transparent-gif-image-using-c/comment-page-1/#comment-3675</link>
		<dc:creator>Salman</dc:creator>
		<pubDate>Fri, 13 Feb 2009 05:14:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.codedblog.com/?p=5#comment-3675</guid>
		<description>I have had success in creating transparent GIFs in ASP.NET. In my code sample I added a hack for forcing GIF transparency and work around the .NET limitation of a pre-defined non-transparent palette. Have a look at this article:

http://911-need-code-help.blogspot.com/2009/02/create-text-images-on-fly-with-aspnet.html</description>
		<content:encoded><![CDATA[<p>I have had success in creating transparent GIFs in ASP.NET. In my code sample I added a hack for forcing GIF transparency and work around the .NET limitation of a pre-defined non-transparent palette. Have a look at this article:<br />
	<a href="http://911-need-code-help.blogspot.com/2009/02/create-text-images-on-fly-with-aspnet.html">http://911-need-code-help.blogspot.com/2009/02/create-text-images-on-fly-with-aspnet.html</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave</title>
		<link>http://www.codedblog.com/2007/08/28/generating-a-transparent-gif-image-using-c/comment-page-1/#comment-3101</link>
		<dc:creator>Dave</dc:creator>
		<pubDate>Fri, 24 Oct 2008 16:05:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.codedblog.com/?p=5#comment-3101</guid>
		<description>Thanks for this!  Just what I was looking for.  Nice job to whomever created it.  

A slight change is required if a color exists twice within a gif color map.  I changed the declaration of the transparent index to :

byte? transparentIdx = null;

Then altered the for loop that searched for the index:

            for (; i != 0; i--)
            {
                fin.Read(buf, 0, 3);
                if ((buf[0] == R) &amp;&amp; (buf[1] == G) &amp;&amp; (buf[2] == B))
                {
                    if (transparentIdx == null)
                        transparentIdx = (byte)(256 - i);
                }
                fout.Write(buf, 0, 3);
            }

            if (transparentIdx == null)
                transparentIdx = 0;

Finally, I changed the assignment at the bottom to convert the nullable index to a standard byte.

                        if (count == 4)
                        {
                            buf[0] &#124;= 0x01;
                            buf[3] = (byte)transparentIdx;
                        }

Again, thanks!</description>
		<content:encoded><![CDATA[<p>Thanks for this!  Just what I was looking for.  Nice job to whomever created it.<br />
	A slight change is required if a color exists twice within a gif color map.  I changed the declaration of the transparent index to :<br />
	byte? transparentIdx = null;<br />
	Then altered the for loop that searched for the index:<br />
            for (; i != 0; i&#8212;)<br />
            {<br />
                fin.Read(buf, 0, 3);<br />
                if ((buf[0] == R) &amp;&amp; (buf[1] == G) &amp;&amp; (buf[2] == B))<br />
                {<br />
                    if (transparentIdx == null)<br />
                        transparentIdx = (byte)(256 &#8211; i);<br />
                }<br />
                fout.Write(buf, 0, 3);<br />
            }<br />
            if (transparentIdx == null)<br />
                transparentIdx = 0;<br />
	Finally, I changed the assignment at the bottom to convert the nullable index to a standard byte.<br />
                        if (count == 4)<br />
                        {<br />
                            buf[0] |= 0&#215;01;<br />
                            buf[3] = (byte)transparentIdx;<br />
                        }<br />
	Again, thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: janos</title>
		<link>http://www.codedblog.com/2007/08/28/generating-a-transparent-gif-image-using-c/comment-page-1/#comment-3094</link>
		<dc:creator>janos</dc:creator>
		<pubDate>Thu, 23 Oct 2008 16:14:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.codedblog.com/?p=5#comment-3094</guid>
		<description>i think the orginial author of the code can be found here:

http://www.ben-rush.net/blog/PermaLink.aspx?guid=103ed74d-c808-47ba-b82d-6e9367714b3e&amp;dotnet=consultant</description>
		<content:encoded><![CDATA[<p>i think the orginial author of the code can be found here:<br />
	<a href="http://www.ben-rush.net/blog/PermaLink.aspx?guid=103ed74d-c808-47ba-b82d-6e9367714b3e&#038;dotnet=consultant">http://www.ben-rush.net/blog/PermaLink.aspx?guid=103ed74d-c808-47ba-b82d-6e9367714b3e&#038;dotnet=consultant</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: victor</title>
		<link>http://www.codedblog.com/2007/08/28/generating-a-transparent-gif-image-using-c/comment-page-1/#comment-2439</link>
		<dc:creator>victor</dc:creator>
		<pubDate>Thu, 03 Jul 2008 17:28:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.codedblog.com/?p=5#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] &amp; 0x80) &gt; 0)
	{
		i = 1 &lt; ((buf[10] &amp; 7) + 1) == 256 ? 256 : 0;
	}
	for (; i != 0; i--)
	{
		fin.Read(buf, 0, 3);
		if ((buf[0] == R) &amp;&amp; (buf[1] == G) &amp;&amp; (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 &gt; 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] &amp; 0&#215;80) &gt; 0)<br />
{<br />
i = 1 &lt; ((buf[10] &amp; 7) + 1) == 256 ? 256 : 0;<br />
}<br />
for (; i != 0; i&#8212;)<br />
{<br />
fin.Read(buf, 0, 3);<br />
if ((buf[0] == R) &amp;&amp; (buf[1] == G) &amp;&amp; (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 &gt; 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-page-1/#comment-2403</link>
		<dc:creator>janeks</dc:creator>
		<pubDate>Thu, 12 Jun 2008 21:38:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.codedblog.com/?p=5#comment-2403</guid>
		<description>I&#039;m wondering wouldn&#039;t be easer to convert GIF to PNG, then use .NET function foradding transparency, then convert PNG back to the GIF?

Or it&#039;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?<br />
	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-page-1/#comment-2386</link>
		<dc:creator>Joe</dc:creator>
		<pubDate>Sun, 25 May 2008 20:54:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.codedblog.com/?p=5#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>
</channel>
</rss>

