因为我想把生成的GIF文件放在一个有图片背景的TD中,如果生成的GIF文件不透明的话,将会把背景图片覆盖了

解决方案 »

  1.   

    gif的背景是自己制作的,不是C#可以控制的,你可以用photoshop來制作,或者上網下載透明的gif圖片。如果單純隻想做透明gif的話,可以使用Animagic GIF 32bit,不必用photoshop。
      

  2.   

    string filePath = Server.MapPath(".")+"\\Gif\\" + fileName;
    if (File.Exists(filePath))
    {
    //如果文件存在,则直接显示出来
    Bitmap bitmap = new Bitmap(filePath);
    Response.ContentType = "image/gif";
    bitmap.Save(Response.OutputStream,ImageFormat.Gif);
    bitmap.Dispose();
    }
    else
    {
    objBitmap = new Bitmap(1,1);
    g = Graphics.FromImage(objBitmap);
    SizeF stringSize = g.MeasureString(showText, stringFont);
    int nSpace = (showText.Length-1) * fontSpace;
    int nWidth = nSpace+(int)stringSize.Width; 
    int nHeight = (int)stringSize.Height; 
    g.Dispose(); 
    objBitmap.Dispose(); objBitmap = new Bitmap(nWidth,nHeight); 
    g = Graphics.FromImage(objBitmap);
    Color color = Color.FromArgb(255,Color.Transparent); //背景透明
    g.FillRectangle(new SolidBrush(color), new Rectangle(0,0,nWidth,nHeight));

    g.TextRenderingHint = TextRenderingHint.AntiAlias;  //提高字符显示质量 int fontLength = showText.Length;
    int j=0; //配合定位每个字的位置
    //对数组中的每个字进行循环输出
    for (int i=0; i<showText.Length; i++)
    {
    g.DrawString(showText.Substring(i,1), stringFont, new SolidBrush(fontColor), new PointF(j, 0), stringFormat);  //画字
    j = j + fontSpace + (int)stringSize.Width / showText.Length;
     
    }

    //将objGraphics对象保存到指定的stream对象,并输出到客户端
    objBitmap.Save(Response.OutputStream, ImageFormat.Gif);
    //将objBitmap保存到系统目录下
    objBitmap.Save(filePath,ImageFormat.Gif);
    }可是这样生成的GIF却不是透明的
      

  3.   

    用gdi+,不过我没做过,看看这篇
    http://www.cnblogs.com/caca/archive/2006/06/24/434825.html
      

  4.   

    高手哪去了,怎么没有人遇到这样的问题吗?
    <table>
     <tr>
      <td background="D:\\bottom.jpg">
       <img src="http://localhost/Picture/Picture.aspx? fontName=&fontSize=20&fontSpace=10&showText=&red=0&green=0&blue=0"></img>
      </td>
     </tr>
    </table>这样做是为了动态添加图片上的文字,Picture.aspx的功能就是生成一个含有文字的透明GIF
      

  5.   

    http://www.cnblogs.com/jillzhang/archive/2006/10/17/530919.html
      

  6.   

    Imports System.Runtime.InteropServices
    Imports System
    Imports System.IO
    Imports System.Drawing
    Imports System.Drawing.Imaging
    Imports System.Drawing.Drawing2DPublic Class transparentGif : Inherits System.Web.UI.Page    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load        Dim pic As New System.Drawing.Bitmap(200, 200, PixelFormat.Format24bppRgb)        '''draw image
            Dim blackpen As New Pen(Color.Black, 3)
            Dim redpen As New Pen(Color.Red, 4)
            Dim silverpen As New Pen(ColorTranslator.FromHtml("#CCCCCC"), 10)
            Dim fBrush As SolidBrush = New SolidBrush(ColorTranslator.FromHtml("#0000FF"))
            Dim g As Graphics = Graphics.FromImage(pic)        g.Clear(Color.White) ' blank the image
            g.DrawLine(silverpen, 7, 80, 110, 80)
            g.SmoothingMode = SmoothingMode.AntiAlias ' antialias objects
            g.DrawString("TEST", New Font("verdana", 24, FontStyle.Bold), fBrush, New PointF(10, 50))
            g.DrawEllipse(blackpen, 5, 5, 110, 110)
            g.DrawEllipse(redpen, 1, 1, 118, 118)        '''save new image
            pic = recolorGif(pic)        ' set the content type
            Response.ContentType = "image/gif"        ' send the image to the viewer
            pic.Save(Response.OutputStream, ImageFormat.Gif)        ' tidy up
            pic.Dispose()    End Sub    Private Function GetColorPalette() As ColorPalette
            ' Make a new Bitmap object to get its Palette.
            Dim bitmap As Bitmap = New Bitmap(1, 1, PixelFormat.Format8bppIndexed)
            Dim palette As ColorPalette = bitmap.Palette   ' Grab the palette
            bitmap.Dispose()
            Return palette              ' Send the palette back
        End Function
        Private Function recolorGif(ByVal image As Image) As Bitmap
            Dim nColors As Integer = 16        ' Make a new 8-BPP indexed bitmap that is the same size as the source image.
            Dim Width As Integer = image.Width
            Dim Height As Integer = image.Height        Dim bitmap As Bitmap = New Bitmap(Width, Height, PixelFormat.Format8bppIndexed)        ' Create a color palette big enough to hold the colors you want.
            Dim pal As ColorPalette = GetColorPalette()        ' Initialize a new color table with entries 
            Dim i As Integer        ' set palette the lazy way!
            ' replace with a proper color algorithm
            For i = 0 To nColors - 1
                pal.Entries(i) = Color.FromArgb(255, 100, 100, 100)
            Next        pal.Entries(0) = Color.FromArgb(255, 0, 0, 0)
            pal.Entries(1) = Color.FromArgb(255, 255, 0, 0)
            pal.Entries(2) = Color.FromArgb(255, 0, 255, 0)
            pal.Entries(3) = Color.FromArgb(255, 0, 0, 255)
            pal.Entries(4) = Color.FromArgb(255, 204, 204, 204)
            pal.Entries(nColors - 1) = Color.FromArgb(0, 255, 255, 255)        'web safe palette use values =
            '00 51 102 153 204 255         ' Set the palette into the new Bitmap object.
            bitmap.Palette = pal        Dim BmpCopy As Bitmap = New Bitmap(Width, Height, PixelFormat.Format32bppArgb)        Dim g As Graphics
            g = Graphics.FromImage(BmpCopy)        g.PageUnit = GraphicsUnit.Pixel        ' Transfer the Image to the Bitmap.
            g.DrawImage(image, 0, 0, Width, Height)        ' Force g to release its resources, namely BmpCopy.
            g.Dispose()        ' Lock a rectangular portion of the bitmap for writing.
            Dim bitmapData As BitmapData
            Dim rect As Rectangle = New Rectangle(0, 0, Width, Height)        bitmapData = bitmap.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed)        ' Copy the pixels from the source image 
            Dim pixels As IntPtr = bitmapData.Scan0
            Dim bits As Byte()      ' the buffer
            Dim pBits As Int32        If (bitmapData.Stride > 0) Then
                pBits = pixels.ToInt32()
            Else
                pBits = pixels.ToInt32() + bitmapData.Stride * (Height - 1)
            End If        Dim stride As Integer = Math.Abs(bitmapData.Stride)
            ReDim bits(Height * stride) ' Allocate the working buffer.        Dim row As Integer
            Dim col As Integer        For row = 0 To Height - 1
                For col = 0 To Width - 1                Dim pixel As Color
                    Dim i8BppPixel As Integer = row * stride + col                pixel = BmpCopy.GetPixel(col, row)                Dim colorIndex As Double
                    If pixel.R = 0 And pixel.G = 0 And pixel.B = 0 Then
                        colorIndex = 0
                    ElseIf pixel.R > 100 And pixel.G = 0 And pixel.B = 0 Then
                        colorIndex = 1
                    ElseIf pixel.G > 100 And pixel.R = 0 And pixel.B = 0 Then
                        colorIndex = 2
                    ElseIf pixel.B > 100 And pixel.R = 0 And pixel.G = 0 Then
                        colorIndex = 3
                    ElseIf pixel.B = 204 And pixel.R = 204 And pixel.G = 204 Then
                        colorIndex = 4
                    Else
                        colorIndex = (nColors - 1)
                    End If                bits(i8BppPixel) = CByte(colorIndex)            Next col
            Next row        ' Put the image bits definition into the bitmap.
            Dim win32 As win32api = New win32api()
            win32.CopyArrayTo(pBits, bits, Height * stride)
            bitmap.UnlockBits(bitmapData)        Return bitmap        BmpCopy.Dispose()
            bitmap.Dispose()    End FunctionEnd ClassPublic Class win32api
        <DllImport("KERNEL32.DLL", EntryPoint:="RtlMoveMemory", _
           SetLastError:=True, CharSet:=CharSet.Auto, _
           ExactSpelling:=True, _
           CallingConvention:=CallingConvention.StdCall)> _
        Public Shared Sub CopyArrayTo(<[In](), MarshalAs(UnmanagedType.I4)> ByVal hpvDest As Int32, <[In](), Out()> ByVal hpvSource() As Byte, ByVal cbCopy As Integer)
            ' Leave function empty
        End SubEnd Class