生成透明GIF的方法
Code
Private Shared Function CreateTransParentGif(ByVal img As Image) As Bitmap
Dim tempms As New System.IO.MemoryStream()
img.Save(tempms, ImageFormat.Gif)
img.Dispose()
Dim tempgif As Bitmap = Bitmap.FromStream(tempms)
tempms.Dispose()
Dim pal = tempgif.Palette 'GetColorPalette()
For index As Integer = 0 To pal.Entries.Length - 1
If pal.Entries(index).R = 0 And pal.Entries(index).G = 0 And pal.Entries(index).B = 0 Then
pal.Entries(index) = Color.FromArgb(0, 255, 255, 255)
End If
Next
Dim bd = tempgif.LockBits(New Rectangle(0, 0, tempgif.Width, tempgif.Height), ImageLockMode.ReadOnly, tempgif.PixelFormat)
Dim bytes(bd.Stride * bd.Height - 1) As Byte
Marshal.Copy(bd.Scan0, bytes, 0, bd.Stride * bd.Height)
tempgif.UnlockBits(bd)
'pal.Entries(40) = Color.FromArgb(0, 255, 255, 255)
'tempgif.Palette = pal
Dim outgif As New Bitmap(tempgif.Width, tempgif.Height, PixelFormat.Format8bppIndexed)
outgif.Palette = pal
Dim ind = outgif.LockBits(New Rectangle(0, 0, outgif.Width, outgif.Height), ImageLockMode.WriteOnly, outgif.PixelFormat)
Marshal.Copy(bytes, 0, ind.Scan0, ind.Stride * ind.Height)
outgif.UnlockBits(ind)
tempgif.Dispose()
Return outgif
End Function
Private Shared Function CreateTransParentGif(ByVal img As Image) As Bitmap
Dim tempms As New System.IO.MemoryStream()
img.Save(tempms, ImageFormat.Gif)
img.Dispose()
Dim tempgif As Bitmap = Bitmap.FromStream(tempms)
tempms.Dispose()
Dim pal = tempgif.Palette 'GetColorPalette()
For index As Integer = 0 To pal.Entries.Length - 1
If pal.Entries(index).R = 0 And pal.Entries(index).G = 0 And pal.Entries(index).B = 0 Then
pal.Entries(index) = Color.FromArgb(0, 255, 255, 255)
End If
Next
Dim bd = tempgif.LockBits(New Rectangle(0, 0, tempgif.Width, tempgif.Height), ImageLockMode.ReadOnly, tempgif.PixelFormat)
Dim bytes(bd.Stride * bd.Height - 1) As Byte
Marshal.Copy(bd.Scan0, bytes, 0, bd.Stride * bd.Height)
tempgif.UnlockBits(bd)
'pal.Entries(40) = Color.FromArgb(0, 255, 255, 255)
'tempgif.Palette = pal
Dim outgif As New Bitmap(tempgif.Width, tempgif.Height, PixelFormat.Format8bppIndexed)
outgif.Palette = pal
Dim ind = outgif.LockBits(New Rectangle(0, 0, outgif.Width, outgif.Height), ImageLockMode.WriteOnly, outgif.PixelFormat)
Marshal.Copy(bytes, 0, ind.Scan0, ind.Stride * ind.Height)
outgif.UnlockBits(ind)
tempgif.Dispose()
Return outgif
End Function