C # Asp.net uploading images and adding watermarks (my personal experience)

Source: Internet
Author: User

I would like to share with you today that "C # Asp.net uploading images and adding texts with watermarks" has no technical content and I have not installed 13, but I still hope to repost it with the author and source. Thank you!

Image WatermarkArticleI searched a lot on Baidu or Google, but it seems that there are not many useful ones. Many of them are in a single version. Others are reposted or shared. In many cases, they cannot meet our own needs, today, I want to use the image watermarking knowledge, so I sorted out my experiences for your reference only. I'm a newbie, haha ~~

Let's start with the following:

Step 1: First, I want the principle

Because of the use of Weibo, we have noticed that your Weibo address will be added to the bottom-right corner of the Weibo image, and the watermark will be added to the bottom-right corner no matter whether the image is large or small, based on this idea, I drew a picture to explain it intuitively...:

From the image, we can intuitively and clearly see the principle of this, so weCodeThe problem to be solved is to obtain the width and height of the image, and calculate the length and height of the region based on the font size and number of texts. This is very easy, huh, huh...

Step 2: Write and test the code

The process is omitted. Check the Code directly:

Addwatertext

  ///   <Summary>          ///  Image Watermark text  ///   </Summary>          ///   <Param name = "oldpath">  Old image address  </Param>          ///   <Param name = "text"> Watermark text  </Param>          ///   <Param name = "newpath">  New Image address  </Param>          ///   <Param name = "Alpha">  Transparency  </Param>          ///   <Param name = "fontsize">  Font Size  </Param>          Public   Static  Void Addwatertext ( String Oldpath, String Text, String Newpath, Int Alpha, Int  Fontsize ){  Try  {Text = Text + "  Copyright  "  ; Filestream FS =New  Filestream (oldpath, filemode. Open); binaryreader br = New  Binaryreader (FS );  Byte [] Bytes = Br. readbytes (( Int  ) Fs. Length); br. Close (); FS. Close (); memorystream MS = New  Memorystream (bytes); system. Drawing. Image imgphoto = System. Drawing. image. fromstream (MS );  Int Imgphotowidth = Imgphoto. width;  Int Imgphotoheight = Imgphoto. height; bitmap BMP hoto = New  Bitmap (imgphotowidth, imgphotoheight, system. Drawing. imaging. pixelformat. format24bpprgb); BMP hoto. setresolution (  72 , 72  ); Graphics gbmphoto = Graphics. fromimage (BMP hoto );  //  GIF background color Gbmphoto. Clear (color. fromname ( "  White  "  ); Gbmphoto. interpolationmode = System. Drawing. drawing2d. interpolationmode. High; gbmphoto. smoothingmode = System. Drawing. drawing2d. smoothingmode. highquality; gbmphoto. drawimage (imgphoto,  New Rectangle ( 0 , 0 , Imgphotowidth, imgphotoheight ), 0 ,0  , Imgphotowidth, imgphotoheight, graphicsunit. pixel); system. Drawing. Font font = Null  ; System. Drawing. sizef crsize = New  Sizef (); font = New Font ( "    "  , Fontsize, fontstyle. Bold );  //  Specified measurement area Crsize = Gbmphoto. measurestring (text, font );  Float Y = imgphotoheight- Crsize. height;  Float X = imgphotowidth- Crsize. width; system. Drawing. stringformat strformat = New  System. Drawing. stringformat (); strformat. Alignment = System. Drawing. stringalignment. Center;  //  Draw two transparent Effects System. Drawing. solidbrush semitransbrush2 = New System. Drawing. solidbrush (color. fromargb (alpha, 56 , 56 , 56  ); Gbmphoto. drawstring (text, Font, semitransbrush2, X + 1 , Y + 1  ); System. Drawing. solidbrush semitransbrush = New System. Drawing. solidbrush (color. fromargb (alpha, 176 ,176 , 176  ); Gbmphoto. drawstring (text, Font, semitransbrush, x, y); BMP hoto. save (newpath, system. drawing. imaging. imageformat. JPEG); gbmphoto. dispose (); imgphoto. dispose (); BMP hoto. dispose ();}  Catch  {;}} 

I will not explain the code, but I have basically explained it in principle. In addition, for parameter settings, we can also add fonts, font colors, whether to bold, and what seats to locate, so that the encapsulation is better, as I only need to add the watermark to the bottom right corner, I will not write any more... the principle is the same. encapsulation is also well encapsulated. Let's write it for you, haha...

Next let's take a look, because the figure is the most intuitive display...

I just cut a picture above.

Call method: namespace. Class Name. addwatertext (picpath, "Tandy Tang-blog Garden", picpath, 255, 18 );

Don't forget to recommend it if you think it is helpful to you ~~

PS: Tandy tang I wish you a pleasant time writing your code!

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.