• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Asp.net上传图片同时生成缩略图和水印图后台代码

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

  /**/
    /// <summary>
    /// 在图片上生成图片水印
    /// </summary>
    /// <param name="Path">原服务器图片路径</param>
    /// <param name="Path_syp">生成的带图片水印的图片路径</param>
    /// <param name="Path_sypf">水印图片路径</param>

代码
1 protected void AddWaterPic(string Path, string Path_syp, string Path_sypf)
2 {
3 System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
4 System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Path_sypf);
5 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
6 g.DrawImage(copyImage, new System.Drawing.Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel);
7 g.Dispose();
8
9 image.Save(Path_syp);
10 image.Dispose();
11 }
12
13 protected void Button2_Click(object sender, EventArgs e)
14 {
15 //自动保存远程图片
16   WebClient client = new WebClient();
17 //备用Reg:<img.*?src=([\"\'])(http:\/\/.+\.(jpg|gif|bmp|bnp))\1.*?>
18   Regex reg = new Regex("IMG[^>]*?src\\s*=\\s*(?:\"(?<1>[^\"]*)\"|'(?<1>[^\']*)')", RegexOptions.IgnoreCase);
19   MatchCollection m = reg.Matches(TextBox1.Text);
20
21 foreach (Match math in m)
22 {
23 string imgUrl = math.Groups[1].Value;
24
25 //在原图片名称前加YYMMDD重名名并上传
26  
27 Regex regName = new Regex(@"\w+.(?:jpg|gif|bmp|png)", RegexOptions.IgnoreCase);
28
29 string strNewImgName = DateTime.Now.ToShortDateString().Replace("-", "") + regName.Match(imgUrl).ToString();
30
31 try
32 {
33 //保存图片
34 //client.DownloadFile(imgUrl, Server.MapPath("../ImageUpload/Auto/" + strNewImgName));
35  
36 }
37 catch
38 {
39 }
40 finally
41 {
42
43 }
44
45 client.Dispose();
46 }
47
48 Response.Write("<script>alert('远程图片保存成功,保存路径为ImageUpload/auto')</script>");
49 }
50 }
51
52

   /// <summary>
    /// 生成缩略图
    /// </summary>
    /// <param name="originalImagePath">源图路径(物理路径)</param>
    /// <param name="thumbnailPath">缩略图路径(物理路径)</param>
    /// <param name="width">缩略图宽度</param>
    /// <param name="height">缩略图高度</param>
    /// <param name="mode">生成缩略图的方式</param>   

代码
1 public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
2 {
3 System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
4
5 int towidth = width;
6 int toheight = height;
7
8 int x = 0;
9 int y = 0;
10 int ow = originalImage.Width;
11 int oh = originalImage.Height;
12
13 switch (mode)
14 {
15 case "HW"://指定高宽缩放(可能变形)
16 break;
17 case "W"://指定宽,高按比例
18 toheight = originalImage.Height * width / originalImage.Width;
19 break;
20 case "H"://指定高,宽按比例
21 towidth = originalImage.Width * height / originalImage.Height;
22 break;
23 case "Cut"://指定高宽裁减(不变形)
24 if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
25 {
26 oh = originalImage.Height;
27 ow = originalImage.Height * towidth / toheight;
28 y = 0;
29 x = (originalImage.Width - ow) / 2;
30 }
31 else
32 {
33 ow = originalImage.Width;
34 oh = originalImage.Width * height / towidth;
35 x = 0;
36 y = (originalImage.Height - oh) / 2;
37 }
38 break;
39 default:
40 break;
41 }
42
43 //新建一个bmp图片
44 System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
45
46 //新建一个画板
47 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
48
49 //设置高质量插值法
50 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
51
52 //设置高质量,低速度呈现平滑程度
53 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
54
55 //清空画布并以透明背景色填充
56 g.Clear(System.Drawing.Color.Transparent);
57
58 //在指定位置并且按指定大小绘制原图片的指定部分
59 g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
60 new System.Drawing.Rectangle(x, y, ow, oh),
61 System.Drawing.GraphicsUnit.Pixel);
62
63 try
64 {
65 //以jpg格式保存缩略图
66 bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
67 }
68 catch (System.Exception e)
69 {
70 throw e;
71 }
72 finally
73 {
74 originalImage.Dispose();
75 bitmap.Dispose();
76 g.Dispose();
77 }
78 }
79
80

/**/
    /// <summary>
    /// 在图片上增加文字水印
    /// </summary>
    /// <param name="Path">原服务器图片路径</param>
    /// <param name="Path_sy">生成的带文字水印的图片路径</param>

代码
1 protected void AddWaterText(string Path, string Path_sy)
2 {
3 string addText = "脉动团,Http://www.mzonetuan.com";
4 System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
5 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
6 g.DrawImage(image, 0, 0, image.Width, image.Height);
7 System.Drawing.Font f = new System.Drawing.Font("Verdana", 10); //字体位置为左空10
8 System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Green);
9
10 g.DrawString(addText, f, b, 14, 14); //字体大小为14X14
11 g.Dispose();
12
13 image.Save(Path_sy);
14 image.Dispose();
15 }
16
17

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap