通用图片处理类CImageLibrary,预设存储JPG格式
作者:C/S框架网  发布日期:2014-12-27 18:06:09
  通用图片处理类CImageLibrary,预设存储JPG格式

  通用图片处理类CImageLibrary,预设存储JPG格式


BUG: 原来存储的是BMP格式,因此数据量较大,现改为JPG格式,速度大大增强!


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



上一篇 下一篇