asp.net中用C#自动生成透明的GIF图片
需在IMG图片的Style属性设为:FILTER: chroma(color:#000000)就能实现自动生成的GIF图片的透明了。因为自动生成的GIF图片的底色总是黑色。
如果你真的要生成一个透明背景的GIF。我可以给你一段code。自己参照一下。
Imports System.Runtime.InteropServices
Imports System
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Drawing2D
Public 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 Function
End Class
Public 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 Sub
End Class
如果你真的要生成一个透明背景的GIF。我可以给你一段code。自己参照一下。
Imports System.Runtime.InteropServices
Imports System
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Drawing2D
Public 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 Function
End Class
Public 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 Sub
End Class