1        /// <summary>
  2         /// 会产生graphics异常的PixelFormat
  3         /// </summary>
  4         private static PixelFormat[] indexedPixelFormats = { PixelFormat.Undefined, PixelFormat.DontCare,
  5 PixelFormat.Format16bppArgb1555, PixelFormat.Format1bppIndexed, PixelFormat.Format4bppIndexed,
  6 PixelFormat.Format8bppIndexed
  7     };
  8
  9
 10         /// <summary>
 11         /// 判断图片的PixelFormat 是否在 引发异常的 PixelFormat 之中
 12         /// </summary>
 13         /// <param name="imgPixelFormat">原图片的PixelFormat</param>
 14         /// <returns></returns>
 15         private static bool IsPixelFormatIndexed(PixelFormat imgPixelFormat)
 16         {
 17             foreach (PixelFormat pf in indexedPixelFormats)
 18             {
 19                 if (pf.Equals(imgPixelFormat)) return true;
 20             }
 21
 22             return false;
 23         }
 24
 25        /// <summary>
 26         /// 在图片上生成图片水印
 27         /// </summary>
 28         /// <param name="Path">原服务器图片路径</param>
 29         /// <param name="Path_syp">生成的带图片水印的图片路径</param>
 30         /// <param name="Path_sypf">水印图片路径</param>
 31         public static void AddShuiYinPic(string Path, string Path_syp, string Path_sypf)
 32         {
 33             System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
 34             System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Path_sypf);
 35             System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
 36             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);
 37             g.Dispose();
 38
 39             image.Save(Path_syp);
 40             image.Dispose();
 41         }
 42         /// <summary>
 43         /// 在图片上增加文字水印
 44         /// </summary>
 45         /// <param name="Path">原服务器图片路径</param>
 46         /// <param name="Path_sy">生成的带文字水印的图片路径</param>
 47         public static void AddShuiYinWord(string Path, string Path_sy)
 48         {
 49
 50
 51             using (System.Drawing.Image img = System.Drawing.Image.FromFile(Path))
 52             {
 53                 //如果原图片是索引像素格式之列的,则需要转换
 54                 if (IsPixelFormatIndexed(img.PixelFormat))
 55                 {
 56                     Bitmap bmp = new Bitmap(img.Width, img.Height, PixelFormat.Format32bppArgb);
 57                     using (Graphics g = Graphics.FromImage(bmp))
 58                     {
 59                         g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
 60                         g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
 61                         g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
 62                         g.DrawImage(img, 0, 0);
 63
 64                         System.Drawing.Font f = new System.Drawing.Font("Verdana", 16);
 65                         System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
 66                         g.DrawString("爱智旮旯", f, b, 15, 15);
 67
 68
 69                     }
 70                     bmp.Save(Path_sy);
 71                     bmp.Dispose();
 72
 73                 }
 74                 else //否则直接操作
 75                 {
 76                     string addText = "爱智旮旯";
 77                     System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
 78                     System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
 79                     g.DrawImage(image, 0, 0, image.Width, image.Height);
 80                     System.Drawing.Font f = new System.Drawing.Font("Verdana", 16);
 81                     System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Blue);
 82
 83                     g.DrawString(addText, f, b, 15, 15);
 84                     g.Dispose();
 85
 86                     image.Save(Path_sy);
 87                     image.Dispose();
 88                 }
 89             }
 90
 91
 92
 93
 94
 95
 96
 97         }
 98
 99         /// <summary>
100         /// 生成缩略图
101         /// </summary>
102         /// <param name="originalImagePath">源图路径(物理路径)</param>
103         /// <param name="thumbnailPath">缩略图路径(物理路径)</param>
104         /// <param name="width">缩略图宽度</param>
105         /// <param name="height">缩略图高度</param>
106         /// <param name="mode">生成缩略图的方式</param>
107         public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
108         {
109             System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
110
111             int towidth = width;
112             int toheight = height;
113
114             int x = 0;
115             int y = 0;
116             int ow = originalImage.Width;
117             int oh = originalImage.Height;
118
119             switch (mode)
120             {
121                 case "HW"://指定高宽缩放(可能变形)
122                     break;
123                 case "W"://指定宽,高按比例
124                     toheight = originalImage.Height * width / originalImage.Width;
125                     break;
126                 case "H"://指定高,宽按比例
127                     towidth = originalImage.Width * height / originalImage.Height;
128                     break;
129                 case "Cut"://指定高宽裁减(不变形)
130                     if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
131                     {
132                         oh = originalImage.Height;
133                         ow = originalImage.Height * towidth / toheight;
134                         y = 0;
135                         x = (originalImage.Width - ow) / 2;
136                     }
137                     else
138                     {
139                         ow = originalImage.Width;
140                         oh = originalImage.Width * height / towidth;
141                         x = 0;
142                         y = (originalImage.Height - oh) / 2;
143                     }
144                     break;
145                 default:
146                     break;
147             }
148
149             //新建一个bmp图片
150             System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
151
152             //新建一个画板
153             System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
154
155             //设置高质量插值法
156             g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
157
158             //设置高质量,低速度呈现平滑程度
159             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
160
161             //清空画布并以透明背景色填充
162             g.Clear(System.Drawing.Color.Transparent);
163
164             //在指定位置并且按指定大小绘制原图片的指定部分
165             g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
166             new System.Drawing.Rectangle(x, y, ow, oh),
167             System.Drawing.GraphicsUnit.Pixel);
168
169             try
170             {
171                 //以jpg格式保存缩略图
172                 bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
173             }
174             catch (System.Exception e)
175             {
176                 throw e;
177             }
178             finally
179             {
180                 originalImage.Dispose();
181                 bitmap.Dispose();
182                 g.Dispose();
183             }
184         }
185
186       protected void Button1_Click(object sender, EventArgs e)
187         {
188             if (FileUpload1.HasFile)
189             {
190                 string fileContentType = FileUpload1.PostedFile.ContentType;
191                 if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg" || fileContentType == "image/jpg")
192                 {
193                     string name = FileUpload1.PostedFile.FileName; // 客户端文件路径
194
195                     FileInfo file = new FileInfo(name);
196                     string fileName = file.Name; // 文件名称
197                     string fileName_s = "s_" + file.Name; // 缩略图文件名称
198                     string fileName_sy = "sy_" + file.Name; // 水印图文件名称(文字)
199                     string fileName_syp = "syp_" + file.Name; // 水印图文件名称(图片)
200                     //以下路径可以根据情况进行修改
201                     string webFilePath = Server.MapPath("file/" + fileName); // 服务器端文件路径
202                     string webFilePath_s = Server.MapPath("file/" + fileName_s);  // 服务器端缩略图路径
203                     string webFilePath_sy = Server.MapPath("file/" + fileName_sy); // 服务器端带水印图路径(文字)
204                     string webFilePath_syp = Server.MapPath("file/" + fileName_syp); // 服务器端带水印图路径(图片)
205                     //以下为水印图片的路径一定要先准备一张水印图片!
206                     string webFilePath_sypf = Server.MapPath("file/shuiyin.jpg"); // 服务器端水印图路径(图片)
207
208                     if (!File.Exists(webFilePath))
209                     {
210                         try
211                         {
212                             FileUpload1.SaveAs(webFilePath); // 使用 SaveAs 方法保存文件
213                             AddShuiYinWord(webFilePath, webFilePath_sy);
214                             //AddShuiYinPic(webFilePath, webFilePath_syp, webFilePath_sypf);
215                             MakeThumbnail(webFilePath, webFilePath_s, 130, 130, "Cut"); // 生成缩略图方法
216                             Label1.Text = "提示:文件“" + fileName + "”成功上传,并生成“" + fileName_s + "”缩略图,文件类型为:" + FileUpload1.PostedFile.ContentType + ",文件大小为:" + FileUpload1.PostedFile.ContentLength + "B";
217                         }
218                         catch (Exception ex)
219                         {
220                             Label1.Text = "提示:文件上传失败,失败原因:" + ex.Message;
221                         }
222                     }
223                     else
224                     {
225                         Label1.Text = "提示:文件已经存在,请重命名后上传";
226                     }
227                 }
228                 else
229                 {
230                     Label1.Text = "提示:文件类型不符";
231                 }
232
233             }
234         }

View Code

转载于:https://www.cnblogs.com/keyyang/p/3808473.html

asp.net生成缩略图、文字图片水印相关推荐

  1. php 缩略图增加水印,PHP生成缩略图加图片水印代码

    一个PHP图片加水印, 生成缩略图类,觉得注释的挺清楚,与大家分享,如果想为你的PHP程序添加图片水印或者生成缩略图的话,应该是个不错的选择. PHP生成缩略图加图片水印代码<?php // 原 ...

  2. php 缩略图增加水印,php 图片上传代码(具有生成缩略图与增加水印功能)

    class upfile { public $filepath = "www.111com.net/"; //上传文件存放文件夹 public $filesize = 100000 ...

  3. php分析图片水印,PHP开发的文字水印,缩略图,图片水印实现类与用法示例

    本文实例讲述了PHP开发的文字水印,缩略图,图片水印实现类与用法.分享给大家供大家参考,具体如下: 1.实现类ImageToTest.class.php参考代码 class ImageToTest { ...

  4. 转载:asp.net生成缩略图通用函数(支持多种生成方式)

    文章转载自书生的Blog,原地址是http://www.cnblogs.com/pbwf/archive/2006/03/31/363981.html 感觉十分好用,比较自己写的更全面也更仔细,就放弃 ...

  5. ASP.NET生成缩略图类C#代码

    using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Dra ...

  6. Asp.net生成缩略图

    当我们要上传图片的时候,往往需要生成缩略图,以往我们要使用第三方控件才能完成.在asp.net中用下面方法轻松搞定: <script language="VB" runat= ...

  7. PHP添加文字图片水印

    需要注意几点: 1.确保GD库的开启(增加图片水印只确保这一点即可) 2.确保msyh.ttf文件存在并引入 增加文字水印: /*打开图片*///1.配置图片路径 $src = "1.jpg ...

  8. ffmpeg指定位置添加文字图片水印

    FFmpeg命令详解_Android&Java&C-CSDN博客 ​​​​​​利用ffmpeg实现添加图片水印和文字水印,添加多个水印.代码和命令实现及中文水印乱码_浪漫老狼的博客-C ...

  9. php gd库 图片水印,php使用GD库实现文字图片水印及缩略图教程

    我们要使用gd库就必须先打开gd库,具体如下 Windows下开启PHP的GD库支持 找到php.ini,打开内容,找到: ;extension=php_gd2.dll 把最前面的分号";& ...

最新文章

  1. js判断display隐藏显示
  2. 8.0 C++远征:对象数组
  3. 利用js对页面数据进行排序
  4. C语言中文件的基本操作函数fprintf和fscanf实例介绍
  5. codeforces 234E Champions' League
  6. 【转】C#、面向对象、设计模式学习
  7. 航海王启航服务器维护要多久,《航海王启航》7月1日服务器更新维护公告
  8. 移动边缘计算 入门笔记(一)
  9. 计算机基础考试函数公式,计算机基础-excel公式考和函数.ppt
  10. 通信原理 AMI码和HDB3码的编码方式
  11. 论文公式居中、编号右对齐
  12. Pygame实战之外星人入侵NO.5——操作子弹
  13. 学习逆向某风控id分析
  14. Bash shell学习笔记(五)
  15. trove 镜像制作
  16. 前端js通过图片路径,展示图片
  17. MATLAB函数大全 .
  18. 开源软件新时代:55个经典开源Windows工具
  19. 双碳丨碳库、碳平衡、温室气体、碳循环等多领域监测与模拟
  20. CA-SSH-DHCP服务实现

热门文章

  1. matlab快速将几幅图片放在一幅图片
  2. python网页内容获取记录pkg
  3. 动手学深度学习Pytorch Task03
  4. 机器学习算法-详细白板推导系列视频
  5. 某大型银行深化系统之十八:性能设计之三
  6. 使用Android OpenGL ES 2.0绘图之二:定义形状
  7. 设计自己的基于Selenium 的自动化测试框架-Java版(1) - 为什么selenium还需要测试框架?...
  8. 基于人脸识别的课堂签到管理系统(四)---摄像头上传实时数据,百度AI读取并返回信息以及多线程操作
  9. proxy_cache的使用
  10. Windows 系统环境变量大全