封装的一些实现图片水印与图片自动结合缩放的类
2007-01-23 16:09
716 查看
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Web;
5
using System.Web.Security;
6
using System.Web.UI;
7
using System.Web.UI.WebControls;
8
using System.Web.UI.WebControls.WebParts;
9
using System.Web.UI.HtmlControls;
10
using System.Drawing;
11
namespace WebHelper
12
{
13
14
/**//// <summary>
15
/// ImageHelper 的摘要说明
16
/// </summary>
17
public class ImageHelper
18
{
19
public ImageHelper()
20
{
21
//
22
// TODO: 在此处添加构造函数逻辑
23
//
24
}
25
Image水印#region Image水印
26
/**//// <summary>
27
/// 写入图像水印
28
/// </summary>
29
/// <param name="str">水印字符串</param>
30
/// <param name="filePath">原图片位置</param>
31
/// <param name="savePath">水印加入后的位置</param>
32
/// <returns></returns>
33
public string CreateBackImage(System.Web.UI.Page pageCurrent, string str, string filePath, string savePath, int x, int y)
34
{
35
System.Drawing.Image img = System.Drawing.Image.FromFile(pageCurrent.MapPath(filePath));
36
//创建图片
37
Graphics graphics = Graphics.FromImage(img);
38
//指定要绘制的面积
39
graphics.DrawImage(img, 0, 0, img.Width, img.Height);
40
//定义字段和画笔
41
Font font = new Font("黑体", 16);
42
Brush brush = new SolidBrush(Color.Yellow);
43
graphics.DrawString(str, font, brush, x, y);
44
//保存并输出图片
45
img.Save(pageCurrent.MapPath(savePath), System.Drawing.Imaging.ImageFormat.Jpeg);
46
return savePath;
47
48
}
49
#endregion
50
Image自动缩小#region Image自动缩小
51
/**//// <summary>
52
/// 缩小图片到指定的大小
53
/// </summary>
54
/// <param name="strOldPic">
55
/// 原图片的位置
56
/// </param>
57
/// <param name="strNewPic">
58
/// 缩小后的图片位置
59
/// </param>
60
/// <param name="intWidth">
61
/// 宽度
62
/// </param>
63
/// <param name="intHeight">
64
/// 高度
65
/// </param>
66
public void SmallPic(string strOldPic, string strNewPic, int intWidth, int intHeight)
67
{
68
69
System.Drawing.Bitmap objPic, objNewPic;
70
try
71
{
72
objPic = new System.Drawing.Bitmap(strOldPic);
73
objNewPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight);
74
objNewPic.Save(strNewPic);
75
76
}
77
catch (Exception exp)
{ throw exp; }
78
finally
79
{
80
objPic = null;
81
objNewPic = null;
82
}
83
}
84
85
public void SmallPic(string strOldPic, string strNewPic, int intWidth)
86
{
87
88
System.Drawing.Bitmap objPic, objNewPic;
89
try
90
{
91
objPic = new System.Drawing.Bitmap(strOldPic);
92
int intHeight = Convert.ToInt32(((intWidth * 1.0) / (objPic.Width * 1.0)) * objPic.Height);
93
objNewPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight);
94
objNewPic.Save(strNewPic, objPic.RawFormat);
95
96
}
97
catch (Exception exp)
{ throw exp; }
98
finally
99
{
100
objPic = null;
101
objNewPic = null;
102
}
103
}
104
105
//public void SmallPic(string strOldPic, string strNewPic, int intHeight)
106
//{
107
108
// System.Drawing.Bitmap objPic, objNewPic;
109
// try
110
// {
111
// objPic = new System.Drawing.Bitmap(strOldPic);
112
// int intWidth = Convert.ToInt32(((intHeight * 1.0) / objPic.Height) * objPic.Width);
113
// objNewPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight);
114
// objNewPic.Save(strNewPic, objPic.RawFormat);
115
116
// }
117
// catch (Exception exp) { throw exp; }
118
// finally
119
// {
120
// objPic = null;
121
// objNewPic = null;
122
// }
123
//}
124
#endregion
125
126
}
127
}
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Web;
5
using System.Web.Security;
6
using System.Web.UI;
7
using System.Web.UI.WebControls;
8
using System.Web.UI.WebControls.WebParts;
9
using System.Web.UI.HtmlControls;
10
using System.Drawing;
11
namespace WebHelper
12
{
13
14
/**//// <summary>
15
/// ImageHelper 的摘要说明
16
/// </summary>
17
public class ImageHelper
18
{
19
public ImageHelper()
20
{
21
//
22
// TODO: 在此处添加构造函数逻辑
23
//
24
}
25
Image水印#region Image水印
26
/**//// <summary>
27
/// 写入图像水印
28
/// </summary>
29
/// <param name="str">水印字符串</param>
30
/// <param name="filePath">原图片位置</param>
31
/// <param name="savePath">水印加入后的位置</param>
32
/// <returns></returns>
33
public string CreateBackImage(System.Web.UI.Page pageCurrent, string str, string filePath, string savePath, int x, int y)
34
{
35
System.Drawing.Image img = System.Drawing.Image.FromFile(pageCurrent.MapPath(filePath));
36
//创建图片
37
Graphics graphics = Graphics.FromImage(img);
38
//指定要绘制的面积
39
graphics.DrawImage(img, 0, 0, img.Width, img.Height);
40
//定义字段和画笔
41
Font font = new Font("黑体", 16);
42
Brush brush = new SolidBrush(Color.Yellow);
43
graphics.DrawString(str, font, brush, x, y);
44
//保存并输出图片
45
img.Save(pageCurrent.MapPath(savePath), System.Drawing.Imaging.ImageFormat.Jpeg);
46
return savePath;
47
48
}
49
#endregion
50
Image自动缩小#region Image自动缩小
51
/**//// <summary>
52
/// 缩小图片到指定的大小
53
/// </summary>
54
/// <param name="strOldPic">
55
/// 原图片的位置
56
/// </param>
57
/// <param name="strNewPic">
58
/// 缩小后的图片位置
59
/// </param>
60
/// <param name="intWidth">
61
/// 宽度
62
/// </param>
63
/// <param name="intHeight">
64
/// 高度
65
/// </param>
66
public void SmallPic(string strOldPic, string strNewPic, int intWidth, int intHeight)
67
{
68
69
System.Drawing.Bitmap objPic, objNewPic;
70
try
71
{
72
objPic = new System.Drawing.Bitmap(strOldPic);
73
objNewPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight);
74
objNewPic.Save(strNewPic);
75
76
}
77
catch (Exception exp)
{ throw exp; }
78
finally
79
{
80
objPic = null;
81
objNewPic = null;
82
}
83
}
84
85
public void SmallPic(string strOldPic, string strNewPic, int intWidth)
86
{
87
88
System.Drawing.Bitmap objPic, objNewPic;
89
try
90
{
91
objPic = new System.Drawing.Bitmap(strOldPic);
92
int intHeight = Convert.ToInt32(((intWidth * 1.0) / (objPic.Width * 1.0)) * objPic.Height);
93
objNewPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight);
94
objNewPic.Save(strNewPic, objPic.RawFormat);
95
96
}
97
catch (Exception exp)
{ throw exp; }
98
finally
99
{
100
objPic = null;
101
objNewPic = null;
102
}
103
}
104
105
//public void SmallPic(string strOldPic, string strNewPic, int intHeight)
106
//{
107
108
// System.Drawing.Bitmap objPic, objNewPic;
109
// try
110
// {
111
// objPic = new System.Drawing.Bitmap(strOldPic);
112
// int intWidth = Convert.ToInt32(((intHeight * 1.0) / objPic.Height) * objPic.Width);
113
// objNewPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight);
114
// objNewPic.Save(strNewPic, objPic.RawFormat);
115
116
// }
117
// catch (Exception exp) { throw exp; }
118
// finally
119
// {
120
// objPic = null;
121
// objNewPic = null;
122
// }
123
//}
124
#endregion
125
126
}
127
}
相关文章推荐
- 封装的一些实现图片水印与图片自动结合缩放的类
- 如何让图片按比例响应式缩放并自动裁剪的css技巧(同时也适用于一些轮播父容器响应式缩放)
- Android 实现图片的自动缩放,适应分辨率不同的手机
- ios学习--结合UIImageView实现图片的移动和缩放(转)
- Qt5:随窗口大小变化背景图片自动缩放的实现
- 有关图片随表格自动缩放的实现
- 结合UIImageView实现图片的移动和缩放
- 利用ASPUPLOAD,ASPJPEG实现图片上传自动生成缩略图及加上水印
- js控制图片自动缩放,实现铺满盒子,不变形,完全局中
- 结合UIImageView实现图片的移动和缩放
- PHP实现给图片自动打水印
- php图片水印添加,压缩,剪切的封装类实现
- Android 实现图片的自动缩放,适应分辨率不同的手机
- jquery实现根据浏览器窗口大小自动缩放图片的方法
- 黄聪:C#图片处理封装类(裁剪、缩放、清晰度、加水印、生成缩略图)有示例(转)
- php封装成类(GD库实现文字、图片水印、压缩图)
- Qt5:随窗口大小变化背景图片自动缩放的实现【转】
- 在asp中实现自动缩放图片(推荐)
- 结合UIImageView实现图片的移动和缩放
- Android 实现图片自动缩放自定义控件