c# 添加图片水印,可以指定水印位置+生成缩略图[付上帅图1,2,3,4]
早上,一哥兒發(fā)來添加圖片水印的資料。有三個(gè)信息,如下:
????????????
xx 09:57:35
http://index.cnblogs.com/archive/2004/10/20/54498.aspx
王二狗 09:57:51
好的,我看看
?
xx 09:58:12
http://www.iyuanma.com/info/18/17026_200592663244.htm
xx 10:07:00
http://www.codefans.com/CodeView/CodeView_12043.html
xx 10:07:18
你看看現(xiàn)成的組件能不能用
????????
???????? 幾分鐘后,我就發(fā)現(xiàn)第二個(gè)網(wǎng)址的內(nèi)容是copy第一個(gè)的(也許是相反),真是天下文章一大抄。于是我對(duì)那條說有什么組件的東東很感興趣,下下來一看,吐血,就是一段代碼,完全copy第一個(gè)文章里的,什么組件啊。真是能欺騙人。
???????? 算了,自己動(dòng)手,豐衣十足。想起上個(gè)月做相冊(cè)的開發(fā),用到了Gallery開源項(xiàng)目的東西。那里面有填加水印的,并且功能比較強(qiáng)大,能設(shè)定位置。不像上面的資料不能調(diào)整水印位置,水印效果估計(jì)也不好,畢竟就那幾行。其實(shí)后來我發(fā)現(xiàn)那段代碼還是錯(cuò)的,調(diào)試通過不了,修改后才能用,至于錯(cuò)在那里在后面介紹。
???????? 我們先看看哥兒給我的資料里的代碼:
?????????? 原來的代碼:
?1 private void Btn_Upload_Click(object sender, System.EventArgs e)
?2??????? {
?3??????????? if(UploadFile.PostedFile.FileName.Trim()!="")
?4??????????? {
?5??????????????? //上傳文件
?6??????????????? string extension = Path.GetExtension(UploadFile.PostedFile.FileName).ToUpper();
?7??????????????? string fileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
?8??????????????? string path = Server.MapPath(".") + "/UploadFile/" + fileName + extension;
?9??????????????? UploadFile.PostedFile.SaveAs(path);
10
11??????????????? //加文字水印,注意,這里的代碼和以下加圖片水印的代碼不能共存
12??????????????? System.Drawing.Image image = System.Drawing.Image.FromFile(path);
13??????????????? Graphics g = Graphics.FromImage(image);
14??????????????? g.DrawImage(image, 0, 0, image.Width, image.Height);
15??????????????? Font f = new Font("Verdana", 32);
16??????????????? Brush b = new SolidBrush(Color.White);
17??????????????? string addText = AddText.Value.Trim();
18??????????????? g.DrawString(addText, f, b, 10, 10);
19??????????????? g.Dispose();
20
21??????????????? //加圖片水印
22??????????????? System.Drawing.Image image = System.Drawing.Image.FromFile(path);
23??????????????? System.Drawing.Image copyImage = System.Drawing.Image.FromFile( Server.MapPath(".") + "/Alex.gif");
24??????????????? Graphics g = Graphics.FromImage(image);
25??????????????? g.DrawImage(copyImage, new Rectangle(image.Width-copyImage.Width, image.Height-copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
26??????????????? g.Dispose();
27
28??????????????? //保存加水印過后的圖片,刪除原始圖片
29??????????????? string newPath = Server.MapPath(".") + "/UploadFile/" + fileName + "_new" + extension;
30??????????????? image.Save(newPath);
31??????????????? image.Dispose();
32??????????????? if(File.Exists(path))
33??????????????? {
34??????????????????? File.Delete(path);
35??????????????? }
36
37??????????????? Response.Redirect(newPath);
38??????????? }
39??????? }
???????? 于是我把Gallery里的代碼整理了下。如下:
????????? 圖片上傳函數(shù),進(jìn)行判斷是否加水印,做出兩種處理方式:
?1??? /// <summary>
?2??????? /// 上傳圖片代碼
?3??????? /// </summary>
?4??????? /// <param name="image_file">HtmlInputFile控件</param>
?5??????? /// <param name="ImgPath">存放的文件夾絕對(duì)位置</param>
?6??????? /// <param name="ImgLink">生成的圖片的名稱帶后綴</param>
?7??????? /// <returns></returns>
?8??????? public bool UpPic(System.Web.UI.HtmlControls.HtmlInputFile image_file,string ImgPath,string ImgLink )
?9??????? {
10??????????? if(image_file.PostedFile.FileName!=null && image_file.PostedFile.FileName.Trim()!="")
11??????????? {
12??????????????? try
13??????????????? {
14??????????????????? if( !System.IO.Directory.Exists(ImgPath))
15??????????????????? {
16??????????????????????? System.IO.Directory.CreateDirectory( ImgPath);
17??????????????????? }
18???????????????????? //生成縮略圖
??????????????????????????? this.GreateMiniImage((ImgPath+ "\\"+"old_"+ImgLink),(ImgPath+ "\\"+"mini_"+ImgLink),50,50);
19??????????????????? //如果顯示水印
20??????????????????? if(ShowWatermark)
21??????????????????? {
22??????????????????????? image_file.PostedFile.SaveAs(ImgPath+ "\\" +"old_"+ImgLink);
23??????????????????????? //加水印
24??????????????????????? this.addWaterMark((ImgPath+ "\\"+"old_"+ImgLink),(ImgPath+ "\\"+ImgLink));
25
26
27???????????????????
28??????????????????? }
29??????????????????? else
30??????????????????? {
31??????????????????????? image_file.PostedFile.SaveAs(ImgPath+ "\\" +ImgLink);
32???????????????????
33???????????????????
34??????????????????? }
35??????????????????? return true;
36??????????????? }
37
38??????????????? catch
39??????????????? {
40??????????????????? return false;
41??????????????? }
42??????????? }
43??????????? else
44???????????
45??????????? {
46??????????????? return false;
47??????????? }
48
49??????? }
?? 加水印的函數(shù)如下:
?? 填加圖片函數(shù),需要下面兩個(gè)函數(shù)的支持,當(dāng)然也可以寫到一起,不過那看起來就很冗長了。
?1??? /// <summary>
?2??????????? /// 添加圖片水印
?3??????????? /// </summary>
?4??????????? /// <param name="oldpath">原圖片絕對(duì)地址</param>
?5??????????? /// <param name="newpath">新圖片放置的絕對(duì)地址</param>
?6??????? private void addWaterMark(string oldpath,string newpath)
?7???????
?8??????? {
?9??????????? try
10??????????? {
11
12??????????????? System.Drawing.Image image = System.Drawing.Image.FromFile(oldpath);
13
14??????????????? Bitmap b = new Bitmap(image.Width, image.Height,PixelFormat.Format24bppRgb);
15??????????????? Graphics g = Graphics.FromImage(b);
16??????????????? g.Clear(Color.White);
17??????????????? g.SmoothingMode = SmoothingMode.HighQuality;
18??????????????? g.InterpolationMode = InterpolationMode.High;
19???????????
20??????????????? g.DrawImage(image, 0, 0, image.Width, image.Height);
21
22??????????????? if(如果需要填加水印)
23??????????????? {
24??????????????????? switch(水印類型)
25??????????????????? {
26??? //是圖片的話??????????????
?????????????????????????????????? case "WM_IMAGE":
27??????????????????????????? this.addWatermarkImage( g,Page.Server.MapPath(Watermarkimgpath),WatermarkPosition,image.Width,image.Height);
28??????????????????????????? break;
29??? //如果是文字???????????????????
?????????????????????????????????? case "WM_TEXT":
30??????????????????????????? this.addWatermarkText( g, WatermarkText,WatermarkPosition
31??????????????????????????????? ,image.Width,image.Height);
32??????????????????????????? break;
33??????????????????? }
34
35??????????????????? b.Save(newpath);
36??????????????????? b.Dispose();
37??????????????????? image.Dispose();
38??????????????? }
39
40??????????? }
41??????????? catch
42??????????? {
43???????????
44??????????????? if(File.Exists(oldpath))
45??????????????? {
46??????????????????? File.Delete(oldpath);
47??????????????? }
48??????????? }
49??????????? finally
50??????????? {
51???????????????
52??????????????? if(File.Exists(oldpath))
53??????????????? {
54??????????????????? File.Delete(oldpath);
55??????????????? }
56???????????
57???????????
58??????????? }
59???????
60???????
61
62
63
64???????
65??????? }
??
?1/// <summary>
?2??????? ///? 加水印文字
?3??????? /// </summary>
?4??????? /// <param name="picture">imge 對(duì)象</param>
?5??????? /// <param name="_watermarkText">水印文字內(nèi)容</param>
?6??????? /// <param name="_watermarkPosition">水印位置</param>
?7??????? /// <param name="_width">被加水印圖片的寬</param>
?8??????? /// <param name="_height">被加水印圖片的高</param>
?9??????? private void addWatermarkText( Graphics picture,string _watermarkText,string _watermarkPosition,int _width,int _height)
10??????? {
11??????????? int[] sizes = new int[]{16,14,12,10,8,6,4};
12??????????? Font crFont = null;
13??????????? SizeF crSize = new??? SizeF();
14??????????? for (int i=0 ;i<7; i++)
15??????????? {
16??????????????? crFont = new Font("arial", sizes[i], FontStyle.Bold);
17??????????????? crSize = picture.MeasureString(_watermarkText, crFont);
18
19??????????????? if((ushort)crSize.Width < (ushort)_width)
20??????????????????? break;
21??????????? }
22
23??????????? float xpos = 0;
24??????????? float ypos = 0;
25
26??????????? switch(_watermarkPosition)
27??????????? {
28??????????????? case "WM_TOP_LEFT":
29??????????????????? xpos = ((float)_width * (float).01) + (crSize.Width / 2);
30??????????????????? ypos = (float)_height * (float).01;
31??????????????????? break;
32??????????????? case "WM_TOP_RIGHT":
33??????????????????? xpos = ((float)_width * (float).99) - (crSize.Width / 2);
34??????????????????? ypos = (float)_height * (float).01;
35??????????????????? break;
36??????????????? case "WM_BOTTOM_RIGHT":
37??????????????????? xpos = ((float)_width * (float).99) - (crSize.Width / 2);
38??????????????????? ypos = ((float)_height * (float).99) - crSize.Height;
39??????????????????? break;
40??????????????? case "WM_BOTTOM_LEFT":
41??????????????????? xpos = ((float)_width * (float).01) + (crSize.Width / 2);
42??????????????????? ypos = ((float)_height * (float).99) - crSize.Height;
43??????????????????? break;
44??????????? }
45
46??????????? StringFormat StrFormat = new StringFormat();
47??????????? StrFormat.Alignment = StringAlignment.Center;
48
49??????????? SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(153, 0, 0,0));
50??????????? picture.DrawString(_watermarkText, crFont, semiTransBrush2, xpos+1, ypos+1, StrFormat);
51
52??????????? SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));
53??????????? picture.DrawString(_watermarkText, crFont, semiTransBrush, xpos, ypos, StrFormat);
54
55
56??????????? semiTransBrush2.Dispose();
57??????????? semiTransBrush.Dispose();
58
59
60
61??????? }
//代碼已經(jīng)修改,可以按比率還填加圖片水印,不過如果背景圖片和水印圖片太不成比率的話(指水印圖片要大于背景圖片的1/4),出來的效果不是很好。
? 1? /// <summary>
? 2??????? ///? 加水印圖片
? 3??????? /// </summary>
? 4??????? /// <param name="picture">imge 對(duì)象</param>
? 5??????? /// <param name="WaterMarkPicPath">水印圖片的地址</param>
? 6??????? /// <param name="_watermarkPosition">水印位置</param>
? 7??????? /// <param name="_width">被加水印圖片的寬</param>
? 8??????? /// <param name="_height">被加水印圖片的高</param>
? 9??????? private void addWatermarkImage( Graphics picture,string WaterMarkPicPath,string _watermarkPosition,int _width,int _height)
?10??????? {
?11??????????? Image watermark = new Bitmap(WaterMarkPicPath);
?12
?13??????????? ImageAttributes imageAttributes = new ImageAttributes();
?14??????????? ColorMap colorMap = new ColorMap();
?15
?16??????????? colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
?17??????????? colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
?18??????????? ColorMap[] remapTable = {colorMap};
?19
?20??????????? imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
?21
?22??????????? float[][] colorMatrixElements = {
?23??????????????????????????????????????????????? new float[] {1.0f,? 0.0f,? 0.0f,? 0.0f, 0.0f},
?24??????????????????????????????????????????????? new float[] {0.0f,? 1.0f,? 0.0f,? 0.0f, 0.0f},
?25??????????????????????????????????????????????? new float[] {0.0f,? 0.0f,? 1.0f,? 0.0f, 0.0f},
?26??????????????????????????????????????????????? new float[] {0.0f,? 0.0f,? 0.0f,? 0.3f, 0.0f},
?27??????????????????????????????????????????????? new float[] {0.0f,? 0.0f,? 0.0f,? 0.0f, 1.0f}
?28??????????????????????????????????????????? };
?29
?30??????????? ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
?31
?32??????????? imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
?33
?34??????????? int xpos = 0;
?35??????????? int ypos = 0;
?36??????????? int WatermarkWidth=0;
?37??????????? int WatermarkHeight=0;
?38??????????? double bl=1d;
?39??????????? //計(jì)算水印圖片的比率
?40??????????? //取背景的1/4寬度來比較
?41??????????? if((_width>watermark.Width*4)&&(_height>watermark.Height*4))
?42??????????? {
?43??????????????? bl=1;
?44??????????? }
?45??????????? else if((_width>watermark.Width*4)&&(_height<watermark.Height*4))
?46??????????? {
?47??????????????? bl=Convert.ToDouble(_height/4)/Convert.ToDouble(watermark.Height);
?48???????????
?49??????????? }else
?50???????????
?51??????????? if((_width<watermark.Width*4)&&(_height>watermark.Height*4))
?52??????????? {
?53??????????????? bl=Convert.ToDouble(_width/4)/Convert.ToDouble(watermark.Width);
?54??????????? }
?55??????????? else
?56??????????? {
?57??????????????? if((_width*watermark.Height)>(_height*watermark.Width))
?58??????????????? {
?59??????????????????? bl=Convert.ToDouble(_height/4)/Convert.ToDouble(watermark.Height);
?60???????????????????
?61??????????????? }
?62??????????????? else
?63??????????????? {
?64??????????????????? bl=Convert.ToDouble(_width/4)/Convert.ToDouble(watermark.Width);
?65???????????????????
?66??????????????? }
?67???????????
?68??????????? }
?69
?70??????????? WatermarkWidth=Convert.ToInt32(watermark.Width*bl);
?71??????????? WatermarkHeight=Convert.ToInt32(watermark.Height*bl);
?72
?73???????????
?74
?75??????????? switch(_watermarkPosition)
?76??????????? {
?77??????????????? case "WM_TOP_LEFT":
?78??????????????????? xpos = 10;
?79??????????????????? ypos = 10;
?80??????????????????? break;
?81??????????????? case "WM_TOP_RIGHT":
?82??????????????????? xpos = _width - WatermarkWidth - 10;
?83??????????????????? ypos = 10;
?84??????????????????? break;
?85??????????????? case "WM_BOTTOM_RIGHT":
?86??????????????????? xpos = _width - WatermarkWidth - 10;
?87??????????????????? ypos = _height -WatermarkHeight - 10;
?88??????????????????? break;
?89??????????????? case "WM_BOTTOM_LEFT":
?90??????????????????? xpos = 10;
?91??????????????????? ypos = _height - WatermarkHeight - 10;
?92??????????????????? break;
?93??????????? }
?94
?95??????????? picture.DrawImage(watermark, new Rectangle(xpos, ypos, WatermarkWidth, WatermarkHeight), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes);
?96
?97
?98??????????? watermark.Dispose();
?99??????????? imageAttributes.Dispose();
100??????? }
?? 生成縮略圖函數(shù)
?1
?2??????? /// <summary>
?3??????? /// 生成縮略圖
?4??????? /// </summary>
?5??????? /// <param name="oldpath">原圖片地址</param>
?6??????? /// <param name="newpath">新圖片地址</param>
?7??????? /// <param name="tWidth">縮略圖的寬</param>
?8??????? /// <param name="tHeight">縮略圖的高</param>
?9??????? private void? GreateMiniImage(string oldpath,string newpath,int tWidth, int tHeight)
10??????? {
11???????
12??????????? try
13??????????? {
14
15??????????????? System.Drawing.Image image = System.Drawing.Image.FromFile(oldpath);
16??????????????? double bl=1d;
17??????????????? if((image.Width<=image.Height)&&(tWidth>=tHeight))
18??????????????? {
19??????????????????? bl=Convert.ToDouble(image.Height)/Convert.ToDouble(tHeight);
20??????????????? }
21??????????????? else if((image.Width>image.Height)&&(tWidth<tHeight))
22??????????????? {
23??????????????????? bl=Convert.ToDouble(image.Width)/Convert.ToDouble(tWidth);
24???????????
25??????????????? }
26??????????????? else
27???????????
28??????????????????? if((image.Width<=image.Height)&&(tWidth<=tHeight))
29??????????????? {
30??????????????????? if(image.Height/tHeight>=image.Width/tWidth)
31??????????????????? {
32??????????????????????? bl=Convert.ToDouble(image.Width)/Convert.ToDouble(tWidth);
33???????????????????
34??????????????????? }
35??????????????????? else
36??????????????????? {
37??????????????????????? bl=Convert.ToDouble(image.Height)/Convert.ToDouble(tHeight);
38??????????????????? }
39??????????????? }
40??????????????? else
41??????????????? {
42??????????????????? if(image.Height/tHeight>=image.Width/tWidth)
43??????????????????? {
44??????????????????????? bl=Convert.ToDouble(image.Height)/Convert.ToDouble(tHeight);
45???????????????????
46??????????????????? }
47??????????????????? else
48??????????????????? {
49??????????????????????? bl=Convert.ToDouble(image.Width)/Convert.ToDouble(tWidth);
50???????????????????
51??????????????????? }
52???????????
53??????????????? }
54
55???????????
56??????????????? Bitmap b = new Bitmap(image ,Convert.ToInt32(image.Width/bl), Convert.ToInt32(image.Height/bl));
57
58??????????????? b.Save(newpath);
59??????????????? b.Dispose();
60??????????????? image.Dispose();
61???????????????
62
63??????????? }
64??????????? catch
65??????????? {
66???????????
67???????????????
68??????????? }
69???????????
70??????? }
??? 如果你能耐著心讀到這里,你可以分辨一下,這兩個(gè)加水印的函數(shù)和網(wǎng)上別人的代碼有什么不同了。你也可以發(fā)現(xiàn)為什么網(wǎng)上的代碼不能運(yùn)行通過了。你只要?jiǎng)酉滦∈?#xff0c;調(diào)試下就知道原因了。
???? 最后做得效果很好,附上帥圖1,2,3
? 帶圖片水印的。帶文字水印
?? 你看看效果不錯(cuò)吧,這些水印都是設(shè)為放在右下角的。至于帶圖片的那張?jiān)趺次恢貌幌裨谟蚁陆?#xff0c;是因?yàn)楸尘皥D片太小,水印圖片太大的原因。我只是隨便做了下測試。新的效果圖已經(jīng)放上。
????? 如果你也是像我這樣菜鳥的話,可能對(duì)你有點(diǎn)用處。大俠就不用看了。我寫出來,其實(shí)是覺得網(wǎng)上連一些基礎(chǔ)的代碼都寫的不好,還抄來抄去,更嚴(yán)重的是還是錯(cuò)誤的。
???? 最新帥圖:
???? 縮略圖:
????由于原圖太大上傳不上來,只得把對(duì)比圖發(fā)上來。
轉(zhuǎn)載于:https://www.cnblogs.com/cheatlove/articles/437867.html
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的c# 添加图片水印,可以指定水印位置+生成缩略图[付上帅图1,2,3,4]的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 开发健壮的企业级应用的研究
- 下一篇: 鹰的重生