2张gif图片连接后背景出现黑色,求教高手解决。。。。。。

活着便精彩 2008-10-21 08:11:25
private void button1_Click(object sender, EventArgs e)
{
////方法一
Image i1 = Image.FromFile(@"D:\tool\2005\demo\battery1.gif");
Image i2 = Image.FromFile(@"D:\tool\2005\demo\battery2.gif");
GetImage(i1, i2);

//方法二
//Bitmap bmp = new Bitmap(72, 36);
//Image img1 = new Bitmap(this.p1.Image, 36, 36);
//Image img2 = new Bitmap(this.p2.Image, 36 ,36);
//Graphics g = Graphics.FromImage(bmp);
//g.DrawImage(img1, 0, 0);
//g.DrawImage(img2, img1.Width, 0);
//this.p3.Image = (Image)bmp;
//this.p3.Image.Save(@"D:\tool\2005\demo\111.gif", ImageFormat.Gif);

}

public void GetImage(Image img1, Image img2)
{
int width = img1.Width + img2.Width;
int height = img1.Height > img2.Height ? img1.Height : img2.Height;
Bitmap bmp = new Bitmap(width, height);
Graphics graphics = Graphics.FromImage(bmp);

Rectangle rect1 = new Rectangle(0, 0, img1.Width, img1.Height);
Rectangle rect2 = new Rectangle(rect1.Right, 0, img2.Width, img2.Height);
graphics.DrawImage(img1,rect1,0,0,img1.Width,img1.Height,GraphicsUnit.Pixel);
graphics.DrawImage(img2,rect2,0,0,img2.Width,img2.Height,GraphicsUnit.Pixel);

graphics.Flush();
graphics.Dispose();
bmp.MakeTransparent();
bmp.Save(@"D:\tool\2005\demo\battery2.gif", ImageFormat.Gif);
bmp.Dispose();
}
...全文
414 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
starts_2000 2008-10-22
  • 打赏
  • 举报
回复
我已经帮你解决了,QQ511029008
活着便精彩 2008-10-22
  • 打赏
  • 举报
回复
2楼用的是白色背景啊,透明怎么改呢 晕死了
qshzf 2008-10-22
  • 打赏
  • 举报
回复
up
slimfeng 2008-10-21
  • 打赏
  • 举报
回复
肯定跟你图片有关,gif文件一般有多帧组成,你读取的时候只读取第一帧,
楼主可以采用下方法设成需要的颜色(可以从Image1或Image2中提取)

public void GetImage(Image img1, Image img2)
{
int width = img1.Width + img2.Width;
int height = img1.Height > img2.Height ? img1.Height : img2.Height;
Bitmap bmp = new Bitmap(width, height);
int x, y;
Graphics graphics = Graphics.FromImage(bmp);
bmp.Save(@"f:\test4.gif", ImageFormat.Gif);//此处可以看到创建的默认图片,一般为黑色

// Loop through the images pixels to reset color.
for (x = 0; x < bmp.Width; x++)
{
for (y = 0; y < bmp.Height; y++)
{
Color pixelColor = bmp.GetPixel(x, y);
Color newColor = Color.FromArgb(255, 255, 255);
bmp.SetPixel(x, y, newColor);
}
}


Rectangle rect1 = new Rectangle(0, 0, img1.Width, img1.Height);
Rectangle rect2 = new Rectangle(rect1.Right, 0, img2.Width, img2.Height);
graphics.DrawImage(img1, rect1, 0, 0, img1.Width, img1.Height, GraphicsUnit.Pixel);
graphics.DrawImage(img2, rect2, 0, 0, img2.Width, img2.Height, GraphicsUnit.Pixel);

graphics.Flush();
graphics.Dispose();
//bmp.MakeTransparent();//注释掉
bmp.Save(@"f:\test3.gif", ImageFormat.Gif);
bmp.Dispose();
}

zx005 2008-10-21
  • 打赏
  • 举报
回复
bmp.MakeTransparent();
加个这个干什么,我试了下去掉这个就没黑色的了

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧