C # resize the image and add a copyright watermark to the image

Source: Internet
Author: User
  1. Using system;
  2. Using system. Data;
  3. Using system. configuration;
  4. Using system. collections;
  5. Using system. Web;
  6. Using system. Web. Security;
  7. Using system. Web. UI;
  8. Using system. Web. UI. webcontrols;
  9. Using system. Web. UI. webcontrols. webparts;
  10. Using system. Web. UI. htmlcontrols;
  11. Using system. drawing;
  12. Using system. Drawing. drawing2d;
  13. Using system. Drawing. imaging;
  14. Public partial class web_makewatermark: system. Web. UI. Page
  15. {
  16. /// <Param name = "newfaxwidtht"> zoom to width </param>
  17. /// <Param name = "newfaxheight"> zoom to height </param>
  18. Int newfaxwidtht = 100;
  19. Int newfaxheight = 120;
  20. Protected void page_load (Object sender, eventargs E)
  21. {
  22. String stroldpic = "images/welcome.jpg ";
  23. String strnewpic = "images/welcome-1.jpg ";
  24. This. smallpic (stroldpic, strnewpic, newfaxwidtht, newfaxheight );
  25. This. makewatermark (strnewpic );
  26. }
  27. /** // <Summary>
  28. /// Generate an image with a copyright watermark
  29. /// </Summary>
  30. /// <Param name = "iconimg"> file address of the watermark image to be generated </param>
  31. Private void makewatermark (string iconimg)
  32. {
  33. // Define the output bitmap
  34. Bitmap BMP = new Bitmap (newfaxwidtht, newfaxheight );
  35. // Define the Drawing Object
  36. Graphics G = graphics. fromimage (BMP );
  37. // Describe the rectangular area of the Drawing
  38. Rectangle newrect = new rectangle (0, 0, newfaxwidtht, newfaxheight );
  39. // Set the background color
  40. G. fillrectangle (New solidbrush (color. White), newrect );
  41. // Obtain the image to generate the watermark
  42. Bitmap newbitmap = new Bitmap (iconimg );
  43. G. drawimage (newbitmap, 0, 0 );
  44. // Define the font of the watermark.
  45. Font font = new font ("Arial", 10, fontstyle. Bold );
  46. // Define the format of the watermark.
  47. Lineargradientbrush brush = new lineargradientbrush (
  48. New rectangle (0, 0, 10, 20 ),
  49. Color. Red,
  50. Color. Blue,
  51. 10f,
  52. True
  53. );
  54. // Add a watermark to the photo
  55. G. drawstring ("szrjjz.com", Font, brush, 0, newFaxHeight-20 );
  56. // Save as a new output image
  57. System. Io. memorystream MS = new system. Io. memorystream ();
  58. BMP. Save (MS, imageformat. JPEG );
  59. // Output image
  60. Response. clearcontent ();
  61. Response. contenttype = "image/JPEG ";
  62. Response. binarywrite (Ms. toarray ());
  63. }
  64. /// <Summary>
  65. /// Scale the image
  66. /// </Summary>
  67. /// <Param name = "stroldpic"> source image file name (including path) </param>
  68. /// <Param name = "strnewpic"> Save As a file name (including path) after scaling </param>
  69. /// <Param name = "intwidth"> zoom to width </param>
  70. /// <Param name = "intheight"> zoom to height </param>
  71. Public void smallpic (string stroldpic, string strnewpic, int intwidth, int intheight)
  72. {
  73. System. Drawing. Bitmap objpic, objnewpic;
  74. Try
  75. {
  76. Objpic = new system. Drawing. Bitmap (stroldpic );
  77. Objnewpic = new system. Drawing. Bitmap (objpic, intwidth, intheight );
  78. Objnewpic. Save (strnewpic );
  79. }
  80. Catch (exception exp) {Throw exp ;}
  81. Finally
  82. {
  83. Objpic = NULL;
  84. Objnewpic = NULL;
  85. }
  86. }
  87. }

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.