欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#利用Grahics进行图片裁剪

程序员文章站 2024-02-19 21:24:58
最开始用了复制代码 代码如下:///         /// 裁剪图片&...

最开始用了

复制代码 代码如下:

/// <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();
        }