图像处理业务逻辑(C/S框架网开源)
作者:C/S原创  发布日期:2011-03-17 23:40:40
图像处理业务逻辑(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结构框架学习网


上一篇 下一篇