程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> asp.net(C#)縮略圖生成類

asp.net(C#)縮略圖生成類

編輯:.NET實例教程

using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace Eyu.Common
...{ 
    /**//// <summary>
    /// 縮放處理
    /// </summary>
    public class Thumbnail
    ...{
        private string workingDirectory = string.Empty; //圖片所在目錄
        private string imgFileName = string.Empty;   //圖片名稱
        private ThumbnailMode mode = ThumbnailMode.ByWidth;
        private int scaleValue = 0;
        /**//// <summary>
        /// 圖片所在目錄
        /// </summary>
        public string WorkingDirectory
        ...{
            get
            ...{
                return workingDirectory;
            }
            set
            ...{
                workingDirectory = value;
            }
        }
        /**//// <summary>
        /// 圖片名稱
        /// </summary>
        public string ImageName
        ...{
            get
            ...{
                return imgFileName;
            }
            set
            ...{
                imgFileName = value;
            }
        }
        /**//// <summary>
        /// 設置縮略模式
        /// </summary>
        public ThumbnailMode Mode
        ...{
            get ...{ return mode; }
            set ...{ mode = value; }
        }
        /**//// <summary>
        /// 縮放尺寸
        /// </summary>
        public int ScaleValue
        ...{
            get ...{ return scaleValue; }
            set ...{ scaleValue = value; }
        }
        /**//// <summary>
        /// 圖片縮略模式
        /// </summary>
        public enum ThumbnailMode
        ...{
            ByPercent,  //按百分比縮放
            ByHeight,   //按高度等比縮放
            ByWidth,    //按寬度等比縮放

        }
        /**//// <summary>
        /// 按模式縮放圖片,輸出圖片名格式為:原圖文件名T.jpg
        /// </summary>
        public void MakeThumbnail()
        ...{
            System.Drawing.Image originalImage = System.Drawing.Image.FromFile(WorkingDirectory + ImageName);
    int towidth = 0;
            int toheight = 0;

            int ow = originalImage.Width;
            int oh = originalImage.Height;

            switch (Mode)
            ...{
                case ThumbnailMode.ByWidth://指定寬,高按比例
                    if (ow >= ScaleValue)
                    ...{
                        toheight = originalImage.Height * ScaleValue / originalImage.Width;
                        towidth = ScaleValue;
                    }
                        
                    else
                    ...{
                        towidth = ow;
                        toheight = oh;
                    }     
                    break;
                case ThumbnailMode.ByHeight://指定高,寬按比例
                    if (oh >= ScaleValue)
                    ...{
                        toheight = ScaleValue;
                        towidth = originalImage.Width * ScaleValue / originalImage.Height;
                    }   
                    else
                    ...{
                        towidth = ow;
                        toheight = oh;
                    }
                    break;
                case ThumbnailMode.ByPercent://按比例縮放                
                    towidth = ow / ScaleValue;
         &nb      toheight = oh / ScaleValue;
                    break;
                default:
                    break;
            }

            //新建一個bmp圖片
            System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
            //新建一個畫板
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
            //設置高質量插值法
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            //設置高質量,低速度呈現平滑程度
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            //清空畫布並以透明背景色填充
            g.Clear(System.Drawing.Color.Transparent);
            //在指定位置並且按指定大小繪制原圖片的指定部分
            g.DrawImage(originalImage, 0, 0, towidth, toheight);
            
            g.Dispose();
            originalImage.Dispose();
            originalImage = (System.Drawing.Image)bitmap.Clone();
            originalImage.Save(WorkingDirectory + ImageName + "T.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
            bitmap.Dispose();
        }
    }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved