#region using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; #endregion namespace CSFrameworkV5.Common { /// /// 图片处理 /// public class CImageLibrary { /// /// 检查图片返回的结果 /// public enum ValidateImageResult { OK, InvalidFileSize, InvalidImageSize } /// /// 数组转换为图片 /// /// . /// 数组 /// public static Image FromBytes(byte[] bs) { if (bs == null) return null; using (var ms = new MemoryStream(bs)) { return Image.FromStream(ms); } } /// /// 获取1x1像素的空图片 /// /// public static byte[] GetEmptyImageByte() { Bitmap bmp = null; try { using (bmp = new Bitmap(1, 1)) { return GetImageBytes(bmp, ImageFormat.Jpeg); } } finally { if (bmp != null) bmp.Dispose(); } } /// /// 图片转换为数组 /// /// 图片文件 /// public static byte[] GetImageBytes(string imageFileName, ImageFormat format) { var img = Image.FromFile(imageFileName); var bs = GetImageBytes(img, format); img.Dispose(); return bs; } /// /// 图片转换为数组 /// /// 图片实例 /// public static byte[] GetImageBytes(Image img, ImageFormat format) { if (img == null) return null; MemoryStream ms = null; Bitmap bmp = null; try { using (ms = new MemoryStream()) { using (bmp = new Bitmap(img)) { bmp.Save(ms, format); return ms.ToArray(); } } } finally { if (ms != null) ms.Dispose(); if (bmp != null) bmp.Dispose(); } } /// /// 按宽度比例缩小图片 /// /// 原始图片 /// 最大宽度 /// public static Image ResizeImage(Image imgSource, int MAX_WIDTH, int MAX_HEIGHT) { var imgOutput = imgSource; var size = new Size(0, 0); //用于存储按比例计算后的宽和高参数 if (imgSource.Width <= 3 || imgSource.Height <= 3) return imgSource; //3X3大小的图片不转换 //按宽度缩放图片 if (imgSource.Width > MAX_WIDTH) //计算宽度 { var rate = MAX_WIDTH / (double)imgSource.Width; //计算宽度比例因子 size.Width = Convert.ToInt32(imgSource.Width * rate); size.Height = Convert.ToInt32(imgSource.Height * rate); imgOutput = imgSource.GetThumbnailImage(size.Width, size.Height, null, IntPtr.Zero); } //按高度缩放图片 if (imgOutput.Height > MAX_HEIGHT) //计算高度 { var rate = MAX_HEIGHT / (double)imgOutput.Height; //计算宽度比例因子 size.Width = Convert.ToInt32(imgOutput.Width * rate); size.Height = Convert.ToInt32(imgOutput.Height * rate); imgOutput = imgSource.GetThumbnailImage(size.Width, size.Height, null, IntPtr.Zero); } return imgOutput; } /// /// 转换为BMP格式 /// /// 原图 /// 返回BMP格式的图片 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文件. } /// /// 转换为JPG格式 /// /// 原图 /// 返回JPG格式的图片 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文件. } /// /// 转换为PNG格式 /// /// 原图 /// 返回PNG格式的图片 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文件. } /// /// 检查图片文件大小 /// /// 图片文件 /// 最大文件大小 /// 最大宽度 /// 最大高度 /// 返回检查结果 public static ValidateImageResult ValidateImage(string file, int MAX_FILE_SIZE, int MAX_WIDTH, int MAX_HEIGHT) { var 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(); } } } }