c#利用Grahics进行图片裁剪
最开始用了
/// <summary>
/// 裁剪图片
/// </summary>
/// <param name="imagepath"/>
/// <param name="savepath">"c:\images\"</param>
private list<string> defclipimage(string imagepath, string savepath)
{
var fileinfo = new fileinfo(imagepath);
if (!fileinfo.exists)
throw new exception("图片" + imagepath + "不存在!");
var savepathlist = new list<string>();
var spath = savepath + fileinfo.name.replace(fileinfo.extension, string.empty);
try
{
var bitmap = new bitmap(imagepath);
var format = bitmap.pixelformat;
bitmap clonebitmap = bitmap.clone(_clonerect1, format);
var temppath = spath + "_1.jpg";
clonebitmap.save(temppath);
savepathlist.add(temppath);
clonebitmap.dispose();
clonebitmap = bitmap.clone(_clonerect2, format);
temppath = spath + "_2.jpg";
clonebitmap.save(temppath);
savepathlist.add(temppath);
clonebitmap.dispose();
clonebitmap = bitmap.clone(_clonerect3, format);
temppath = spath + "_3.jpg";
clonebitmap.save(temppath);
savepathlist.add(temppath);
clonebitmap.dispose();
clonebitmap = bitmap.clone(_clonerect4, format);
temppath = spath + "_4.jpg";
clonebitmap.save(temppath);
savepathlist.add(temppath);
clonebitmap.dispose();
bitmap.dispose();
return savepathlist;
}
catch
{
throw new exception("图片" + imagepath + "处理失败!");
}
}
但是速度太慢。
后来发现用grahics 会快很多
private void test()
{
bitmap bitmap = new bitmap(application.startuppath + @"\image\1.jpg");
var bt = new bitmap(7500, 3750);
var grahics = graphics.fromimage(bt);
grahics.drawimage(bitmap, _clonerect1, _clonerect1,graphicsunit.pixel);
bt.save(application.startuppath + "1.jpg");
grahics.drawimage(bitmap, _clonerect1, _clonerect2, graphicsunit.pixel);
bt.save(application.startuppath + "2.jpg");
grahics.drawimage(bitmap, _clonerect1, _clonerect3, graphicsunit.pixel);
bt.save(application.startuppath + "3.jpg");
grahics.drawimage(bitmap, _clonerect1, _clonerect4, graphicsunit.pixel);
bt.save(application.startuppath + "4.jpg");
grahics.dispose();
bt.dispose();
}