CKFinder的水印控件的问题

由于项目需要,想在CKFinder上传图片时打上水印,于是放狗一顿狂搜,发现CKSource官方就提供这样一个插件:watermark,正宗的行货水印功能!迫不及待地下载下来安装到plugins目录中,配置config.ascx,随便选了一个logo.gif,打开项目,运行,上传图片,哦耶!搞定!漂亮的半透明水印效果就被加到了图片的右下角!

 

本来书说到这儿也该大圆满结局了,不过天有不测风云,项目有不测变化,BOSS来了一句“水印要放在中间” ,问题跟着来了!watermark这个插件只支持设置水印距离左、下边缘的偏移量,愣是没有设置为中间的选项!无语了,郁闷了,杯具了,浮云了........最后实在木有办法只好到CKSource的官方网站上找答案,发现官网提供的文档还算全面,于是决定自己写个插件,创新咱不会,照官网的葫芦画个瓢应该还是没问题的。折腾了半天,终于搞定了,由于时间紧迫,功能比较单一,就是在中间打个水印,只能在中间,没有选项,而且透明度是50%,都是写死的,暂且凑和用了。

 

代码如下,仅供参考:

 

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.IO;
  6 using System.Drawing;
  7 using System.Drawing.Drawing2D;
  8 using System.Drawing.Imaging;
  9 using System.Web;
 10 using System.Web.UI;
 11 using CKFinder;
 12 
 13 namespace CKFinder.Plugins
 14 {
 15     public class RangeonMark : CKFinder.CKFinderPlugin
 16     {
 17         public string JavascriptPlugins
 18         {
 19             get { return ""; }
 20         }
 21 
 22         public void Init(CKFinder.Connector.CKFinderEvent CKFinderEvent)
 23         {
 24             CKFinderEvent.AfterFileUpload +=
 25                   new CKFinder.Connector.CKFinderEvent.Hook(this.RangeonMarkAfterFileUploadCommandEventHandler);
 26         }
 27 
 28         protected void RangeonMarkAfterFileUploadCommandEventHandler(object sender, CKFinder.Connector.CKFinderEventArgs e)
 29         {
 30             if (!File.Exists((string)e.data[1]))
 31             {
 32                 return;
 33             }
 34             try
 35             {
 36                 this.CreateWatermark((string)e.data[1]);
 37             }
 38             catch
 39             {
 40             }
 41         }
 42 
 43         public void CreateWatermark(string sourceFile)
 44         {
 45             MemoryStream ms = new MemoryStream();
 46             Image img = Image.FromFile(sourceFile);
 47             img.Save(ms, img.RawFormat);
 48             byte[] data = ms.GetBuffer();
 49 
 50             ms.Dispose();
 51             img.Dispose();
 52 
 53             CreateWatermark(sourceFile, data);
 54         }
 55 
 56         public void CreateWatermark(string sourceFile, byte[] imgData)
 57         {
 58             string logo = "logo.gif";
 59             string logo_file = logo;
 60             if (!File.Exists(logo_file))
 61             {
 62                 if (File.Exists(HttpContext.Current.Server.MapPath(logo_file)))
 63                 {
 64                     logo_file = HttpContext.Current.Server.MapPath(logo_file);
 65                 }
 66                 else
 67                 {
 68                     logo_file = HttpContext.Current.Server.MapPath(((Page)HttpContext.Current.Handler).ResolveUrl("~/ckfinder/plugins/rangeonmark/" + logo));
 69                     if (!File.Exists(logo_file))
 70                     {
 71                         logo_file = HttpContext.Current.Server.MapPath(((Page)HttpContext.Current.Handler).ResolveUrl("ckfinder/plugins/rangeonmark/" + logo));
 72                         if (!File.Exists(logo_file))
 73                         {
 74                             return;
 75                         }
 76                     }
 77                 }
 78             }
 79 
 80             MemoryStream ms = new MemoryStream(imgData);
 81             Image imgSource = Image.FromStream(ms);
 82             Bitmap bmpOutput = new Bitmap(imgSource.Width, imgSource.Height);
 83             Bitmap bmpLogo = new Bitmap(logo_file);
 84             Graphics g = Graphics.FromImage(bmpOutput);
 85             Bitmap bmpPixel = this.CreateNonIndexedImage(bmpLogo);
 86 
 87             if (bmpLogo.Width > imgSource.Width || bmpLogo.Height > imgSource.Height)
 88             {
 89                 bmpPixel = this.resizeImage(bmpLogo, new Size((bmpLogo.Width > imgSource.Width) ? imgSource.Width : bmpLogo.Width, (bmpLogo.Height > imgSource.Height) ? imgSource.Height : bmpLogo.Height));
 90             }
 91             Bitmap bmpResized = bmpPixel;
 92 
 93             for (int i = 0; i < bmpResized.Width; i++)
 94             {
 95                 for (int j = 0; j < bmpResized.Height; j++)
 96                 {
 97                     Color pixel = bmpResized.GetPixel(i, j);
 98                     if (pixel.A != 0)
 99                     {
100                         bmpPixel.SetPixel(i, j, Color.FromArgb(128, pixel)); // alpha=255 : solid
101                     }
102                 }
103             }
104 
105             int IntX = (imgSource.Width - bmpPixel.Width) / 2;
106             int IntY = (imgSource.Height - bmpPixel.Height) / 2;
107 
108             g.InterpolationMode = InterpolationMode.HighQualityBicubic;
109             ImageFormat rawFormat = imgSource.RawFormat;
110             g.DrawImage(imgSource, 00, imgSource.Width, imgSource.Height);
111             g.DrawImage(bmpPixel, IntX, IntY);
112             imgSource.Dispose();
113             bmpLogo.Dispose();
114             bmpPixel.Dispose();
115             g.Dispose();
116             bmpOutput.Save(sourceFile, rawFormat);
117             imgSource.Dispose();
118 
119         }
120 
121         private Bitmap resizeImage(Image imgToResize, Size size)
122         {
123             int width = imgToResize.Width;
124             int height = imgToResize.Height;
125             float num = (float)size.Width / (float)width;
126             float num2 = (float)size.Height / (float)height;
127             float num3 = (num2 < num) ? num2 : num;
128             int width2 = (int)((float)width * num3);
129             int height2 = (int)((float)height * num3);
130             Bitmap bitmap = new Bitmap(width2, height2);
131             Graphics graphics = Graphics.FromImage(bitmap);
132             graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
133             graphics.DrawImage(imgToResize, 00, width2, height2);
134             graphics.Dispose();
135             return bitmap;
136         }
137 
138         private Bitmap CreateNonIndexedImage(Bitmap src)
139         {
140             Bitmap bitmap = new Bitmap(src.Width, src.Height, PixelFormat.Format32bppArgb);
141             using (Graphics graphics = Graphics.FromImage(bitmap))
142             {
143                 graphics.DrawImage(src, 00);
144             }
145             return bitmap;
146         }
147     }
148 }

 

 水平有限,代码写的比较糙,让各位牛人见笑了。

 

posted @ 2011-06-20 23:09  润之  阅读(2055)  评论(5编辑  收藏  举报