温馨提示:代码在线浏览功能只能做为源码浏览参考,不能展示项目的全部,如果想更进一步了解该代码请下载:NETCMSv1.5(Build0509)完整源码版
当前文件路径:NetCMSv15/NetCMS.Content/Common/FSImage.cs

1//====================================================== 2
//== (c)2008 aspxcms inc by NeTCMS v1.0 == 3
//== Forum:bbs.aspxcms.com == 4
//== Website:www.aspxcms.com == 5
//====================================================== 6
using System; 7
using System.Collections.Generic; 8
using System.Text; 9
using System.IO; 10
using System.Web; 11
using System.Drawing; 12
using System.Drawing.Imaging; 13
14
namespace NetCMS.Content.Common 15
{ 16
/// <summary> 17
/// 生成水印、缩略图、生成有指定内容的图片 18
/// </summary> 19
public class FSImage 20
{ 21
private int _width; 22
private int _height; 23
private string _title = "www.netcms"; //文字标题 24
private Color _bgcolor = Color.White; 25
private int _fontsize = 14; //字号 26
private string _familyname = "仿宋"; //字体 27
private FontStyle _fontstyle = FontStyle.Regular; 28
private Color _forcecolor = Color.Black; 29
private string _filepath = ""; //获取路径及文件名 30
private PointF _txtpos; 31
private long _quality =100; 32
private string _waterpos = "0"; 33
private string _waterpath = ""; 34
/// <summary> 35
/// 构建函数 36
/// </summary> 37
/// <param name="width">生成图片的宽度。 38
/// 如果小于或等于零:高度也小于或等于零,则图片按原图的大小(水印或缩略图)或标题文字的大小生成图片;如高度大于零,则按高度优先比例缩放原图 39
/// 如果大于零:高度也大于零,按指定大小生成新图片;如果高度小于等于零,则按宽度优先比较缩放图片</param> 40
/// <param name="height">图片高度,见图片宽度注释</param> 41
/// <param name="filepath">原文件物理磁盘路径(水印、缩略图)或保存文件路径(按文字生成图片)</param> 42
public FSImage(int width, int height, string filepath) 43
{ 44
_txtpos = new PointF(-1, -1); 45
_width = width; 46
_height = height; 47
_filepath = filepath.Replace("/","\\"); 48
} 49
/// <summary> 50
/// 水印或生成图片的文字,默认为:www.netcms 51
/// </summary> 52
public string Title 53
{ 54
set { _title = value; } 55
get { return _title; } 56
} 57
/// <summary> 58
/// 以像素为单位的字体大小,默认为:14 59
/// </summary> 60
public int FontSize 61
{ 62
get { return _fontsize; } 63
set { _fontsize = value; } 64
} 65
/// <summary> 66
/// 设置新图片的背景色,默认为透明 67
/// </summary> 68
public Color BackGroudColor 69
{ 70
get { return _bgcolor; } 71
set { _bgcolor = value; } 72
} 73
/// <summary> 74
/// 采用的字体名称,默认为仿宋体 75
/// </summary> 76
public string FontFamilyName 77
{ 78
get { return _familyname; } 79
set { _familyname = value; } 80
} 81
/// <summary> 82
/// 设置字体样式,默认为普通文本 83
/// </summary> 84
public FontStyle StrStyle 85
{ 86
set { _fontstyle = value; } 87
get { return _fontstyle; } 88
} 89
/// <summary> 90
/// 水印文字的颜色,默认为红色 91
/// </summary> 92
public Color FontColor 93
{ 94
set { _forcecolor = value; } 95
get { return _forcecolor; } 96
} 97
/// <summary> 98
/// 水印文本的位置坐标,以图片的左上角为原点,默认位置为图片的右下角 99
/// 如果x座标小于零则文字在图片的右边,如果y座标小于0则文字在图片的下边 100
/// 设置时注意这个位置是从文字的左上角为原点,如(0,0)表示文字在图片的左上角 101
/// </summary> 102
public PointF TextPos 103
{ 104
set { _txtpos.X = value.X; _txtpos.Y = value.Y; } 105
get { return _txtpos; } 106
} 107
108
/// <summary> 109
/// 输出图片质量,质量范围0-100,类型为long 110
/// </summary> 111
public long Quality 112
{ 113
get { return _quality; } 114
set { _quality = value; } 115
} 116
117
/// <summary> 118
/// 水印位置 119
/// </summary> 120
public string Waterpos 121
{ 122
get { return _waterpos; } 123
set { _waterpos = value; } 124
} 125
126
/// <summary> 127
/// 水印图片宽度 128
/// </summary> 129
public int Width 130
{ 131
get { return _width; } 132
set { _width = value; } 133
} 134
135
/// <summary> 136
/// 水印图片高度 137
/// </summary> 138
public int Height 139
{ 140
get { return _height; } 141
set { _height = value; } 142
} 143
144
/// <summary> 145
/// 水印图片地址 146
/// </summary> 147
public string WaterPath 148
{ 149
get { return _waterpath; } 150
set { _waterpath = value; } 151
} 152
153
/// <summary> 154
/// 生成文字水印 155
/// </summary> 156
public void Watermark() 157
{ 158
if (_title == null || _title.Equals("")) 159
throw new NullReferenceException("图片标题为空!"); 160
ImageFormat format = GetFormat(); 161
if (!File.Exists(_filepath)) 162
throw new FileNotFoundException("指定路径的文件不存在"); 163
Graphics g; 164
Bitmap bitmap = GetBitmap(out g); 165
Font f = new Font(_familyname, _fontsize, _fontstyle, GraphicsUnit.Pixel); 166
SizeF size = g.MeasureString(_title, f); 167
switch (_waterpos) 168
{ 169
case "1": 170
_txtpos.X = ((float)_width * (float).01) + (size.Width / 2) - 20; 171
_txtpos.Y = (float)_height * (float).01; 172
break; 173
case "3": 174
_txtpos.X = ((float)_width * (float).99) - (size.Width); 175
_txtpos.Y = (float)_height * (float).01; 176
break; 177
case "4": 178
_txtpos.X = ((float)_width * (float).99) - (size.Width); 179
_txtpos.Y = ((float)_height * (float).99) - size.Height; 180
break; 181
case "2": 182
_txtpos.X = ((float)_width * (float).01) + (size.Width / 2) - 20; 183
_txtpos.Y = ((float)_height * (float).99) - size.Height; 184
break; 185
default: 186
_txtpos.X = ((float)_width * (float).50) - 20; 187
_txtpos.Y = ((float)_height * (float).50); 188
break; 189
} 190
Brush b = new SolidBrush(_forcecolor); 191
g.DrawString(_title, f, b, _txtpos); 192
g.Dispose(); 193
bitmap.Save(_filepath, format); 194
bitmap.Dispose(); 195
} 196
/// <summary> 197
/// 生成缩略图 198
/// </summary> 199
/// <param name="newpath">缩略图的保存物理路径(包括文件名),如果为null或空则覆盖原图</param> 200
public void Thumbnail(string newpath) 201
{ 202
ImageFormat format = GetFormat(); 203
if (!File.Exists(_filepath)) 204
throw new FileNotFoundException("指定路径的文件不存在"); 205
Graphics g; 206
Bitmap bitmap = GetBitmap(out g); 207
if (newpath == null || newpath.Trim().Equals("")) 208
bitmap.Save(_filepath, format); 209
else 210
{ 211
newpath = newpath.Replace("/","\\"); 212
if (newpath.IndexOf("\\") > 0) 213
{ 214
string dir = newpath.Substring(0, newpath.LastIndexOf("\\")); 215
if (!Directory.Exists(dir)) 216
{ 217
Directory.CreateDirectory(dir); 218
} 219
}




