#region 图片处理 /// <summary> /// 生成缩略图重载方法1,返回缩略图的Image对象 /// </summary> /// <param name="Width">缩略图的宽度</param> /// <param name="Height">缩略图的高度</param> /// <returns>缩略图的Image对象</returns> public Image GetReducedImage(int Width, int Height) { try { Image ResourceImage = Image.FromFile(""); //用指定的大小和格式初始化Bitmap类的新实例 Bitmap bitmap = new Bitmap(Width, Height, PixelFormat.Format32bppArgb); //从指定的Image对象创建新Graphics对象 Graphics graphics = Graphics.FromImage(bitmap); //清除整个绘图面并以透明背景色填充 graphics.Clear(Color.Transparent); //在指定位置并且按指定大小绘制原图片对象 graphics.DrawImage(ResourceImage, new Rectangle(0, 0, Width, Height)); return bitmap; } catch (Exception e) { return null; } } /// <summary> /// 截取图片方法 /// </summary> /// <param name="url">图片地址</param> /// <param name="beginX">开始位置-X</param> /// <param name="beginY">开始位置-Y</param> /// <param name="getX">截取宽度</param> /// <param name="getY">截取长度</param> /// <param name="fileName">文件名称</param> /// <param name="savePath">保存路径</param> /// <param name="fileExt">后缀名</param> public static string CutImage(string url, int beginX, int beginY, int getX, int getY, string fileName, string savePath, string fileExt) { if ((beginX < getX) && (beginY < getY)) { Bitmap bitmap = new Bitmap(url);//原图 if (((beginX + getX) <= bitmap.Width) && ((beginY + getY) <= bitmap.Height)) { Bitmap destBitmap = new Bitmap(getX, getY);//目标图 Rectangle destRect = new Rectangle(0, 0, getX, getY);//矩形容器 Rectangle srcRect = new Rectangle(beginX, beginY, getX, getY); Graphics.FromImage(destBitmap); //Graphics.DrawImage(bitmap, destRect, srcRect, GraphicsUnit.Pixel); ImageFormat format = ImageFormat.Png; switch (fileExt.ToLower()) { case "png": format = ImageFormat.Png; break; case "bmp": format = ImageFormat.Bmp; break; case "gif": format = ImageFormat.Gif; break; } destBitmap.Save(savePath + "//" + fileName, format); return savePath + "\" + "*" + fileName.Split('.')[0] + "." + fileExt; } else { return "截取范围超出图片范围"; } } else { return "请确认(beginX < getX)&&(beginY < getY)"; } } #endregion