TO:mm995(pighead) 按你的说法,我写了如下程序,但3.gif内容是空的,请赐教!!谢谢Bitmap fBitmap = new Bitmap(@"c:\1.gif"); Bitmap sBitmap = new Bitmap(@"c:\2.gif");
Bitmap b = new Bitmap(800,600); Graphics g = Graphics.FromImage(b);int fwidth = fBitmap.Size.Width; int fheight = fBitmap.Size.Height;int swidth = sBitmap.Size.Width; int sheight = sBitmap.Size.Height;g.DrawImage(fBitmap,8,23); g.DrawImage(sBitmap,8,23+fheight);b.Save(@"c:\3.gif");
先创建一个空的bitmap,然后从该bitmap获得一个graphics,
然后往该graphics中画你那两个tif,
最后把第一个创建的bitmap save就可以了。
先创建一个空的bitmap,然后从该bitmap获得一个graphics,请问如何从该bitmap获得一个graphics
Graphics g = Graphics.FromImage(bitmap);
按你的说法,我写了如下程序,但3.gif内容是空的,请赐教!!谢谢Bitmap fBitmap = new Bitmap(@"c:\1.gif");
Bitmap sBitmap = new Bitmap(@"c:\2.gif");
Bitmap b = new Bitmap(800,600);
Graphics g = Graphics.FromImage(b);int fwidth = fBitmap.Size.Width;
int fheight = fBitmap.Size.Height;int swidth = sBitmap.Size.Width;
int sheight = sBitmap.Size.Height;g.DrawImage(fBitmap,8,23);
g.DrawImage(sBitmap,8,23+fheight);b.Save(@"c:\3.gif");
b.Save(filename,ImageFormat.Gif);但我点击保存后,如果再单击保存则会出现一般性错误,不知为何!!
private void btnFirst_Click(object sender, System.EventArgs e)
{
IDataObject d = Clipboard.GetDataObject(); if(d.GetDataPresent(DataFormats.Bitmap))
{
Bitmap b = (Bitmap)d.GetData(DataFormats.Bitmap);
b.Save(@"c:\tmp1.gif",ImageFormat.Gif);
}
else
{
MessageBox.Show("当前剪贴板中未包含图象数据!!");
}
} private void btnSecond_Click(object sender, System.EventArgs e)
{
IDataObject d = Clipboard.GetDataObject(); if(d.GetDataPresent(DataFormats.Bitmap))
{
Bitmap b = (Bitmap)d.GetData(DataFormats.Bitmap);
b.Save(@"c:\tmp2.gif",ImageFormat.Gif);
}
else
{
MessageBox.Show("当前剪贴板中未包含图象数据!!");
}
} private void btnCombin_Click(object sender, System.EventArgs e)
{
int fwidth=0,fheight=0,swidth=0,sheight=0,twidth=0,theight=0;
Bitmap fBitmap=null,sBitmap=null,tBitmap=null; pBox.Image = null;
// 第一张图片
if(File.Exists(@"c:\tmp1.gif"))
{
fBitmap = new Bitmap(@"c:\tmp1.gif");
fwidth = fBitmap.Size.Width;
fheight = fBitmap.Size.Height;
}
else
{
MessageBox.Show("请先生成第一幅图片!!");
return;
} // 第二张图片
if(File.Exists(@"c:\tmp2.gif"))
{
sBitmap = new Bitmap(@"c:\tmp2.gif");
swidth = sBitmap.Size.Width;
sheight = sBitmap.Size.Height;
}
// 第三张图片
if(File.Exists(@"c:\tmp3.gif"))
{
tBitmap = new Bitmap(@"c:\tmp3.gif");
twidth = tBitmap.Size.Width;
theight = tBitmap.Size.Height;
}
Bitmap b = new Bitmap(fwidth,fheight+sheight+theight);
Graphics g = Graphics.FromImage(b); g.DrawImage(fBitmap,0,0);
if(File.Exists(@"c:\tmp2.gif"))
{
g.DrawImage(sBitmap,0,fheight);
}
if(File.Exists(@"c:\tmp3.gif"))
{
g.DrawImage(tBitmap,0,fheight+sheight);
} if(this.filename == null)
{
FileForm fileForm = new FileForm();
fileForm.par = this;
fileForm.ShowDialog(this);
} try
{
if(this.filename!=null)
{
b.Save(filename,ImageFormat.Gif);
pBox.Image = Image.FromFile(filename); if(File.Exists(@"c:\tmp1.gif"))
File.Delete(@"c:\tmp1.gif");
if(File.Exists(@"c:\tmp2.gif"))
File.Delete(@"c:\tmp2.gif");
if(File.Exists(@"c:\tmp3.gif"))
File.Delete(@"c:\tmp3.gif");
}
}
catch
{
MessageBox.Show("发生一般性错误,请重新运行该程序!!" + this.filename);
}
} private void btnExit_Click(object sender, System.EventArgs e)
{
this.Close();
} private void btnThird_Click(object sender, System.EventArgs e)
{
IDataObject d = Clipboard.GetDataObject(); if(d.GetDataPresent(DataFormats.Bitmap))
{
Bitmap b = (Bitmap)d.GetData(DataFormats.Bitmap);
b.Save(@"c:\tmp3.gif",ImageFormat.Gif);
}
else
{
MessageBox.Show("当前剪贴板中未包含图象数据!!");
}
}
怀疑可能是调用b.Save(filename,ImageFormat.Gif)时出错,
检查一下当filename存在时,是否可以调用b.Save(filename,ImageFormat.Gif)btw:你那个catch里总得把异常信息也显示出来吧
确实是try块中的b.save异常,出错信息是"GDI+ 中发生一般性错误"何解?
你单击第一次以后tmp1.gif,tmp2.gif,tmp3.git都将被删除,
这样第二次时,fBitmap,sBitmap,tBitmap都为空,btw:你程序的逻辑可能有错误,我C#也不熟,
我写代码测试一下
但还是会出现错误,好象是说tmp1.gif正由另一进程使用,我再贴部分代码你看一下吧!!private void btnFirst_Click(object sender, System.EventArgs e)
{
IDataObject d = Clipboard.GetDataObject();if(d.GetDataPresent(DataFormats.Bitmap))
{
Bitmap b = (Bitmap)d.GetData(DataFormats.Bitmap);
if(File.Exists(@"c:\HYBZSWAP\tmp1.gif"))
{
pBox1.Image = null;
File.Delete(@"c:\HYBZSWAP\tmp1.gif");
}
b.Save(@"c:\HYBZSWAP\tmp1.gif",ImageFormat.Gif);
pBox1.Size = b.Size;
pBox1.Image = Image.FromFile(@"c:\HYBZSWAP\tmp1.gif");}
else
{
MessageBox.Show("当前剪贴板中未包含图象数据!!");
}
}...// 合并按钮
private void btnCombin_Click(object sender, System.EventArgs e)
{
int fwidth=0,fheight=0,swidth=0,sheight=0,twidth=0,theight=0,owidth=0,oheight=0,iwidth=0,iheight=0,xwidth=0,xheight=0;
Bitmap fBitmap=null,sBitmap=null,tBitmap=null,oBitmap=null,iBitmap=null,xBitmap=null,b=null;// 第一张图片
if(File.Exists(@"c:\HYBZSWAP\tmp1.gif"))
{
fBitmap = new Bitmap(@"c:\HYBZSWAP\tmp1.gif");
fwidth = fBitmap.Size.Width;
fheight = fBitmap.Size.Height;
}
else
{
MessageBox.Show("请先生成第一幅图片!!");
return;
}
...if(this.filename == null)
{
// 获得合并后的文件名
...
fileForm.par = this;
fileForm.ShowDialog(this);
}try
{
if(this.filename!=null)
{
if(File.Exists(filename))
{
File.Delete(filename);
}
b.Save(filename,ImageFormat.Gif);
}
}
catch
{
MessageBox.Show("发生一般性错误,请重新运行该程序!!");
}
}
在btnCombin_Click中的3个bitmap没有释放资源,
所以当你再单击第一个按钮的时候tmp1.gif还在使用.在btnCombin_Click中的最后加下面语句应该就可以了:
fBitmap.Dispose();
sBitmap.Dispose();
tBitmap.Dispose();
private void btnFirst_Click(object sender, System.EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
IDataObject d = Clipboard.GetDataObject(); if(d.GetDataPresent(DataFormats.Bitmap))
{
Bitmap b = (Bitmap)d.GetData(DataFormats.Bitmap);
if (File.Exists(@"c:\tmp1.gif"))
File.Delete(@"c:\tmp1.gif");
b.Save(@"c:\tmp1.gif",ImageFormat.Gif);
}
else
{
MessageBox.Show("当前剪贴板中未包含图象数据!!");
}
this.Cursor = Cursors.Arrow;
} private void btnSecond_Click(object sender, System.EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
IDataObject d = Clipboard.GetDataObject(); if(d.GetDataPresent(DataFormats.Bitmap))
{
Bitmap b = (Bitmap)d.GetData(DataFormats.Bitmap);
if (File.Exists(@"c:\tmp2.gif"))
File.Delete(@"c:\tmp2.gif");
b.Save(@"c:\tmp2.gif",ImageFormat.Gif);
}
else
{
MessageBox.Show("当前剪贴板中未包含图象数据!!");
}
this.Cursor = Cursors.Arrow;
} private void btnCombine_Click(object sender, System.EventArgs e)
{
this.Cursor = Cursors.WaitCursor; int fwidth=0,fheight=0,swidth=0,sheight=0,twidth=0,theight=0;
Bitmap fBitmap=null,sBitmap=null,tBitmap=null; // 第一张图片
if(File.Exists(@"c:\tmp1.gif"))
{
fBitmap = new Bitmap(@"c:\tmp1.gif");
fwidth = fBitmap.Size.Width;
fheight = fBitmap.Size.Height;
}
else
{
MessageBox.Show("请先生成第1幅图片!!");
return;
} // 第二张图片
if(File.Exists(@"c:\tmp2.gif"))
{
sBitmap = new Bitmap(@"c:\tmp2.gif");
swidth = sBitmap.Size.Width;
sheight = sBitmap.Size.Height;
}
else
{
MessageBox.Show("请先生成第2幅图片!!");
return;
}
// 第三张图片
if(File.Exists(@"c:\tmp3.gif"))
{
tBitmap = new Bitmap(@"c:\tmp3.gif");
twidth = tBitmap.Size.Width;
theight = tBitmap.Size.Height;
}
else
{
MessageBox.Show("请先生成第3幅图片!!");
return;
}
Bitmap b = new Bitmap(fwidth,fheight+sheight+theight);
Graphics g = Graphics.FromImage(b); g.DrawImage(fBitmap,0,0);
g.DrawImage(sBitmap,0,fheight);
g.DrawImage(tBitmap,0,fheight+sheight); try
{
if(this.filename!=null)
{
if (File.Exists(@"c:\combine.gif"))
File.Delete(@"c:\combine.gif");
b.Save(@"c:\combine.gif",ImageFormat.Gif);
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
//释放资源
fBitmap.Dispose();
sBitmap.Dispose();
tBitmap.Dispose();
this.Cursor = Cursors.Arrow;
} private void btnThird_Click(object sender, System.EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
IDataObject d = Clipboard.GetDataObject(); if(d.GetDataPresent(DataFormats.Bitmap))
{
Bitmap b = (Bitmap)d.GetData(DataFormats.Bitmap);
if (File.Exists(@"c:\tmp3.gif"))
File.Delete(@"c:\tmp3.gif");
b.Save(@"c:\tmp3.gif",ImageFormat.Gif);
}
else
{
MessageBox.Show("当前剪贴板中未包含图象数据!!");
}
this.Cursor = Cursors.Arrow;
}
}
}
非常感谢你的解答,问题基本解决,但还有一个问题就是有可能我要多次单击btnFirst按钮,以重新生成第一张临时图片,此时也出现tmp1.gif还在使用的问题,按你的思路也不起作用,当然btnCombin已经按你所说的搞定了,如果阁下有时间的话就帮我看看,下午下班前结贴.private void btnFirst_Click(object sender, System.EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
IDataObject d = Clipboard.GetDataObject(); if(d.GetDataPresent(DataFormats.Bitmap))
{
Bitmap b = (Bitmap)d.GetData(DataFormats.Bitmap);
if(File.Exists(@"c:\HYBZSWAP\tmp1.gif"))
{
pBox1.Image = null;
File.Delete(@"c:\HYBZSWAP\tmp1.gif");
}
b.Save(@"c:\HYBZSWAP\tmp1.gif",ImageFormat.Gif);
pBox1.Size = b.Size;
pBox1.Image = Image.FromFile(@"c:\HYBZSWAP\tmp1.gif");
b.Dispose();
}
else
{
MessageBox.Show("当前剪贴板中未包含图象数据!!");
} this.Cursor = Cursors.Arrow;
}
{
this.Cursor = Cursors.WaitCursor;
IDataObject d = Clipboard.GetDataObject();
Bitmap b = null; if(d.GetDataPresent(DataFormats.Bitmap))
{
b = (Bitmap)d.GetData(DataFormats.Bitmap);
if(File.Exists(@"c:\HYBZSWAP\tmp1.gif"))
{
if(pBox1.Image != null) pBox1.Image.Dispose();
File.Delete(@"c:\HYBZSWAP\tmp1.gif");
}
b.Save(@"c:\HYBZSWAP\tmp1.gif",ImageFormat.Gif);
pBox1.Size = b.Size;
pBox1.Image = Image.FromFile(@"c:\HYBZSWAP\tmp1.gif");
}
else
{
MessageBox.Show("当前剪贴板中未包含图象数据!!");
}
if(b!=null) b.Dispose();
this.Cursor = Cursors.Arrow;
}To:mm995(pighead)
你到以下贴子领另外的100分吧,谢谢你的参与!!http://expert.csdn.net/Expert/topic/1408/1408986.xml?temp=.9579126
http://expert.csdn.net/Expert/topic/1410/1410026.xml?temp=2.895534E-03