pre { /*控制代码不换行*/ white-space: pre; word-wrap: normal; }

分享一个C#缩略图生成类

很多时候我们上传图片,通常会对原图进行缩略图生成的处理,便于在其他地方显示,从而加快页面的加载速度。那么在C#中,我们如何来生成缩略图呢? 下面我给大家分享一个在C#中生成缩略图的类,大家只要根据需要传入参数,调用一下方法即可完成缩略图的生成工作,十分方便。好了,不多说了,直接上代 码。

缩略图生成类代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
public class Thumbnail
    {
        private string _OrigImagePath;
        private string _ThumbSavePath;
        private int _Width;
        private int _Height;
        private bool _Stretched;
        private bool _Beveled;
        private bool _AutoSaveThumb;
 
        /// <summary>         /// 图片源文件路径
        /// </summary>         public string OrigImagePath
        {
            get { return _OrigImagePath; }
            set { _OrigImagePath = value; }
        }
 
        /// <summary>         /// 缩略图存储路径
        /// </summary>         public string ThumbSavePath
        {
            get { return _ThumbSavePath; }
            set { _ThumbSavePath = value; }
        }
 
        /// <summary>         /// 缩略图宽度
        /// </summary>         public int Width
        {
            get { return _Width; }
            set { _Width = value; }
        }
 
        /// <summary>         /// 缩略图高度
        /// </summary>         public int Height
        {
            get { return _Height; }
            set { _Height = value; }
        }
 
        /// <summary>         /// 是否拉伸图片
        /// </summary>         public bool Stretched
        {
            get { return _Stretched; }
            set { _Stretched = value; }
        }
 
        /// <summary>         /// 是否使用图片边框模糊特效
        /// </summary>         public bool Beveled
        {
            get { return _Beveled; }
            set { _Beveled = value; }
        }
 
        /// <summary>         /// 是否自动保存缩略图
        /// </summary>         public bool AutoSaveThumb
        {
            get { return _AutoSaveThumb; }
            set { _AutoSaveThumb = value; }
        }
 
 
        public Thumbnail()
        {
        }
 
        public Thumbnail(string oPath, string sPath, int width, int height, bool stretched)
        {
            _OrigImagePath = oPath;
            _ThumbSavePath = sPath;
            _Width = width;
            _Height = height;
            _Stretched = stretched;
        }
 
        /// <summary>         /// 获取图片的缩略图
        /// </summary>         /// <returns></returns>         public Bitmap GetThumbnail()
        {
            Bitmap bitmapNew;
            float fx, fy, f;
            float widthOrig, heightOrig;
            int widthTh, heightTh;
 
            bitmapNew = new Bitmap(_OrigImagePath); //加载图片
 
            if (!_Stretched)
            {
                widthOrig = bitmapNew.Width;
                heightOrig = bitmapNew.Height;
                fx = widthOrig / _Width;
                fy = heightOrig / _Height;
 
                f = Math.Max(fx, fy);
                if (f < 1)
                {
                    f = 1;
                }
 
                widthTh = (int)(widthOrig / f);
                heightTh = (int)(heightOrig / f);
            }
            else
            {
                widthTh = _Width;
                heightTh = _Height;
            }
 
            bitmapNew = (Bitmap)bitmapNew.GetThumbnailImage(widthTh, heightTh, new Image.GetThumbnailImageAbort(ThumbnailCallback),
             IntPtr.Zero);
 
            if (_Beveled)
            {
 
                int widTh, heTh;
                widTh = bitmapNew.Width; heTh = bitmapNew.Height;
                int BevW = 10, LowA = 0, HighA = 180, Dark = 80, Light = 255;
                // hilight color, low and high
                Color clrHi1 = Color.FromArgb(LowA, Light, Light, Light);
                Color clrHi2 = Color.FromArgb(HighA, Light, Light, Light);
                Color clrDark1 = Color.FromArgb(LowA, Dark, Dark, Dark);
                Color clrDark2 = Color.FromArgb(HighA, Dark, Dark, Dark);
 
                LinearGradientBrush br;
                Rectangle rectSide;
 
                Graphics newG = Graphics.FromImage(bitmapNew);
                Size szHorz = new Size(widTh, BevW);
                Size szVert = new Size(BevW, heTh);
                // ---- draw dark (shadow) sides first
                // draw bottom-side of bevel
                szHorz += new Size(0, 2);
                szVert += new Size(2, 0);
                rectSide = new Rectangle(new Point(0, heTh - BevW), szHorz);
                br = new LinearGradientBrush(
                  rectSide, clrDark1, clrDark2, LinearGradientMode.Vertical);
                rectSide.Inflate(0, -1);
                newG.FillRectangle(br, rectSide);
                // draw right-side of bevel
                rectSide = new Rectangle(new Point(widTh - BevW, 0), szVert);
                br = new LinearGradientBrush(
                  rectSide, clrDark1, clrDark2, LinearGradientMode.Horizontal);
                rectSide.Inflate(-1, 0);
                newG.FillRectangle(br, rectSide);
                // ---- draw bright (hilight) sides next
                szHorz -= new Size(0, 2); szVert -= new Size(2, 0);
                // draw top-side of bevel
                rectSide = new Rectangle(new Point(0, 0), szHorz);
                br = new LinearGradientBrush(
                  rectSide, clrHi2, clrHi1, LinearGradientMode.Vertical);
                newG.FillRectangle(br, rectSide);
                // draw left-side of bevel
                rectSide = new Rectangle(new Point(0, 0), szVert);
                br = new LinearGradientBrush(
                  rectSide, clrHi2, clrHi1, LinearGradientMode.Horizontal);
                newG.FillRectangle(br, rectSide);
                // dispose graphics objects and return bitmap
                br.Dispose(); newG.Dispose();
 
            }
 
            if (_AutoSaveThumb)
            {
                SaveThumbnail(bitmapNew, _ThumbSavePath + GetUniqueThumbName());
            }
            return bitmapNew;
        }
 
        /// <summary>         /// 获取一个唯一的缩略图的名称
        /// </summary>         /// <returns></returns>         public String GetUniqueThumbName()
        {
            StringBuilder sUN = new StringBuilder();
            sUN.AppendFormat("Thumb_{0}_{1}x{2}",
              Path.GetFileName(_OrigImagePath), _Width, _Height);
            if (_Stretched) sUN.Append("_S");
            if (_Beveled) sUN.Append("_B");
            sUN.Append(".jpg");
            return sUN.ToString();
        }
 
        /// <summary>         /// 将缩略图以jpeg格式保存
        /// </summary>         public void SaveThumbnail(Bitmap bmThumb, String path)
        {
            bmThumb.Save(path, ImageFormat.Jpeg);
        }
 
 
        public bool ThumbnailCallback() { return false; }
 
    }

调用缩略图类代码:

1
2
3
4
5
6
7
8
string sPath = "c:\\1.jpg"//原图路径
string dPath = "c:\\2.jpg"//生成缩略图的路径
int width = 300;             //缩略图宽度
int height = 600;            //缩略图高度
bool stretched = false;      //是否自动拉伸,如果false则保持原来的比例,否则按width和height拉伸
Thumbnail tb = new Thumbnail(sPath, dPath, width, height, stretched);
Bitmap pic = tb.GetThumbnail();
tb.SaveThumbnail(pic, dPath);

好了,就到这里,非常简单,希望对大家有所帮助

posted @ 2012-03-09 02:14  monkey's  阅读(1237)  评论(1)    收藏  举报