|
图像处理业务逻辑(C/S框架网开源)
图像处理业务逻辑(C/S框架网开源)
using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Drawing; using System.Drawing.Imaging; /* * 版权:C/S框架网 www.csframework.com * * 图像处理业务逻辑 */ namespace CSFramework.BLL { public class CImageLibrary { /// <summary> /// 检查图片尺寸 /// </summary> public enum ValidateImageResult { OK, InvalidFileSize, InvalidImageSize } /// <summary> /// 是否Flash格式 /// </summary> /// <param name="fileName"></param> /// <returns></returns> public static bool ValidateFlash(string fileName) { byte[] header = new byte[8]; FileStream fs = new FileStream(fileName, FileMode.Open); try { fs.Read(header, 0, 8); } finally { fs.Close(); } string ext = Path.GetExtension(fileName);//文件扩展名 string sHeader = Encoding.Default.GetString(header); sHeader = sHeader.Substring(0, 3); //文件头三个字节"FWS(无压缩),CWS(标准ZLIB已压缩)" return (ext.ToUpper() == ".SWF") && ((sHeader == "FWS") || (sHeader == "CWS")); } /// <summary> /// 检查文件是否图片文件类型 /// </summary> /// <param name="fileName"></param> /// <returns></returns> public static bool ValidateImage(string fileName) { bool isImage = false; Image img = Image.FromFile(fileName); if (img.RawFormat.Equals(ImageFormat.Jpeg) || img.RawFormat.Equals(ImageFormat.Png) || img.RawFormat.Equals(ImageFormat.Gif) || img.RawFormat.Equals(ImageFormat.Icon)) { isImage = true; } return isImage; } //检查图片大小 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 = Image.FromFile(file); if (img.Width > MAX_WIDTH || img.Height > MAX_HEIGHT) return ValidateImageResult.InvalidImageSize; return ValidateImageResult.OK; } //按宽度比例缩小图片 public static Image GetOutputSizeImage(Image imgSource, int MAX_WIDTH) { Image imgOutput = imgSource; Size size = new Size(imgSource.Width, imgSource.Height); if (imgSource.Width <= 3 || imgSource.Height <= 3) return imgSource; //3X3大小的图片不转换 if (imgSource.Width > MAX_WIDTH || imgSource.Height > MAX_WIDTH) { double rate = MAX_WIDTH / (double)imgSource.Width; if (imgSource.Height * rate > MAX_WIDTH) rate = MAX_WIDTH / (double)imgSource.Height; size.Width = Convert.ToInt32(imgSource.Width * rate); size.Height = Convert.ToInt32(imgSource.Height * rate); imgOutput = imgSource.GetThumbnailImage(size.Width, size.Height, null, IntPtr.Zero); } return imgOutput; } //按比例缩小图片 public static Image GetOutputSizeImage(Image imgSource, Size outSize) { Image imgOutput = imgSource.GetThumbnailImage(outSize.Width, outSize.Height, null, IntPtr.Zero); return imgOutput; } public static byte[] GetImageBytes(string imageFileName) { Image img = Image.FromFile(imageFileName); return GetImageBytes(img); } /// <summary> /// 获取图片大小 /// </summary> /// <param name="img"></param> /// <returns></returns> public static byte[] GetImageBytes(Image img) { if (img == null) return null; try { System.IO.MemoryStream ms = new MemoryStream(); img.Save(ms, ImageFormat.Jpeg); byte[] bs = ms.ToArray(); ms.Close(); return bs; } catch { return null; } } /// <summary> /// Byte数组转换为图像类 /// </summary> /// <param name="bs"></param> /// <returns></returns> public static Image FromBytes(byte[] bs) { if (bs == null) return null; try { MemoryStream ms = new MemoryStream(bs); Image returnImage = Image.FromStream(ms); ms.Close(); return returnImage; } catch { return null; } } } } // 来源:www.CSFramework.com, C/S结构框架学习网
参考文档:
Asp.Net生成图片验证码(C/S框架网开源) C#源代码高亮着色类(C/S框架网开源) Asp.Net使用SmtpClient发送邮件(C/S框架网开源) Asp.Net FileUpload类实现上传文件(C/S框架网开源) MES系统快速开发平台|MES开源框架|C/S框架网 原创MES系统框架及MES开源框架 | C/S框架网 C#开源框架 | .NET开源快速开发平台 | C/S框架网 通用图片处理类CImageLibrary,预设存储JPG格式|C/S框架网 C/S框架网的开源框架优势
其它资料:
什么是C/S结构? | C/S框架核心组成部分 | C/S框架-WebService部署图 | C/S框架-权限管理 | C/S结构系统框架 - 3.0高级版介绍 | C/S结构系统框架 - 功能介绍 | C/S结构系统框架 - 产品列表 | C/S结构系统框架 - 应用展示(图) | |
|