C#创建缩略图操作类实例
程序员文章站
2023-01-22 12:58:41
本文实例讲述了c#创建缩略图操作类。分享给大家供大家参考。具体分析如下:
这个c#类可以生成各种形式的缩略图,可以自动保持图片比例缩略,可以根据百分比获得图片尺寸等...
本文实例讲述了c#创建缩略图操作类。分享给大家供大家参考。具体分析如下:
这个c#类可以生成各种形式的缩略图,可以自动保持图片比例缩略,可以根据百分比获得图片尺寸等
using system; using system.collections.generic; using system.drawing; using system.drawing.imaging; namespace htmlsnap { public static class imagehelper { /// <summary> /// 获取缩略图 /// </summary> /// <param name="image"></param> /// <param name="width"></param> /// <param name="height"></param> /// <returns></returns> public static image getthumbnailimage(image image, int width, int height) { if (image == null || width < 1 || height < 1) return null; // 新建一个bmp图片 // image bitmap = new system.drawing.bitmap(width, height); // 新建一个画板 // using (graphics g = system.drawing.graphics.fromimage(bitmap)) { // 设置高质量插值法 // g.interpolationmode = system.drawing.drawing2d.interpolationmode.highqualitybicubic; // 设置高质量,低速度呈现平滑程度 // g.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality; // 高质量、低速度复合 // g.compositingquality = system.drawing.drawing2d.compositingquality.highquality; // 清空画布并以透明背景色填充 // g.clear(color.transparent); // 在指定位置并且按指定大小绘制原图片的指定部分 // g.drawimage(image, new rectangle(0, 0, width, height), new rectangle(0, 0, image.width, image.height), graphicsunit.pixel); return bitmap; } } /// <summary> /// 生成缩略图,并保持纵横比 /// </summary> /// <param name="image"></param> /// <param name="width"></param> /// <param name="height"></param> /// <returns>生成缩略图后对象</returns> public static image getthumbnailimagekeepratio(image image, int width, int height) { size imagesize = getimagesize(image, width, height); return getthumbnailimage(image, imagesize.width, imagesize.height); } /// <summary> /// 根据百分比获取图片的尺寸 /// </summary> /// <param name="picture"></param> /// <param name="percent"></param> /// <returns></returns> public static size getimagesize(image picture, int percent) { if (picture == null || percent < 1) return size.empty; int width = picture.width * percent / 100; int height = picture.height * percent / 100; return getimagesize(picture, width, height); } /// <summary> /// 根据设定的大小返回图片的大小,考虑图片长宽的比例问题 /// </summary> /// <param name="picture"></param> /// <param name="width"></param> /// <param name="height"></param> /// <returns></returns> public static size getimagesize(image picture, int width, int height) { if (picture == null || width < 1 || height < 1) return size.empty; size imagesize; imagesize = new size(width, height); double heightratio = (double)picture.height / picture.width; double widthratio = (double)picture.width / picture.height; int desiredheight = imagesize.height; int desiredwidth = imagesize.width; imagesize.height = desiredheight; if (widthratio > 0) imagesize.width = convert.toint32(imagesize.height * widthratio); if (imagesize.width > desiredwidth) { imagesize.width = desiredwidth; imagesize.height = convert.toint32(imagesize.width * heightratio); } return imagesize; } /// <summary> /// 获取图像编码解码器的所有相关信息 /// </summary> /// <param name="mimetype">包含编码解码器的多用途网际邮件扩充协议 (mime) 类型的字符串</param> /// <returns>返回图像编码解码器的所有相关信息</returns> public static imagecodecinfo getcodecinfo(string mimetype) { imagecodecinfo[] codecinfo = imagecodecinfo.getimageencoders(); foreach (imagecodecinfo ici in codecinfo) { if (ici.mimetype == mimetype) return ici; } return null; } public static imagecodecinfo getimagecodecinfo(imageformat format) { imagecodecinfo[] encoders = imagecodecinfo.getimageencoders(); foreach (imagecodecinfo icf in encoders) { if (icf.formatid == format.guid) { return icf; } } return null; } public static void saveimage(image image, string savepath, imageformat format) { saveimage(image, savepath, getimagecodecinfo(format)); } /// <summary> /// 高质量保存图片 /// </summary> /// <param name="image"></param> /// <param name="savepath"></param> /// <param name="ici"></param> private static void saveimage(image image, string savepath, imagecodecinfo ici) { // 设置 原图片 对象的 encoderparameters 对象 // encoderparameters parms = new encoderparameters(1); encoderparameter parm = new encoderparameter(encoder.quality, ((long)95)); parms.param[0] = parm; image.save(savepath, ici, parms); parms.dispose(); } } }
希望本文所述对大家的c#程序设计有所帮助。
上一篇: C#实现图像锐化的方法
下一篇: C#图像处理之头发检测的方法