/// <summary> ///add a picture with a watermark effect at the specified position in a picture/// </summary> /// <param name= "Sourceimage" >Specify the absolute path to the source picture</param> /// <param name= "Watermarkimage" >Specify the absolute path to the watermark picture</param> /// <param name= "SaveImage" >absolute path to save picture</param> Public Static voidMakewatermark (stringSourceimage,stringWatermarkimage,stringsaveimage) { //create an object to manipulate the source picture that needs to be watermarkedImage Imgphoto =Image.FromFile (sourceimage); //Gets the width and height of the source picture intPhwidth =Imgphoto.width; intPhheight =Imgphoto.height; //create a blank image in BMP format (width and height consistent with the source picture)Bitmap Bmphoto =NewBitmap (Phwidth, Phheight, Pixelformat.format24bpprgb); //set the resolution of the new blank BMP picturebmphoto.setresolution (imgphoto.horizontalresolution, imgphoto.verticalresolution); //set the BMP picture as a drawing objectGraphics Grphoto =graphics.fromimage (Bmphoto); //set the quality of the generated pictureGrphoto.smoothingmode =Smoothingmode.antialias; //load the source picture into the new BMP picturegrphoto.drawimage (Imgphoto,//Photo Image Object NewRectangle (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//Image object that creates a watermark pictureImage Imgwatermark =NewBitmap (watermarkimage); //get the width and height of a watermark picture intWmwidth =Imgwatermark.width; intWmheight =Imgwatermark.height; //------------------------------------------------------------ //First step: Insert watermark Picture//------------------------------------------------------------ //Create a Bitmap based on the previously modified photograph BitmapBitmap Bmwatermark =NewBitmap (Bmphoto); Bmwatermark.setresolution (Imgphoto.horizontalresolution, imgphoto.verticalresolution); //Load This Bitmap into a new Graphic ObjectGraphics Grwatermark =graphics.fromimage (Bmwatermark); //To achieve a transulcent watermark we'll apply (2) Color//manipulations by defineing a ImageAttributes object and//seting (2) of its properties.ImageAttributes ImageAttributes =NewImageAttributes (); //The first step in manipulating the watermark image was to replace//the background color with one which is trasparent (alpha=0, r=0, g=0, b=0)//To does this we'll use a Colormap and use this to define a remaptableColorMap ColorMap =NewColorMap (); //My watermark is defined with a background of 100% Green this would//Is the color we search for and replace with transparencyColormap.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 do 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=NewColorMatrix (colormatrixelements); Imageattributes.setcolormatrix (Wmcolormatrix, Colormatrixflag.default, Coloradjusttype.bitmap); //For this example we 'll place the watermark in the upper right//hand corner of the photograph. Offset down, pixels and to the//Left ten pixles intXPOSOFWM = ((phwidth-wmwidth)-Ten); intYPOSOFWM =Ten; Grwatermark.drawimage (Imgwatermark,NewRectangle (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 WidthWmheight,//Watermark HeightGraphicsUnit.Pixel,//Unit of MeasurmentImageAttributes);//ImageAttributes Object//Replace the original photgraphs bitmap with the new bitmapImgphoto.dispose (); Imgphoto=Bmwatermark; Grphoto.dispose (); Grwatermark.dispose (); Bmphoto.dispose (); //------------------------------------------------------------ //Step Three: Save the picture//------------------------------------------------------------Imgphoto.save (SaveImage, imageformat.jpeg); //releasing resources in useImgphoto.dispose (); Imgwatermark.dispose (); Bmwatermark.dispose (); }
C # watermark Transparency picture