C#图片转换类支持按比例缩放图片
C#图片转换类支持按比例缩放图片一个图片转换类,比如PNG转JPG,BMP转PNG按比例缩小图片
分享一个图片转换类,比如 PNG ->JPG, BMP -> PNG按比例缩小图片。 C# Code: using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; namespace www.csframework.com { /// <summary> /// 图片处理 /// </summary> public class CImageLibrary { /// <summary> /// 检查图片返回的结果 /// </summary> public enum ValidateImageResult { OK, InvalidFileSize, InvalidImageSize } /// <summary> /// 检查图片文件大小 /// </summary> /// <param name="file">图片文件</param> /// <param name="MAX_FILE_SIZE">最大文件大小</param> /// <param name="MAX_WIDTH">最大宽度</param> /// <param name="MAX_HEIGHT">最大高度</param> /// <returns>返回检查结果</returns> public static ValidateImageResult ValidateImage(string file, int MAX_FILE_SIZE, int MAX_WIDTH, int MAX_HEIGHT) { byte[] bs = File.ReadAllBytes(file); double size = (bs.Length / 1024); //大于50KB if (size > MAX_FILE_SIZE) return ValidateImageResult.InvalidFileSize; Image img = null; try { img = Image.FromFile(file); if (img.Width > MAX_WIDTH || img.Height > MAX_HEIGHT) return ValidateImageResult.InvalidImageSize; else return ValidateImageResult.OK; } finally { img.Dispose(); } } /// <summary> /// 等比例缩放图片 /// </summary> /// <param name="bitmap">原图</param> /// <param name="destWidth">缩放后宽度</param> /// <param name="destHeight">缩放后高度</param> /// <returns></returns> public static Bitmap ZoomImage(Bitmap bitmap, int destWidth, int destHeight) { try { System.Drawing.Image sourImage = bitmap; int width = 0, height = 0; //按比例缩放 int sourWidth = sourImage.Width; int sourHeight = sourImage.Height; if (sourHeight > destHeight || sourWidth > destWidth) { if ((sourWidth * destHeight) > (sourHeight * destWidth)) { width = destWidth; height = (destWidth * sourHeight) / sourWidth; } else { height = destHeight; width = (sourWidth * destHeight) / sourHeight; } } else { width = sourWidth; height = sourHeight; } Bitmap destBitmap = new Bitmap(width, height); //Bitmap destBitmap = new Bitmap(destWidth, destHeight); Graphics g = Graphics.FromImage(destBitmap); //g.Clear(Color.Transparent); g.Clear(Color.White); //设置画布的描绘质量 g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; //g.DrawImage(sourImage, new Rectangle((destWidth - width) / 2, (destHeight - height) / 2, width, height), 0, 0, sourImage.Width, sourImage.Height, GraphicsUnit.Pixel); g.DrawImage(sourImage, new Rectangle(0, 0, width, height), 0, 0, sourImage.Width, sourImage.Height, GraphicsUnit.Pixel); g.Dispose(); //设置压缩质量 //System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters(); //long[] quality = new long[1]; //quality[0] = 100; //System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality); //encoderParams.Param[0] = encoderParam; //sourImage.Dispose(); return destBitmap; } catch { return bitmap; } } /// <summary> /// 按宽度比例缩小图片 /// </summary> /// <param name="imgSource">原始图片</param> /// <param name="MAX_WIDTH">最大宽度</param> /// <returns></returns> public static Image ResizeImage(Image imgSource, int MAX_WIDTH, int MAX_HEIGHT) { return ZoomImage(new Bitmap(imgSource), MAX_WIDTH, MAX_HEIGHT); } /// <summary> /// 图片转换为数组 /// </summary> /// <param name="imageFileName">图片文件</param> /// <returns></returns> public static byte[] GetImageBytes(string imageFileName, ImageFormat format) { Image img = Image.FromFile(imageFileName); byte[] bs = GetImageBytes(img, format); img.Dispose(); return bs; } /// <summary> /// 图片转换为数组 /// </summary> /// <param name="img">图片实例</param> /// <returns></returns> public static byte[] GetImageBytes(Image img, ImageFormat format) { if (img == null) return null; MemoryStream ms = new MemoryStream(); try { Bitmap bmp = new Bitmap(img); bmp.Save(ms, format); byte[] bs = ms.ToArray(); ms.Close(); return bs; } catch { ms.Close(); return null; } } /// <summary> /// 数组转换为图片 /// </summary>. /// <param name="bs">数组</param> /// <returns></returns> public static Image FromBytes(byte[] bs) { if (bs == null) return null; MemoryStream ms = null; try { ms = new MemoryStream(bs); Image returnImage = Image.FromStream(ms); return returnImage; } catch { if (ms != null) ms.Close(); return null; } } /// <summary> /// 转换为BMP格式 /// </summary> /// <param name="source">原图</param> /// <returns>返回BMP格式的图片</returns> public static Image ToBMP(Image source, string outFileName) { try { source.Save(outFileName, ImageFormat.Bmp); } catch (Exception ex) { throw new Exception("原图文件流可能已关闭!\r\n" + ex.Message); } if (source.RawFormat == ImageFormat.Bmp) { return source as Bitmap; //BMP格式不需要转换 } return Image.FromFile(outFileName);//加载存储的BMP文件. } /// <summary> /// 转换为JPG格式 /// </summary> /// <param name="source">原图</param> /// <returns>返回JPG格式的图片</returns> public static Image ToJPG(Image source, string outFileName) { try { source.Save(outFileName, ImageFormat.Jpeg); } catch (Exception ex) { throw new Exception("原图文件流可能已关闭!\r\n" + ex.Message); } if (source.RawFormat == ImageFormat.Jpeg) { return source as Bitmap; //BMP格式不需要转换 } return Image.FromFile(outFileName);//加载存储的BMP文件. } /// <summary> /// 转换为PNG格式 /// </summary> /// <param name="source">原图</param> /// <returns>返回PNG格式的图片</returns> public static Image ToPNG(Image source, string outFileName) { try { source.Save(outFileName, ImageFormat.Png); } catch (Exception ex) { throw new Exception("原图文件流可能已关闭!\r\n" + ex.Message); } if (source.RawFormat == ImageFormat.Png) { return source as Bitmap; //BMP格式不需要转换 } return Image.FromFile(outFileName);//加载存储的BMP文件. } /// <summary> /// 获取1x1像素的空图片 /// </summary> /// <returns></returns> public static byte[] GetEmptyImageByte() { Bitmap bmp = new Bitmap(1, 1); return GetImageBytes(bmp, ImageFormat.Jpeg); } } } //来源:C/S框架网(www.csframework.com) QQ:23404761
参考文档:
C#.NET扩展的动态对象支持动态属性和方法以及Json格式与对象互转 C#.NET DateTime时间格式转换为Unix时间戳格式(int类型) C# ADO.NET根据实体对象类型转换创建DataTable对象 C# 下载Web网页指定URL图片并转换为Base64String格式 C# Model对象实例转换为URL网址参数Param形式 C# GUID序列号转换生成唯一的16位短字符串或19位长数字序号 C#数据转换类ConvertEx,封装.Net的Convert对象 C# Image图片转Icon图标 C# Dev GridView表格的CustomUnboundColumnData事件异步加载图片类AsynDownImage C# 动态对象转换为实体对象支持泛型(DynamicToEntity) C# Dev GridView表格使用RepositoryItemPictureEdit显示图标或图片 C# 不占用文件句柄方式加载图片文件(加载到内存流MemoryStream) C# 本地图片缓存器ImageFileCache (www.csframework.com) C# 将GUID转换为最长16位的短字符串序号 WCF开发框架 - BasicHttpBinding绑定的web.config与C#代码配置(支持https协议)
其它资料:
什么是C/S结构? | C/S框架核心组成部分 | C/S框架-WebService部署图 | C/S框架-权限管理 | C/S结构系统框架 - 5.1旗舰版介绍 | C/S结构系统框架 - 功能介绍 | C/S结构系统框架 - 产品列表 | C/S结构系统框架 - 应用展示(图) | 三层体系架构详解 | C/S架构轻量级快速开发框架 | C/S框架网客户案例 | WebApi快速开发框架 | C/S框架代码生成器 | 用户授权注册软件系统 | 版本自动升级软件 | 数据库底层应用框架 | CSFramework.CMS内容管理系统 | |