C # Add a watermark to the image to set transparency

Source: Internet
Author: User
  1. /// <Summary>
  2. /// Creating a watermarked photograph with GDI + for. net
  3. /// </Summary>
  4. /// <Param name = "rsrcimgpath"> physical path of the original image </param>
  5. /// <Param name = "rmarkimgpath"> physical path of the watermark image </param>
  6. /// <Param name = "rmarktext"> watermark text (do not show the watermark text as a blank string) </param>
  7. /// <Param name = "rdstimgpath"> output the physical path of the merged image. </param>
  8. // @ Finishing: anyrock@mending.cn
  9. Public VoidBuildwatermark (StringRsrcimgpath,StringRmarkimgpath,StringRmarktext,StringRdstimgpath)
  10. {
  11. // The following (CodeCreates an image object from a specified file and defines variables for its width and height.
  12. // These lengths will be used to create a bitmap object that uses the format of 24 bits per pixel as the color data.
  13. Image imgphoto = image. fromfile (rsrcimgpath );
  14. IntPhwidth = imgphoto. width;
  15. IntPhheight = imgphoto. height;
  16. Bitmap BMP hoto =NewBitmap (phwidth, phheight, pixelformat. format24bpprgb );
  17. BMP hoto. setresolution );
  18. Graphics grphoto = graphics. fromimage (BMP hoto );
  19. // This code loads the watermark image. The watermark image is saved as a BMP file in green (a = 0, r = 0, G = 255, B = 0) as the background color.
  20. // Once again, a variable is defined for its width and height.
  21. Image imgwatermark =NewBitmap (rmarkimgpath );
  22. IntWmwidth = imgwatermark. width;
  23. IntWmheight = imgwatermark. height;
  24. // This Code draws imgphoto to the position of the graphics object (x = 0, y = 0) at the original size of 100%.
  25. // All future drawings will occur on the top of the original photo.
  26. Grphoto. smoothingmode = smoothingmode. antialias;
  27. Grphoto. drawimage (
  28. Imgphoto,
  29. NewRectangle (0, 0, phwidth, phheight ),
  30. 0,
  31. 0,
  32. Phwidth,
  33. Phheight,
  34. Graphicsunit. pixel );
  35. // To maximize the copyright information size, we will test 7 different font sizes to determine the maximum size we can use for our photo width.
  36. // To effectively complete this operation, we define an integer array and traverse these integer values to measure copyright strings of different sizes.
  37. // Once we determine the maximum possible size, we exit the loop and draw the text
  38. Int[] Sizes =New Int[] {, 4 };
  39. Font crfont =Null;
  40. Sizef crsize =NewSizef ();
  41. For(IntI = 0; I <7; I ++)
  42. {
  43. Crfont =NewFont ("Arial", sizes [I],
  44. Fontstyle. Bold );
  45. Crsize = grphoto. measurestring (rmarktext,
  46. Crfont );
  47. If((Ushort) Crsize. width <(Ushort) Phwidth)
  48. Break;
  49. }
  50. // Because all the photos have various heights, the position starting from the bottom of the image starts at 5%.
  51. // Use the height of the rmarktext string to determine the Y axis of the string.
  52. // Determine the X axis by calculating the center of the image, define a stringformat object, and set stringalignment to center.
  53. IntYpixlesfrombottom = (Int) (Phheight *. 05 );
  54. FloatYposfrombottom = (phheight-
  55. Ypixlesfrombottom)-(crsize. Height/2 ));
  56. FloatXcenterofimg = (phwidth/2 );
  57. Stringformat strformat =NewStringformat ();
  58. Strformat. Alignment = stringalignment. Center;
  59. // Now we have all the required coordinates to create a solidbrush with a color (alpha value 60%) of 153 black.
  60. // Draw a Copyright string at the right position of 1 pixel away from the right and at the bottom of the pixel.
  61. // This deviation is used to create the shadow effect. Repeat this process using a brush to draw the same text at the top of the previously drawn text.
  62. Solidbrush semitransbrush2 =
  63. NewSolidbrush (color. fromargb (153, 0 ));
  64. Grphoto. drawstring (rmarktext,
  65. Crfont,
  66. Semitransbrush2,
  67. NewPointf (xcenterofimg + 1, yposfrombottom + 1 ),
  68. Strformat );
  69. Solidbrush semitransbrush =NewSolidbrush (
  70. Color. fromargb (153,255,255,255 ));
  71. Grphoto. drawstring (rmarktext,
  72. Crfont,
  73. Semitransbrush,
  74. NewPointf (xcenterofimg, yposfrombottom ),
  75. Strformat );
  76. // Create a bitmap based on the modified image. Load this bitmap to a new graphic object.
  77. Bitmap bmwatermark =NewBitmap (BMP hoto );
  78. Bmwatermark. setresolution (
  79. Imgphoto. horizontalresolution,
  80. Imgphoto. verticalresolution );
  81. Graphics grwatermark =
  82. Graphics. fromimage (bmwatermark );
  83. // By defining an imageattributes object and setting its two attributes, we implement two colors to achieve translucent watermark effect.
  84. // The first step for processing the watermark image is to make the background image transparent (alpha = 0, r = 0, G = 0, B = 0 ). We use a colormap and define a remaptable to do this.
  85. // As shown above, my watermark is defined as a 100% green background. We will find this color and then replace it with transparency.
  86. Imageattributes =
  87. NewImageattributes ();
  88. Colormap =NewColormap ();
  89. Colormap. oldcolor = color. fromargb (255, 0,255, 0 );
  90. Colormap. newcolor = color. fromargb (0, 0, 0, 0 );
  91. Colormap [] remaptable = {colormap };
  92. // The second color is used to change the opacity of the watermark.
  93. // Use the 5x5 matrix of the rgba space with coordinates provided by the application.
  94. // By setting the third and third columns as 0.3f, we have reached an opaque level. The result is that the watermark is slightly displayed under the image.
  95. Imageattributes. setremaptable (remaptable,
  96. Coloradjusttype. Bitmap );
  97. Float[] [] Colormatrixelements = {
  98. New Float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f },
  99. New Float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f },
  100. New Float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f },
  101. New Float[] {0.0f, 0.0f, 0.0f, 0.3f, 0.0f },
  102. New Float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
  103. };
  104. Colormatrix wmcolormatrix =New
  105. Colormatrix (colormatrixelements );
  106. Imageattributes. setcolormatrix (wmcolormatrix,
  107. Colormatrixflag. Default,
  108. Coloradjusttype. Bitmap );
  109. // When two colors are added to the imageattributes object, we can now draw a watermark on the right hand side of the photo.
  110. // We will deviate from 10 pixels to the bottom, and 10 pixels to the left.
  111. IntMarkwidth;
  112. IntMarkheight;
  113. // Mark is wider than the original graph
  114. If(Phwidth <= wmwidth)
  115. {
  116. Markwidth = phWidth-10;
  117. Markheight = (markwidth * wmheight)/wmwidth;
  118. }
  119. Else If(Phheight <= wmheight)
  120. {
  121. Markheight = phHeight-10;
  122. Markwidth = (markheight * wmwidth)/wmheight;
  123. }
  124. Else
  125. {
  126. Markwidth = wmwidth;
  127. Markheight = wmheight;
  128. }
  129. IntXposofwm = (phwidth-markwidth)-10 );
  130. IntYposofwm = 10;
  131. Grwatermark. drawimage (imgwatermark,
  132. NewRectangle (xposofwm, yposofwm, markwidth,
  133. Markheight ),
  134. 0,
  135. 0,
  136. Wmwidth,
  137. Wmheight,
  138. Graphicsunit. pixel,
  139. Imageattributes );
  140. // The final step is to replace the original image with the new bitmap. Destroy two graphic objects and save the image to the file system.
  141. Imgphoto = bmwatermark;
  142. Grphoto. Dispose ();
  143. Grwatermark. Dispose ();
  144. Imgphoto. Save (rdstimgpath, imageformat. JPEG );
  145. Imgphoto. Dispose ();
  146. Imgwatermark. Dispose ();
  147. }

 

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.