您的位置:首页 > 其它

生成透明GIF的方法

2009-10-28 22:12 148 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: