最近更新:2010年7月2日0:12:28
Image image = System.Drawing.Image.FromFile(@"C:\A.JPG");
Bitmap pbitmap = newBitmap(image);
pbitmap.MakeTransparent(Color.White);
pictureBox1.Image = pbitmap;
我是不太明白微软是如何处理图片的。貌似可以“点”出很多东西出来,但是还是有很多人喜欢在编写C#的时候调用API去完成相对简单的事。以下代码一大堆,自己看。
附带一个java生成透明的图片文件http://www.cnblogs.com/dotjava/archive/2008/09/04/1283963.html
public Bitmap MakeTransparentGif(Bitmap bitmap, Color color)
{
byte R = color.R;
byte G = color.G;
byte B = color.B;
MemoryStream fin = new MemoryStream();
bitmap.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 null; //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 null;
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);
}