Ho preso dalla rete una procedura in c# che serve a rendere trasparente un gif.
Ho provato a tradurlo in vb.net
ma niente da fare.
Forse quello che è sfuggito a me sarà più chiaro a voi. Confido nel vostro aiuto:
c#
codice:
public static void MakeTransparentGif(ref Bitmap bmp, Color color)
{
byte R = color.R;
byte G = color.G;
byte B = color.B;
MemoryStream fin = new MemoryStream();
bmp.Save(fin, System.Drawing.Imaging.ImageFormat.Gif);
MemoryStream fout = new MemoryStream((int)fin.Length);
int count = 0;
byte[] buf = new byte[256];
byte transparentIdx = 0;
fin.Seek(0, SeekOrigin.Begin);
//header
count = fin.Read(buf, 0, 13);
if ((buf[0] != 71) || (buf[1] != 73) || (buf[2] != 70)) return; //GIF
fout.Write(buf, 0, 13);
int i = 0;
if ((buf[10] & 0x80) > 0)
{
i = 1 << ((buf[10] & 7) + 1) == 256 ? 256 : 0;
}
for (; i != 0; i--)
{
fin.Read(buf, 0, 3);
if ((buf[0] == R) && (buf[1] == G) && (buf[2] == B))
{
transparentIdx = (byte)(256 - i);
}
fout.Write(buf, 0, 3);
}
bool gcePresent = false;
while (true)
{
fin.Read(buf, 0, 1);
fout.Write(buf, 0, 1);
if (buf[0] != 0x21) break;
fin.Read(buf, 0, 1);
fout.Write(buf, 0, 1);
gcePresent = (buf[0] == 0xf9);
while (true)
{
fin.Read(buf, 0, 1);
fout.Write(buf, 0, 1);
if (buf[0] == 0) break;
count = buf[0];
if (fin.Read(buf, 0, count) != count) return;
if (gcePresent)
{
if (count == 4)
{
buf[0] |= 0x01;
buf[3] = transparentIdx;
}
}
fout.Write(buf, 0, count);
}
}
while (count > 0)
{
count = fin.Read(buf, 0, 1);
fout.Write(buf, 0, 1);
}
fin.Close();
fout.Flush();
//return new Bitmap(fout);
bmp.Dispose();
bmp = new Bitmap(fout);
}
vb.net
codice:
Public Sub MakeTransparentGif(ByRef bmp As Bitmap, ByVal color As Color)
Dim R As Byte = color.R
Dim G As Byte = color.G
Dim B As Byte = color.B
Dim fin As New MemoryStream()
bmp.Save(fin, System.Drawing.Imaging.ImageFormat.Gif)
Dim fout As New MemoryStream(CInt(fin.Length))
Dim count As Integer = 0
Dim buf As Byte() = New Byte(255) {}
Dim transparentIdx As Byte = 0
fin.Seek(0, SeekOrigin.Begin)
'header
count = fin.Read(buf, 0, 13)
If (buf(0) <> 71) OrElse (buf(1) <> 73) OrElse (buf(2) <> 70) Then 'GIF
Return
End If
fout.Write(buf, 0, 13)
Dim i As Integer = 0
If (buf(10) And 128) > 0 Then
If ((buf(10) And 7) + 1) = 256 Then
i = 1 << 256
Else
i = 1 << 0
End If
End If
While i <> 0
fin.Read(buf, 0, 3)
If (buf(0) = R) AndAlso (buf(1) = G) AndAlso (buf(2) = B) Then
transparentIdx = CByte((256 - i))
End If
fout.Write(buf, 0, 3)
i -= 1
End While
Dim gcePresent As Boolean = False
While True
fin.Read(buf, 0, 1)
fout.Write(buf, 0, 1)
If buf(0) <> 33 Then
Exit While
End If
fin.Read(buf, 0, 1)
fout.Write(buf, 0, 1)
gcePresent = (buf(0) = 249)
While True
fin.Read(buf, 0, 1)
fout.Write(buf, 0, 1)
If buf(0) = 0 Then
Exit While
End If
count = buf(0)
If fin.Read(buf, 0, count) <> count Then
Return
End If
If gcePresent Then
If count = 4 Then
buf(0) = 1
buf(3) = transparentIdx
End If
End If
fout.Write(buf, 0, count)
End While
End While
While count > 0
count = fin.Read(buf, 0, 1)
fout.Write(buf, 0, 1)
End While
fin.Close()
fout.Flush()
'return new Bitmap(fout);
bmp.Dispose()
bmp = New Bitmap(fout)
End Sub