asp.net生成缩略图、文字图片水印

  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         }

asp.net生成缩略图、文字图片水印

时间: 2024-08-13 05:18:20

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

缩略图、图片水印图、文字水印图

原文:缩略图.图片水印图.文字水印图 源代码下载地址:http://www.zuidaima.com/share/1550463651056640.htm 文字水印效果:原图 加文字水印后: 加图片水印后:

asp.net生成缩略图

/// <summary> /// 生成缩略图 /// </summary> /// <param name="orginalImagePat">原图片地址</param> /// <param name="thumNailPath">缩略图地址</param> /// <param name="width">缩略图宽度</param> /// <

ASP.NET生成缩略图的代码

01.        // <summary> 02.        /// 生成缩略图 03.        /// </summary> 04.        /// <param name="originalImagePath">源图路径</param> 05.        /// <param name="thumbnailPath">缩略图路径</param> 06.       

C# 生成缩略图 去除图片旋转角度

图片生成缩略图会有旋转角度 /// <summary> /// 测试JRE图片压缩后图片会旋转问题 /// </summary> public void Uploadimg1() { HttpPostedFile hpf = HttpContext.Current.Request.Files[0]; var context = HttpContext.Current; string newurl = context.Server.MapPath("/upload/img/

ASP.NET图片上传,加水印文字和水印图片!

看了清清月儿的这篇文章让自己受益匪浅,但是觉得还有一些问题.上传图片后还有原来的图片文件存在,觉得这样很不爽,调用file类的delete方法删除原来没有生成水印的图片另外自己又加了一个限制图片大小的函数 1.最简单的单文件上传(没花头) 效果图:说明:这是最基本的文件上传,在asp.net1.x中没有这个FileUpload控件,只有html的上传控件,那时候要把html控件转化为服务器控件,很不好用.其实所有文件上传的美丽效果都是从这个FileUpload控件衍生,第一个例子虽然简单却是根本

ASP组件AspJpeg(加水印)生成缩略图等使用方法

ASP组件AspJpeg(加水印)生成缩略图等使用方法 作者: 字体:[增加 减小] 类型:转载 时间:2012-12-17我要评论 ASPJPEG是一款功能相当强大的图象处理组件,用它可以轻松地做出图片的缩略图和为图片加上水印功能.下面简单介绍一下使用方法,需要的朋友可以了解下 一.为图片添加水印 复制代码 代码如下: <% Dim Jpeg ''''//声明变量 Set Jpeg = Server.CreateObject("Persits.Jpeg") ''''//调用组件

PHP加水印代码 支持文字和图片水印

PHP加图片水印.文字水印类代码,PHP加水印类,支持文字图片水印的透明度设置.水印图片背景透明.自己写的一个类,因为自己开发的一套CMS中要用到,网上的总感觉用着不顺手,希望大家也喜欢这个类,后附有类使用方法. <?php class WaterMask{ public $waterType = 1; //水印类型:0为文字水印.1为图片水印 public $pos = 0; //水印位置 public $transparent = 45; //水印透明度 public $waterStr =

.NET图片操作类,包含图片格式转换、图片缩放、 文字水印、图片水印、路径转换

using System;using System.Collections.Generic;using System.Text;using System.IO;using System.Drawing.Imaging;using System.Drawing;using System.Web;namespace ZC.Utils{  public  static class ImageHelper  { #region 图片格式转换      /// <summary>      /// 图片

PHP.25-TP框架商城应用实例-后台1-添加商品功能、钩子函数、在线编辑器、过滤XSS、上传图片并生成缩略图

添加商品功能 1.创建商品控制器[C] /www.test.com/shop/Admin/Controller/GoodsController.class.php <?php namespace Admin\Controller; use Think\Controller; //后台添加商品功能控制器 class GoodsController extends Controller { //显示和处理表单 public function add() { //判断用户是否提交了表单(如果提交了,就