<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[Heck's  Blog]]></title> 
<link>https://www.heckjj.com/index.php</link> 
<description><![CDATA[一瞬间的决定，往往可以改变很多，事实上，让自己成功的往往不是知识，是精神！ 如果你总是为自己找借口，那只好让成功推迟。执行力，今天！]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[Heck's  Blog]]></copyright>
<item>
<link>https://www.heckjj.com/csharp-asp-net-pic-upload-mark/</link>
<title><![CDATA[c# asp.net上传图片并且加上水印]]></title> 
<author>Heck &lt;@hecks.tk&gt;</author>
<category><![CDATA[Web开发]]></category>
<pubDate>Fri, 10 Sep 2010 07:26:16 +0000</pubDate> 
<guid>https://www.heckjj.com/csharp-asp-net-pic-upload-mark/</guid> 
<description>
<![CDATA[ 
	<textarea name="code" class="c#" rows="15" cols="100">
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;

using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Reflection;

/// <summary>
/// FileUpload 的摘要说明
/// </summary>
public class FileUpload
&#123;
&nbsp;&nbsp;private string BasePath;

&nbsp;&nbsp;public FileUpload()
&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;//
&nbsp;&nbsp;&nbsp;&nbsp;// TODO: 在此处添加构造函数逻辑
&nbsp;&nbsp;&nbsp;&nbsp;//
&nbsp;&nbsp;&#125;

&nbsp;&nbsp;/// <summary> 
&nbsp;&nbsp;/// 给图片上水印 
&nbsp;&nbsp;/// </summary> 
&nbsp;&nbsp;/// <param name="filePath">原图片地址</param> 
&nbsp;&nbsp;/// <param name="waterFile">水印图片地址</param> 
&nbsp;&nbsp;public void MarkWater(string filePath, string waterFile)
&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;//GIF不水印 
&nbsp;&nbsp;&nbsp;&nbsp;int i = filePath.LastIndexOf(".");
&nbsp;&nbsp;&nbsp;&nbsp;string ex = filePath.Substring(i, filePath.Length - i);
&nbsp;&nbsp;&nbsp;&nbsp;if (string.Compare(ex, ".gif", true) == 0)
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;

&nbsp;&nbsp;&nbsp;&nbsp;string ModifyImagePath = BasePath + filePath;//修改的图像路径 
&nbsp;&nbsp;&nbsp;&nbsp;int lucencyPercent = 25;
&nbsp;&nbsp;&nbsp;&nbsp;Image modifyImage = null;
&nbsp;&nbsp;&nbsp;&nbsp;Image drawedImage = null;
&nbsp;&nbsp;&nbsp;&nbsp;Graphics g = null;
&nbsp;&nbsp;&nbsp;&nbsp;try
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//建立图形对象 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;modifyImage = Image.FromFile(ModifyImagePath, true);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;drawedImage = Image.FromFile(BasePath + waterFile, true);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g = Graphics.FromImage(modifyImage);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//获取要绘制图形坐标 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int x = modifyImage.Width - drawedImage.Width;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int y = modifyImage.Height - drawedImage.Height;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//设置颜色矩阵 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;float[][] matrixItems =&#123; 
new float[] &#123;1, 0, 0, 0, 0&#125;, 
new float[] &#123;0, 1, 0, 0, 0&#125;, 
new float[] &#123;0, 0, 1, 0, 0&#125;, 
new float[] &#123;0, 0, 0, (float)lucencyPercent/100f, 0&#125;, 
new float[] &#123;0, 0, 0, 0, 1&#125;&#125;;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ColorMatrix colorMatrix = new ColorMatrix(matrixItems);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ImageAttributes imgAttr = new ImageAttributes();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imgAttr.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//绘制阴影图像 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g.DrawImage(drawedImage, new Rectangle(x, y, drawedImage.Width, drawedImage.Height), 10, 10, drawedImage.Width, drawedImage.Height, GraphicsUnit.Pixel, imgAttr);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//保存文件 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string[] allowImageType =&#123; ".jpg", ".gif", ".png", ".bmp", ".tiff", ".wmf", ".ico" &#125;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FileInfo fi = new FileInfo(ModifyImagePath);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ImageFormat imageType = ImageFormat.Gif;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;switch (fi.Extension.ToLower())
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case ".jpg":
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imageType = ImageFormat.Jpeg;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case ".gif":
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imageType = ImageFormat.Gif;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case ".png":
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imageType = ImageFormat.Png;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case ".bmp":
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imageType = ImageFormat.Bmp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case ".tif":
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imageType = ImageFormat.Tiff;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case ".wmf":
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imageType = ImageFormat.Wmf;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case ".ico":
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imageType = ImageFormat.Icon;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;default:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MemoryStream ms = new MemoryStream();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;modifyImage.Save(ms, imageType);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;byte[] imgData = ms.ToArray();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;modifyImage.Dispose();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;drawedImage.Dispose();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g.Dispose();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FileStream fs = null;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File.Delete(ModifyImagePath);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fs = new FileStream(ModifyImagePath, FileMode.Create, FileAccess.Write);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (fs != null)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fs.Write(imgData, 0, imgData.Length);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fs.Close();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;finally
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;drawedImage.Dispose();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;modifyImage.Dispose();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g.Dispose();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch &#123; ;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&#125;
&#125;
</textarea><br/>Tags - <a href="https://www.heckjj.com/tags/c%2523/" rel="tag">c#</a> , <a href="https://www.heckjj.com/tags/asp.net/" rel="tag">asp.net</a> , <a href="https://www.heckjj.com/tags/%25E5%259B%25BE%25E7%2589%2587/" rel="tag">图片</a> , <a href="https://www.heckjj.com/tags/%25E6%25B0%25B4%25E5%258D%25B0/" rel="tag">水印</a> , <a href="https://www.heckjj.com/tags/%25E4%25B8%258A%25E4%25BC%25A0/" rel="tag">上传</a>
]]>
</description>
</item><item>
<link>https://www.heckjj.com/csharp-asp-net-pic-upload-mark/#blogcomment</link>
<title><![CDATA[[评论] c# asp.net上传图片并且加上水印]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://www.heckjj.com/csharp-asp-net-pic-upload-mark/#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>