首页 > 代码库 > 图片处理
图片处理
#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
图片处理
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。