C#实现给图片加水印的方法
程序员文章站
2023-09-07 19:31:26
本文实例讲述了c#实现给图片加水印的方法。分享给大家供大家参考,具体如下:
using system;
using system.drawing;
using...
本文实例讲述了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 + "\\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 + "\\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 + "\\watermark_final.jpg", imageformat.jpeg); imgphoto.dispose(); imgwatermark.dispose(); } } }
更多关于c#相关内容感兴趣的读者可查看本站专题:《c#图片操作技巧汇总》、《c#字符串操作技巧总结》及《c#面向对象程序设计入门教程》
希望本文所述对大家c#程序设计有所帮助。
上一篇: C#中Arraylist的sort函数用法实例分析
下一篇: 简单学习C#中的泛型方法使用