本帖最后由 cooltest2 于 2009-08-21 14:06:08 编辑

解决方案 »

  1.   

    Bitmap bt = Image.FromFile("c:\a.gif"); 
    Bitmap gifBt = new Bitmap(bt.Width,bt.Height); 
    Graphics gifGra = Graphics.FromImage(gifBt); 
    gifGra.Clear(ColorTranslator.FromHtml("#00FF00")); 
    gifGra.DrawImage(bt,new Rectangle(0,0,gifBt.Width,gifBt.Height),0,0,bt.Width,bt.Height,GraphicsUnit.Pixel); 
    string gifFile = Server.MapPath(Constant.LocalTmp + saveFileName + ".gif") ; 
    gifBt.Save(gifFile,System.Drawing.Imaging.ImageFormat.Gif); 
    gifBt.Dispose(); 把 “00FF00” 就透明  
    不过做完总会有一些锯齿。。 
      

  2.   

    遍历图片所有像素,检查是否有Alpha值为0的。
      

  3.   

    http://www.ben-rush.net/blog/PermaLink.aspx?guid=103ed74d-c808-47ba-b82d-6e9367714b3e&dotnet=consultant
      

  4.   

    I was helping someone out on the newsgroups who wanted to draw a GIF from his ASP.Net web page; one of the requirements was that it had a transparent background. As it turns out, this little exercise is a bit more complicated than it at first seems due to - what I think - is either a bug or an anomaly in the GDI+ framework. First - a bit of education. GIF is an indexed image format, meaning that it uses a color palette and individual pixels reference a color by indexing into the palette. One of the "colors" in the palette is a non-color or transparent; a transparent color is anything with an alpha value of 0 (ie both 0-255-255-255 and 0-128-128-128 are "invisible" where the first digit represents the value of the alpha octet).Interestingly, the .Net runtime's GDI+ Graphics.FromImage() method DOES NOT like indexed file formats, so if you want to draw onto a GIF using the GDI+ framework, it cannot be done via this method. It's screwy; don't ask me why it is this way. Well, this guy wanted to draw text onto a GIF with a transparent background, so....how to you go about drawing text onto a GIF with a transparent background? My first thought was modifying the palette so that all references to, say, black had an alpha transparency level of 0 - meaning invisible. What this would mean is that anything that referenced a black index into the color palette wouldn't be visible (100% alpha value). Another interesting "quirk" of the .Net runtime GDI+ framework, however, is that whenever you save the data to a stream (file, memory stream, output stream, etc) it will screw with the color palette. I didn't really try to understand what it was doing, but if you look at the color palette in a debugger window while using it, the save method just toys with it and mucks up your mods to it. So...the only option, lock down the data, and reference each individual pixel in the image, pointing it to the entry in the GIFs color palette for a transparent color instead of black. And it worked....here is the code (implemented as a C# library that you may use freely - please be mindful that I have not tested this but for just a little bit, there may be resource leaks, etc):public class TransparentGif
    {
     public TransparentGif()
     {
     }
        public static MemoryStream DrawTransparentGif(
            String text, Color textColor, Int32 width, Int32 height)
        { 
            Bitmap drawableBMP = new Bitmap(width, height);
            Graphics gdc = Graphics.FromImage(drawableBMP);
            try
            {
                gdc.FillRectangle(new SolidBrush(Color.Black), new Rectangle(0, 0, width, height));
                gdc.DrawString(text, new Font("Arial", 13), new SolidBrush(textColor), 0, 0);
                MemoryStream memStream = new MemoryStream();
                drawableBMP.Save(memStream, ImageFormat.Gif);            memStream.Seek(0, SeekOrigin.Begin);
                System.Drawing.Image gifed = System.Drawing.Image.FromStream(memStream);            ColorPalette cp = gifed.Palette;
                Int32 alpha = 0;
                for (Int32 c = 0; c < cp.Entries.Length; c++)
                {
                    if (cp.Entries[c].A == 0)
                    {
                        alpha = c;
                         break; 
                    }
                }            BitmapData data = ((Bitmap)gifed).LockBits(
        new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, gifed.PixelFormat);            Byte[] buffer = new Byte[width* height];
                Marshal.Copy(data.Scan0, buffer, 0, width* height);            for (Int32 c = 0; c < buffer.Length; c++)
                {
                    if (cp.Entries[buffer[c]].ToArgb() == Color.Black.ToArgb())
                    {
                        buffer[c] = (Byte)alpha;
                    }
                }            Marshal.Copy(buffer, 0, data.Scan0, buffer.Length);            ((Bitmap)gifed).UnlockBits(data);            MemoryStream outStream = new MemoryStream();
                gifed.Save(outStream, ImageFormat.Gif);
                outStream.Seek(0, SeekOrigin.Begin);            return outStream; 
            }
            finally
            {
                if (gdc != null)
                    gdc.Dispose(); 
            }
        }
    }If you comment out the actual pixel manipulation, what gets drawn to my web page is the following: 
    ...or a GIF with a black background. If you bring back in the actual pixel manipulation part of the above code, you get this: 
    Total alpha transparency of the GIF....
      

  5.   

    http://topic.csdn.net/u/20081218/17/a3ee1ea4-35f0-4e3a-ae69-127a214d4cb8.html