C # watermark A picture to set transparency

Source: Internet
Author: User

C # to the picture watermark, you can set the transparency, set the location of the watermark can see the previous article ha

/// <summary>    ///Creating a watermarked photograph with GDI + for. NET/// </summary>    /// <param name= "Rsrcimgpath" >Physical path of the original picture</param>    /// <param name= "Rmarkimgpath" >the physical path of the watermark picture</param>    /// <param name= "Rmarktext" >Watermark text (does not display watermark text set to empty string)</param>    /// <param name= "Rdstimgpath" >the physical path to the output of the synthesized picture</param>     Public voidBuildwatermark (stringRsrcimgpath,stringRmarkimgpath,stringRmarktext,stringRdstimgpath) {     //the following (code) creates an image object from a specified file, and then defines the variable for its Width and height. //these lengths will be used to create a bitmap object that uses the format of the bits per pixel as the color data. Image Imgphoto =Image.FromFile (Rsrcimgpath); intPhwidth =Imgphoto.width; intPhheight =Imgphoto.height; Bitmap Bmphoto=NewBitmap (Phwidth, Phheight, Pixelformat.format24bpprgb); Bmphoto.setresolution ( the, the); Graphics Grphoto=graphics.fromimage (Bmphoto); //This code is loaded into the watermark image, the watermark image has been saved as a BMP file, with green (a=0,r=0,g=255,b=0) as the background color. //once again, a variable is defined for its width and height. Image Imgwatermark =NewBitmap (Rmarkimgpath); intWmwidth =Imgwatermark.width; intWmheight =Imgwatermark.height; //This code draws imgphoto to the (x=0,y=0) position of the Graphics object at 100% of its original size. //all subsequent drawings will take place at the top of the original photo. Grphoto.smoothingmode =Smoothingmode.antialias; Grphoto.drawimage (Imgphoto,NewRectangle (0,0, Phwidth, phheight),0,          0, Phwidth, Phheight, GraphicsUnit.Pixel); //to maximize the size of the copyright information, we will test 7 different font sizes to determine the maximum possible size we can use for our photo widths. //to do this effectively, we will define an integer array and then traverse these integer values to measure the different size of the copyright string. //Once we determine the maximum possible size, we exit the loop and draw the text    int[] sizes =New int[] { -, -, A,Ten,8,6,4 }; Font Crfont=NULL; SizeF crsize=NewSizeF ();  for(inti =0; I <7; i++) {Crfont=NewFont ("Arial", Sizes[i], fontstyle.bold); Crsize=grphoto.measurestring (Rmarktext, Crfont); if((ushort) Crsize.width < (ushort) phwidth) Break; }     //because all the photos have a variety of heights, it is decided to start at the bottom of the image at the beginning of the 5% position. //Use the height of the Rmarktext string to determine the appropriate y-axis for the drawing string. //The x-axis is determined by the center of the computed image, then a StringFormat object is defined, and the stringalignment is set to center.     intYpixlesfrombottom = (int) (Phheight *. to); floatYposfrombottom = ((Phheight-ypixlesfrombottom)-(Crsize.height/2)); floatXcenterofimg = (Phwidth/2); StringFormat Strformat=NewStringFormat (); Strformat.alignment=Stringalignment.center; //now we have all the coordinates we need to create a solidbrush with a color of 60% black (alpha value 153). //draw a copyright string at the right 1 pixels from the right and 1 pixels at the bottom. //This deviation will be used to create a shadow effect.    Use the brush to repeat a process that draws the same text at the top of the previous drawn text. SolidBrush SEMITRANSBRUSH2 =NewSolidBrush (Color.FromArgb (153,0,0,0)); Grphoto.drawstring (Rmarktext, Crfont, SemiTransBrush2,NewPointF (xcenterofimg +1, Yposfrombottom +1), Strformat); SolidBrush Semitransbrush=NewSolidBrush (Color.FromArgb (153,255,255,255)); Grphoto.drawstring (Rmarktext, Crfont, Semitransbrush,NewPointF (xcenterofimg, Yposfrombottom), Strformat); //Create a bitmap based on the previously modified photo.    Load this bitmap into a new graphic object. Bitmap Bmwatermark =NewBitmap (Bmphoto);     Bmwatermark.setresolution (Imgphoto.horizontalresolution, imgphoto.verticalresolution); Graphics Grwatermark=graphics.fromimage (Bmwatermark); //by defining a ImageAttributes object and setting its two properties, we are implementing two colors to achieve a translucent watermark effect. //the first step in handling the watermark image is to turn the background pattern transparent (alpha=0, r=0, g=0, b=0).        We use a colormap and define a remaptable to do this. //as shown earlier, my watermark is defined as 100% green background, we will search for this color and replace it with transparency. ImageAttributes ImageAttributes =NewImageAttributes (); ColorMap ColorMap=NewColorMap (); Colormap.oldcolor= Color.FromArgb (255,0,255,0); Colormap.newcolor= Color.FromArgb (0,0,0,0); Colormap[] Remaptable={COLORMAP}; //the second color processing is used to change the opacity of the watermark. //do this by applying a 5x5 matrix that contains the RGBA space that provides the coordinates. //by setting the third row and the third column 0.3f we have reached an opaque level.    The result is that the watermark appears slightly below the image. imageattributes.setremaptable (remaptable, Coloradjusttype.bitmap); 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); //with two color processing added to the ImageAttributes object, we can now draw a watermark on the right hand side of the photo. //we will deviate from the 10 pixel to the bottom, 10 pixels to the left.     intMarkwidth; intMarkheight; //mark is wider than the original figure.    if(Phwidth <=wmwidth) {Markwidth= Phwidth-Ten; Markheight= (Markwidth * wmheight)/Wmwidth; }     Else if(Phheight <=wmheight) {Markheight= Phheight-Ten; Markwidth= (Markheight * wmwidth)/Wmheight; }     Else{markwidth=Wmwidth; Markheight=Wmheight; }     intXPOSOFWM = ((phwidth-markwidth)-Ten); intYPOSOFWM =Ten; Grwatermark.drawimage (Imgwatermark,NewRectangle (XPOSOFWM, YPOSOFWM, Markwidth, markheight),0,          0, Wmwidth, Wmheight, GraphicsUnit.Pixel, ImageAttributes); //The final step will be to replace the original image with the new bitmap.    Destroys two graphic objects and then saves the image to the file system. Imgphoto =Bmwatermark;     Grphoto.dispose ();     Grwatermark.dispose ();     Imgphoto.save (Rdstimgpath, imageformat.jpeg);     Imgphoto.dispose (); Imgwatermark.dispose (); }

C # watermark A picture to set transparency

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.