利用Grahics 进行图片裁剪_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > 利用Grahics 进行图片裁剪

利用Grahics 进行图片裁剪

 2013/12/12 18:09:20  GISER_U  博客园  我要评论(0)
  • 摘要:这两天做了一个图片对比工具,里面要处理两张大的图片,所以要对图片先进行裁剪最开始用了///<summary>///裁剪图片///</summary>///<paramname="imagePath"/>///<paramname="savePath">"c:\images\"</param>privateList<string>DefClipImage(stringimagePath,stringsavePath)
  • 标签:图片 利用

这两天做了一个图片对比工具,里面要处理两张大的图片,所以要对图片先进行裁剪最开始用了

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

 

最后上个做的工具的效果图,采用开源地图控件:)

发表评论
用户名: 匿名