图片上传 并生成水印

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5
  6 namespace MySystemManager.Utility
  7 {
  8   public   class ImageTextAndPicter
  9     {
 10         /// <summary>
 11         /// 生成缩略图
 12         /// </summary>
 13         /// <param name="originalImagePath">源图路径(物理路径)</param>
 14         /// <param name="thumbnailPath">缩略图路径(物理路径)</param>
 15         /// <param name="width">缩略图宽度</param>
 16         /// <param name="height">缩略图高度</param>
 17         /// <param name="mode">生成缩略图的方式</param>
 18         public  void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
 19         {
 20             System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
 21
 22             int towidth = width;
 23             int toheight = height;
 24
 25             int x = 0;
 26             int y = 0;
 27             int ow = originalImage.Width;
 28             int oh = originalImage.Height;
 29
 30             switch (mode)
 31             {
 32                 case "HW"://指定高宽缩放(可能变形)
 33                     break;
 34                 case "W"://指定宽,高按比例
 35                     toheight = originalImage.Height * width / originalImage.Width;
 36                     break;
 37                 case "H"://指定高,宽按比例
 38                     towidth = originalImage.Width * height / originalImage.Height;
 39                     break;
 40                 case "Cut"://指定高宽裁减(不变形)
 41                     if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
 42                     {
 43                         oh = originalImage.Height;
 44                         ow = originalImage.Height * towidth / toheight;
 45                         y = 0;
 46                         x = (originalImage.Width - ow) / 2;
 47                     }
 48                     else
 49                     {
 50                         ow = originalImage.Width;
 51                         oh = originalImage.Width * height / towidth;
 52                         x = 0;
 53                         y = (originalImage.Height - oh) / 2;
 54                     }
 55                     break;
 56                 default:
 57                     break;
 58             }
 59
 60             //新建一个bmp图片
 61             System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
 62
 63             //新建一个画板
 64             System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
 65
 66             //设置高质量插值法
 67             g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
 68
 69             //设置高质量,低速度呈现平滑程度
 70             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
 71
 72             //清空画布并以透明背景色填充
 73             g.Clear(System.Drawing.Color.Transparent);
 74
 75             //在指定位置并且按指定大小绘制原图片的指定部分
 76             g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
 77                 new System.Drawing.Rectangle(x, y, ow, oh),
 78                 System.Drawing.GraphicsUnit.Pixel);
 79
 80             try
 81             {
 82                 //以jpg格式保存缩略图
 83                 bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
 84             }
 85             catch (System.Exception e)
 86             {
 87                 throw e;
 88             }
 89             finally
 90             {
 91                 originalImage.Dispose();
 92                 bitmap.Dispose();
 93                 g.Dispose();
 94             }
 95         }
 96         /// <summary>
 97         /// 在图片上增加文字水印
 98         /// </summary>
 99         /// <param name="Path">原服务器图片路径</param>
100         /// <param name="Path_sy">生成的带文字水印的图片路径</param>
101         /// <param name="addText">文字水印内容</param>
102         public void AddWater(string Path, string Path_sy, string addText)
103         {
104
105             System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
106             System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
107             g.DrawImage(image, 0, 0, image.Width, image.Height);
108             System.Drawing.Font f = new System.Drawing.Font("Verdana", 20);//字体大小
109             System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Green);
110             //位置定位
111             g.DrawString(addText, f, b, 15, 15);
112             g.Dispose();
113
114             image.Save(Path_sy);
115             image.Dispose();
116         }
117         /// <summary>
118         /// 在图片上生成图片水印
119         /// </summary>
120         /// <param name="Path">原服务器图片路径</param>
121         /// <param name="Path_syp">生成的带图片水印的图片路径</param>
122         /// <param name="Path_sypf">水印图片路径</param>
123         public void AddWaterPic(string Path, string Path_syp, string Path_sypf)
124         {
125             System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
126             System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Path_sypf);
127             System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
128             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);
129             g.Dispose();
130
131             image.Save(Path_syp);
132             image.Dispose();
133         }
134
135     }
136 }
时间: 2024-08-01 02:09:56

图片上传 并生成水印的相关文章

[原创]超强C#图片上传,加水印,自动生成缩略图源代码

<%@ Page Language=“C#“ AutoEventWireup=“true“ %> <%@ Import Namespace=“System“ %> <%@ Import Namespace=“System.IO“ %> <%@ Import Namespace=“System.Net“ %> <%@ Import NameSpace=“System.Web“ %> <%@ Import NameSpace=“Legalsof

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

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

数往知来 JQuery 图片上传_水印 &lt;二十二&gt;

一.图片上传  :enctype='multipart/form-date 首先在进行文件上传时,添加form表单中的enctype属性指定enctype='multipart/form-date', 文件数据发送给服务端,并不是把文件路径发送给服务端了,它会随机生成一个分割字符串, 把每一个表单元素分割开 <form method='post' action='' enctype='multipart/form-date'> //这里的enctype是提交请求报文的报文体的一种编码格式, 默

spring mvc 图片上传,图片压缩、跨域解决、 按天生成目录 ,删除,限制为图片代码等相关配置

spring mvc 图片上传,跨域解决 按天生成目录 ,删除,限制为图片代码,等相关配置 fs.root=data/ #fs.root=/home/dev/fs/ #fs.root=D:/fs/ #fs.domains=182=http://172.16.100.182:18080,localhost=http://localhost:8080 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE be

spring mvc 图片上传,图片压缩、跨域解决、 按天生成文件夹 ,删除,限制为图片代码等相关配置

spring mvc 图片上传,跨域解决 按天生成文件夹 ,删除,限制为图片代码,等相关配置 fs.root=data/ #fs.root=/home/dev/fs/ #fs.root=D:/fs/ #fs.domains=182=http://172.16.100.182:18080,localhost=http://localhost:8080 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE b

图片上传根据stream生成image

对于图片上传代码的整合 因为需要判断上传的图片的宽高是否符合尺寸,所以在最初拿到inputstream的时候,就直接获取image格式的图片 本来是想在下面的checkFile中获取的,不过直接使用System.Drawing.Image.FromStream(request.PostedFile.InputStream);的时候报错了,错误意思是说参数为空,应该是由于上面获取byte[]file的时候,关闭了stream,导致request.PostFile.InputStream也没有了,所

ASP.NET 图片上传工具类 upload image简单好用功能齐全

使用方法: UploadImage ui = new UploadImage(); //可选参数 //ui.SetWordWater = "哈哈";//文字水印 ui.SetPicWater = Server.MapPath("2.png");//图片水印(图片和文字都赋值图片有效) ui.SetPositionWater = 4;//水印图片的位置 0居中.1左上角.2右上角.3左下角.4右下角 ui.SetSmallImgHeight = "110,4

thinkphp实现UploadFile.class.php图片上传功能

图片上传在网站里是很常用的功能.ThinkPHP里也有自带的图片上传类(UploadFile.class.php) 和图片模型类(Image.class.php).方便于我们去实现图片上传功能,下面是实现方法 1.我们首先需要创建一个表 复制代码代码如下: CREATE TABLE IF NOT EXISTS `tp_image` ( `id` int(11) NOT NULL AUTO_INCREMENT, `image` varchar(200) NOT NULL, `create_time

几乎考虑到了每个细节的php图片上传

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <?php /****************************************************************************** 参数说明: $max_file_size  : 上传文件大小限制, 单位BYTE $destination_folder : 上传文件路径