通用图片处理类CImageLibrary,预设存储JPG格式|C/S框架网
通用图片处理类CImageLibrary,预设存储JPG格式|C/S框架网
修正BUG: 原来存储的是BMP格式,因此数据量较大,现改为JPG格式,优化程序速度! C# Code: /// <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 ms1 = new MemoryStream(); try { img.Save(ms1, format); byte[] bs1 = ms1.ToArray(); ms1.Close(); return bs1; } catch { ms1.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
参考文档:
图像处理业务逻辑CImageLibrary类(C/S框架网开源) Asp.Net生成图片验证码(C/S框架网开源) 通用文本提示对话框窗体支持保存文本文件|C/S框架网 CS开发框架之自增字段(Identity),时间戳(Timestamp)字段处理|C/S框架网 原创文档-Winform开发框架提供通用导入Excel功能界面|C/S框架网 Winform通用框架(C#.NET)之权限管理系统详细设计|C/S框架网 Winform+DevExpress框架之独立图片存储解决方案|C/S框架网 Winform快速开发框架之存储图片资源解决方案|C/S框架网 C#.NET短信通用接口设计-调用短信API案例|C/S框架网 Winform界面的功能按钮同等权限控制|C/S框架网 加密解密tb_DataSet/tb_MyUser表的密码|C/S框架网 关于C/S快速开发框架采用的文本加密解密策略|C/S框架网 CSFramework开发框架标准版成功案例-凹印制版ERP系统|C/S框架网 CSFramework企业版V4与旗舰版V5区别|C/S框架网 Winform增删查改基础框架|C/S框架网
其它资料:
什么是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内容管理系统 | |