图片显示时加水印(不改变原图片)
[轉(zhuǎn)]:http://adyhpq.blog.163.com/blog/
剛剛寫了一個(gè)在顯示圖片是加水印的程序(不改變?cè)瓐D片)的程序,寫出來(lái)和大家分享一下,也許有的人已經(jīng)早就會(huì)了
原理很簡(jiǎn)單,就是攔截HTTP請(qǐng)求,寫一個(gè)HttpHandler,首先我在web.config里面配置了圖片的根路徑:
<appSettings>
??? <add key="monitorPath" value="pictures"/>
?</appSettings>
這個(gè)pictures是放所有圖片的地方,包括下面的文件夾,從這里顯示的圖片都要加上水印.
然后寫一個(gè)類,繼承HttpHandler,這個(gè)類將自動(dòng)放在AppCode下面,如下:
public class ShuiyinHandler:IHttpHandler
{
??? public bool IsReusable
??? {
??????? get { return true; }
??? }
??? public void ProcessRequest(HttpContext context)
??? {
??????? try
??????? {
??????????? //得到請(qǐng)求路徑
??????????? string url = context.Request.Url.AbsoluteUri.ToLower();
??????????? string monitorPath = ConfigurationManager.AppSettings["monitorPath"];
??????????? //是否包含圖片路徑
??????????? bool IsInterestUrl = url.Contains(monitorPath);
??????????? System.Drawing.Image imgSource = null;
???????????
????????????? //判斷原圖片是否存在
??????????? string physicalPath = context.Request.PhysicalPath;
??????????? if (!System.IO.File.Exists(physicalPath))
??????????? {
??????????????? context.Response.Write("圖片不存在");
??????????????? return;
??????????? }
??????????? //如果不是要加水印的文件或文件夾,就原樣輸出
??????????? if (!IsInterestUrl)
??????????? {
??????????????? imgSource = System.Drawing.Image.FromFile(physicalPath);
??????????????? imgSource.Save(context.Response.OutputStream, imgSource.RawFormat);
??????????????? imgSource.Dispose();
??????????????? return;
??????????? }
??????????? imgSource = System.Drawing.Image.FromFile(physicalPath);
??????????? //判斷是否是索引圖像格式
??????????? if (imgSource.PixelFormat == PixelFormat.Format1bppIndexed || imgSource.PixelFormat == PixelFormat.Format4bppIndexed || imgSource.PixelFormat == PixelFormat.Format8bppIndexed)
??????????? {
??????????????? //轉(zhuǎn)成位圖,這步很重要
??????????????? Bitmap bitmap = new Bitmap(imgSource.Width, imgSource.Height);
??????????????? System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(bitmap);
??????????????? System.Drawing.Font font = new System.Drawing.Font("Arial Black", 30.0f, System.Drawing.FontStyle.Bold);
??????????????? //將原圖畫在位圖上
??????????????? graphic.DrawImage(imgSource, new Point(0, 0));
??????????????? //將水印加在位圖上
??????????????? graphic.DrawString("www.dgxyt.com", font, System.Drawing.Brushes.Red, new System.Drawing.PointF());
??????????????????? //將位圖輸入到流
??????????????? bitmap.Save(context.Response.OutputStream, ImageFormat.Jpeg);
??????????????? graphic.Dispose();
??????????????? imgSource.Dispose();
??????????????? bitmap.Dispose();
??????????? }
??????????? else
??????????? {
??????????????? //不是索引圖像格式,直接在上面加上水印
??????????????? System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(imgSource);
??????????????? System.Drawing.Font font = new System.Drawing.Font("Arial Black", 30.0f, System.Drawing.FontStyle.Bold);
??????????????? graphic.DrawString("www.dgxyt.com", font, System.Drawing.Brushes.Red, new System.Drawing.PointF());
??????????????? imgSource.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
??????????????? imgSource.Dispose();
??????????????? graphic.Dispose();
??????????? }
??????????? //標(biāo)明類型為jpg:如果不標(biāo)注,IE沒(méi)有問(wèn)題,但Firefox會(huì)出現(xiàn)亂碼
??????????? context.Response.ContentType = "image/jpeg";
??????????? context.Response.Flush();
??????????? context.Response.End();
??????? }
??????? catch
??????? {
??????????? throw;
??????? }
??? }
}
?
這樣HttpHandler寫好了,在web.config中中的HttpHandler節(jié)點(diǎn)下,加下如下代碼:
<httpHandlers>
????? <add verb="*" path="*.jpg,*.bmp,*.ico,*.jpeg" type="ShuiyinHandler"/>
? </httpHandlers>
?
這表示,jpg,bmp,ico,jpeg都可以加上水印,gif沒(méi)有試過(guò),不過(guò)大家可以試下.
?
寫好之后,放在IIS上,居然不出來(lái)加水印的效果,這讓我郁悶了好久,后來(lái)終于找到原因,原來(lái)是沒(méi)有在IIS上沒(méi)有注冊(cè)以jpg,bmp,ico,jpeg為后綴的文件,IIS默認(rèn)是不處理這些文件的。
?
處理方法:在網(wǎng)站上目錄上右擊,選擇屬性,選擇主目錄,配置,
隨便選擇列表中的一行,點(diǎn)擊編緝,復(fù)制執(zhí)行文件的路徑,然后關(guān)閉,再點(diǎn)擊添加,可執(zhí)行文件用粘貼就可以了,后綴名寫上.jpg,點(diǎn)擊確定。按此方法再添加其他后綴名。
?
這樣就實(shí)現(xiàn)水印效果了。
轉(zhuǎn)載于:https://www.cnblogs.com/sainaxingxing/archive/2008/09/03/1283303.html
總結(jié)
以上是生活随笔為你收集整理的图片显示时加水印(不改变原图片)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 通过编程为ASP.NET页面设置缓存
- 下一篇: 如何做一个优秀的销售代表