首页 > 代码库 > ImageHelper
ImageHelper
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Configuration; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Drawing.Drawing2D; namespace Water.Helper { /// <summary> /// 图片处理辅助类 /// </summary> public class ImageHelper { private static int thumbnailsOutWidth = int.Parse(ConfigurationManager.AppSettings["ThumbnailsOutWidth"]); #region 获取缩略图对象 /// <summary> /// 返回一个图片对像 /// </summary> /// <param name="img">图片源</param> /// <param name="towidth">输出宽度</param> /// <param name="toheight">输出高度</param> /// <param name="x">图片X位置,当图片宽度小于输出图片宽度时可设置此项</param> /// <param name="y">图片Y位置,当图片宽度小于输出图片宽度时可设置此项</param> /// <param name="pw">图片宽度</param> /// <param name="ph">图片高度</param> /// <param name="bgcolor">填充背景颜色</param> /// <returns></returns> private static Bitmap getImage(Image img, int towidth, int toheight, int x, int y, int pw, int ph, Color bgcolor) { //新建一个bmp图片 Bitmap bitmap = new Bitmap(towidth, toheight, PixelFormat.Format32bppArgb); bitmap.SetResolution(img.HorizontalResolution, img.VerticalResolution); //新建一个画板 Graphics g = Graphics.FromImage(bitmap); //设置高质量插值法 g.CompositingQuality = CompositingQuality.HighQuality; g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; //清空画布并以白色填充 g.Clear(bgcolor); //在指定位置并且按指定大小绘制原图片的指定部分 //g.DrawImage(img, 0, 0, towidth, toheight); g.DrawImage(img, new Rectangle(x, y, pw, ph), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel); g.Dispose(); img.Dispose(); return bitmap; } /// <summary> /// 获取缩略图对象 /// </summary> /// <returns></returns> public static Bitmap GetThumImage(Stream fileStream) { //新建一个bmp图片 Bitmap bitmap = null; //获取一个图片对象 Image img = Image.FromStream(fileStream); //获取原图的文件格式 ImageFormat tFormat = ImageFormat.Jpeg; float YPicWidth = (float)img.Width; float YPicHeight = (float)img.Height; float OutPicWidth = (float)thumbnailsOutWidth; float OutPicHeight = 0f; //按尺寸裁切 float ybl = YPicWidth / YPicHeight; //根据输出图片宽度获取缩放后图片高度 OutPicHeight = YPicHeight * ((float)thumbnailsOutWidth / YPicWidth); //返回缩放后的图片再进行裁切 bitmap = getImage(img, (int)OutPicWidth, (int)OutPicHeight, 0, 0, (int)OutPicWidth, (int)OutPicHeight, Color.White); //释放源对象 img.Dispose(); return bitmap; } #endregion } }
ImageHelper
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。