图片操作的类(ImageLibrary)-按比例缩放图片

 

/*******************************************************************
    * 凡注明易学原创的源代码及文档不得进行商业用途转载 *
    * 如需转载请注明出处 *

    * 易学网祝您学业有成!*    
/*******************************************************************


源代码说明:
 1.按比例缩放图片, 2.获取图片文件大小

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace vjsdn.Common
{
   public class ImageLibrary
   {
      public enum ValidateImageResult { OK, InvalidFileSize, InvalidImageSize }
      
      //public const string LogoImageErrorMsg = "仅支持图片宽度不超过400像素或者高度不超过400像素";
      //public const string CaseBackImageErrorMsg = "";
      
      //检查图片大小
      public static ValidateImageResult ValidateImage(string file)
      {
         byte[] bs = File.ReadAllBytes(file);
         
         double size = (bs.Length / 1024);
         //大于50KB
         if (size > 50) return ValidateImageResult.InvalidFileSize;
         Image img = Image.FromFile(file);
         if (img.Width > 400 && img.Height > 400) return ValidateImageResult.InvalidImageSize;
         return ValidateImageResult.OK;
      }
      
      //按比例缩小图片
      public static Image GetOutputSizeImage(Image imgSource, int MAX_SIZE)
      {
         Image imgOutput = imgSource;
         
         Size size = new Size(imgSource.Width, imgSource.Height);
         if (imgSource.Width == 0 || imgSource.Height == 0) return imgSource;
         
         if (imgSource.Width > MAX_SIZE || imgSource.Height > MAX_SIZE)
         {
            double rate = MAX_SIZE / (double)imgSource.Width;
            
            if (imgSource.Height * rate > MAX_SIZE)
            rate = MAX_SIZE / (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);
      }
      
      public static byte[] GetImageBytes(Image img)
      {
         if (img == null) return null;
         try
         {
            System.IO.MemoryStream ms = new MemoryStream();
            // img.Save(ms, img.RawFormat);
            img.Save(ms, ImageFormat.Jpeg);
            byte[] bs = ms.ToArray();
            ms.Close();
            return bs;
         }
         catch { return null; }
      }
      
      
      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; }
      }
   }
}

本页面由HTML生成器自动生成>>
原帖地址:http://www.vjsdn.com/bbs/bbstopicdetails.aspx?html=0&pid=241

 


posted @ 2009-10-14 09:56  海上村民  阅读(267)  评论(0)    收藏  举报