天天看点

[GDI+] 生成缩略图的类文件SmallImage (转载)

直接看代码吧,

大家可以直接复制使用

/// <summary>
/// 类说明:SmallImage类,
/// 编码日期:2012-08-20
/// 编 码 人:  苏飞
/// 联系方式:361983679  Email:[url=mailto:[email protected]][email protected][/url]  Blogs:[url=http://www.cckan.net]http://www.cckan.net[/url]
/// 修改日期:2012-08-20
/// </summary>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace SystemImage
{
    public class SmallImage
    {
        //产生缩略图,高是100像素,宽按比例缩小
        public static void GetSmallImage(string originalImagePath, string thumbnailPath, int width, int height)
        {
            System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
            if (originalImage.Width < originalImage.Height)
            {
                width = originalImage.Width * width / originalImage.Height;//按比例指定宽度,高度固定不变,是100个像素,整个图像按比例缩小            
            }
            else
            {
                height = originalImage.Height * height / originalImage.Width;
            }
            System.Drawing.Image bitmap = new System.Drawing.Bitmap(width,height);            
            Graphics g = System.Drawing.Graphics.FromImage(bitmap);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;//.HighQualityBicubic;//设置高质量插值法            
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;//.HighQuality;//设置高质量,低速度呈现平滑程度           
            g.Clear(Color.Transparent); //清空画布并以透明背景色填充
            g.DrawImage(originalImage, new Rectangle(0, 0, width, height), new Rectangle(0, 0, originalImage.Width, originalImage.Height), GraphicsUnit.Pixel);
            bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
            originalImage.Dispose(); 
            bitmap.Dispose();
            g.Dispose();           
        }
 
        //检查大图像,如果宽超过四百,或高超过六百,就按比例缩放
        public static bool CheckBigAreaImg(string url,string newUrl)
        {
            bool result = false;
            using (System.Drawing.Image img = Bitmap.FromFile(url))
            {
                int height = img.Size.Height;//获得用户上传图片的宽度和高度
                int width = img.Size.Width;               
          
                if (height > 800 || width > 530)
                {
                    result = true;
                    GetSmallImage(url, newUrl, 500, 500);
                } 
            }
            if (result)
            {
                File.Delete(url);
            }
            return result;
        }
    }    
}      

转载于:https://www.cnblogs.com/lizeyan/p/3628273.html

ui