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)
' 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
' 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
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));