亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > C# > 正文

C#實現給圖片加水印的方法

2020-01-24 01:17:16
字體:
來源:轉載
供稿:網友

本文實例講述了C#實現給圖片加水印的方法。分享給大家供大家參考,具體如下:

using System;using System.Drawing;using System.Drawing.Imaging;using System.Drawing.Drawing2D;namespace Tutorial{  class WaterMark  {    [STAThread]    static void Main(string[] args)    {      //set a working directory      string WorkingDirectory = @"C:/Documents and Settings/administrator.JAZZMINE/My Documents/Projects/Tutorials/WaterMark";      //define a string of text to use as the Copyright message      string Copyright = "Copyright ?2002 - AP Photo/David Zalubowski";      //create a image object containing the photograph to watermark      Image imgPhoto = Image.FromFile(WorkingDirectory + "http://watermark_photo.jpg");      int phWidth = imgPhoto.Width;      int phHeight = imgPhoto.Height;      //create a Bitmap the Size of the original photograph      Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);      bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);      //load the Bitmap into a Graphics object      Graphics grPhoto = Graphics.FromImage(bmPhoto);      //create a image object containing the watermark      Image imgWatermark = new Bitmap(WorkingDirectory + "http://watermark.bmp");      int wmWidth = imgWatermark.Width;      int wmHeight = imgWatermark.Height;      //------------------------------------------------------------      //Step #1 - Insert Copyright message      //------------------------------------------------------------      //Set the rendering quality for this Graphics object      grPhoto.SmoothingMode = SmoothingMode.AntiAlias;      //Draws the photo Image object at original size to the graphics object.      grPhoto.DrawImage(        imgPhoto,                // Photo Image object        new Rectangle(0, 0, phWidth, phHeight), // Rectangle structure        0,                   // x-coordinate of the portion of the source image to draw.        0,                   // y-coordinate of the portion of the source image to draw.        phWidth,                // Width of the portion of the source image to draw.        phHeight,                // Height of the portion of the source image to draw.        GraphicsUnit.Pixel);          // Units of measure      //-------------------------------------------------------      //to maximize the size of the Copyright message we will      //test multiple Font sizes to determine the largest posible      //font we can use for the width of the Photograph      //define an array of point sizes you would like to consider as possiblities      //-------------------------------------------------------      int[] sizes = new int[]{16,14,12,10,8,6,4};      Font crFont = null;      SizeF crSize = new SizeF();      //Loop through the defined sizes checking the length of the Copyright string      //If its length in pixles is less then the image width choose this Font size.      for (int i=0 ;i<7; i++)      {        //set a Font object to Arial (i)pt, Bold        crFont = new Font("arial", sizes[i], FontStyle.Bold);        //Measure the Copyright string in this Font        crSize = grPhoto.MeasureString(Copyright, crFont);        if((ushort)crSize.Width < (ushort)phWidth)          break;      }      //Since all photographs will have varying heights, determine a      //position 5% from the bottom of the image      int yPixlesFromBottom = (int)(phHeight *.05);      //Now that we have a point size use the Copyrights string height      //to determine a y-coordinate to draw the string of the photograph      float yPosFromBottom = ((phHeight - yPixlesFromBottom)-(crSize.Height/2));      //Determine its x-coordinate by calculating the center of the width of the image      float xCenterOfImg = (phWidth/2);      //Define the text layout by setting the text alignment to centered      StringFormat StrFormat = new StringFormat();      StrFormat.Alignment = StringAlignment.Center;      //define a Brush which is semi trasparent black (Alpha set to 153)      SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(153, 0, 0, 0));      //Draw the Copyright string      grPhoto.DrawString(Copyright, //string of text        crFont, //font        semiTransBrush2, //Brush        new PointF(xCenterOfImg+1,yPosFromBottom+1), //Position        StrFormat);      //define a Brush which is semi trasparent white (Alpha set to 153)      SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));      //Draw the Copyright string a second time to create a shadow effect      //Make sure to move this text 1 pixel to the right and down 1 pixel      grPhoto.DrawString(Copyright, //string of text        crFont, //font        semiTransBrush, //Brush        new PointF(xCenterOfImg,yPosFromBottom), //Position        StrFormat); //Text alignment      //------------------------------------------------------------      //Step #2 - Insert Watermark image      //------------------------------------------------------------      //Create a Bitmap based on the previously modified photograph Bitmap      Bitmap bmWatermark = new Bitmap(bmPhoto);      bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);      //Load this Bitmap into a new Graphic Object      Graphics grWatermark = Graphics.FromImage(bmWatermark);      //To achieve a transulcent watermark we will apply (2) color      //manipulations by defineing a ImageAttributes object and      //seting (2) of its properties.      ImageAttributes imageAttributes = new ImageAttributes();      //The first step in manipulating the watermark image is to replace      //the background color with one that is trasparent (Alpha=0, R=0, G=0, B=0)      //to do this we will use a Colormap and use this to define a RemapTable      ColorMap colorMap = new ColorMap();      //My watermark was defined with a background of 100% Green this will      //be the color we search for and replace with transparency      colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);      colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);      ColorMap[] remapTable = {colorMap};      imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);      //The second color manipulation is used to change the opacity of the      //watermark. This is done by applying a 5x5 matrix that contains the      //coordinates for the RGBA space. By setting the 3rd row and 3rd column      //to 0.3f we achive a level of opacity      float[][] colorMatrixElements = {        new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},        new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},        new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},        new float[] {0.0f, 0.0f, 0.0f, 0.3f, 0.0f},        new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}};      ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);      imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default,        ColorAdjustType.Bitmap);      //For this example we will place the watermark in the upper right      //hand corner of the photograph. offset down 10 pixels and to the      //left 10 pixles      int xPosOfWm = ((phWidth - wmWidth)-10);      int yPosOfWm = 10;      grWatermark.DrawImage(imgWatermark,        new Rectangle(xPosOfWm,yPosOfWm,wmWidth,wmHeight), //Set the detination Position        0, // x-coordinate of the portion of the source image to draw.        0, // y-coordinate of the portion of the source image to draw.        wmWidth, // Watermark Width        wmHeight, // Watermark Height        GraphicsUnit.Pixel, // Unit of measurment        imageAttributes); //ImageAttributes Object      //Replace the original photgraphs bitmap with the new Bitmap      imgPhoto = bmWatermark;      grPhoto.Dispose();      grWatermark.Dispose();      //save new image to file system.      imgPhoto.Save(WorkingDirectory + "http://watermark_final.jpg", ImageFormat.Jpeg);      imgPhoto.Dispose();      imgWatermark.Dispose();    }  }}

更多關于C#相關內容感興趣的讀者可查看本站專題:《C#圖片操作技巧匯總》、《C#字符串操作技巧總結》及《C#面向對象程序設計入門教程

希望本文所述對大家C#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
欧美激情视频一区二区| 日韩中文在线中文网三级| 欧美亚洲在线视频| 美女久久久久久久久久久| 亚洲老头老太hd| 成人免费自拍视频| 国产精品高清免费在线观看| 国产精品亚洲片夜色在线| 亚洲影院污污.| 久久久久久久久久国产精品| 国产999在线观看| 亚洲字幕在线观看| 亚洲精品丝袜日韩| 精品呦交小u女在线| 97人人爽人人喊人人模波多| 热门国产精品亚洲第一区在线| 亚洲aⅴ男人的天堂在线观看| 色妞一区二区三区| 久久亚洲国产成人| 欧美大秀在线观看| 欧美刺激性大交免费视频| 亚洲精品白浆高清久久久久久| 欧美日本亚洲视频| 北条麻妃在线一区二区| 日韩国产精品视频| 亚洲午夜久久久久久久| 久久久精品久久久久| 亚洲国产又黄又爽女人高潮的| 久久国内精品一国内精品| 欧美激情亚洲激情| 高清一区二区三区日本久| 国产精品视频网| 亚洲电影免费观看高清完整版在线| 在线视频日本亚洲性| 亚洲综合视频1区| 国产成人综合av| 中文字幕免费精品一区| 亚洲天堂第一页| 91精品啪aⅴ在线观看国产| 欧美在线视频免费播放| 久久久噜久噜久久综合| 亚洲激情视频网| 国产成人精品久久亚洲高清不卡| 午夜免费久久久久| 欧美日韩亚洲网| 欧美日本啪啪无遮挡网站| 国产精品h在线观看| 日韩麻豆第一页| 亚洲成人在线视频播放| 91精品久久久久久综合乱菊| 午夜精品在线视频| 91精品国产高清| 中文字幕亚洲无线码在线一区| 日韩中文字幕久久| 亚洲人成绝费网站色www| 国产精品国产福利国产秒拍| 国模视频一区二区| 中文字幕精品影院| 超碰97人人做人人爱少妇| 日韩av中文字幕在线| 久久精品国产96久久久香蕉| 成人网址在线观看| 92看片淫黄大片看国产片| 久久久精品网站| 国产精品678| 秋霞午夜一区二区| 国产欧美一区二区三区久久人妖| 亚洲香蕉伊综合在人在线视看| 91在线视频成人| 成人h视频在线观看播放| 日本老师69xxx| 2019中文字幕在线免费观看| 亚洲欧美综合区自拍另类| www.国产精品一二区| 亚洲一级一级97网| 欧洲一区二区视频| 国产日本欧美一区二区三区在线| 亚洲欧美激情精品一区二区| 亚洲精品国产综合区久久久久久久| 精品国产欧美一区二区三区成人| 欧美极品少妇全裸体| 国产精品久久av| 久久九九国产精品怡红院| 亚洲韩国青草视频| 性色av一区二区三区免费| 超碰91人人草人人干| 欧美插天视频在线播放| 性欧美视频videos6一9| 国产欧美久久一区二区| 久久亚洲精品一区| 久久av中文字幕| 欧美限制级电影在线观看| 亚洲日韩欧美视频一区| 日韩欧美国产一区二区| 欧美孕妇与黑人孕交| 日韩欧美国产成人| 日韩精品黄色网| 中文字幕不卡在线视频极品| 国产视频久久久久| 视频在线观看一区二区| 成人免费看黄网站| 欧美日韩免费看| 日本一区二区在线播放| 国产成人精品999| 97热在线精品视频在线观看| 国内揄拍国内精品| 国产成人久久久| 久久精品国产欧美亚洲人人爽| 日韩在线视频线视频免费网站| 成人中文字幕+乱码+中文字幕| 欧美专区中文字幕| 亚洲**2019国产| 国模私拍视频一区| 欧美中文在线观看国产| 91免费精品国偷自产在线| 精品国产拍在线观看| 欧美激情videos| 1769国内精品视频在线播放| 国模私拍视频一区| 国产亚洲欧洲高清| 国内精品一区二区三区四区| 国产精品日韩一区| 亚洲欧洲日产国产网站| 欧美精品免费播放| 伊人久久大香线蕉av一区二区| 青青a在线精品免费观看| www.日韩不卡电影av| 亚洲国产精品视频在线观看| 91国语精品自产拍在线观看性色| 2019国产精品自在线拍国产不卡| 国产精品久久久久久久美男| 久久亚洲国产成人| 欧美风情在线观看| 午夜精品久久久久久久99热| 欧美一区二区视频97| 亚洲国产精品推荐| 亚洲精品电影网站| 国产精品色视频| 成人精品一区二区三区电影黑人| 亚洲另类激情图| 欧美激情a∨在线视频播放| 中文字幕在线精品| 欧美日韩成人网| 亚洲性生活视频在线观看| 日韩麻豆第一页| 欧美在线观看视频| 精品日韩视频在线观看| 欧美在线观看日本一区| 日韩欧美视频一区二区三区| 国产亚洲精品久久| 国产精品久久久久久久久久久久| 日日狠狠久久偷偷四色综合免费| 久久综合伊人77777蜜臀| 日韩综合中文字幕| 狠狠做深爱婷婷久久综合一区| 国产99久久久欧美黑人| 国产精品久久久久久久久久久久久| 91精品视频免费观看| 92福利视频午夜1000合集在线观看| 91最新国产视频| 最近的2019中文字幕免费一页| 18一19gay欧美视频网站| 欧美精品精品精品精品免费| …久久精品99久久香蕉国产|