Asp.net Method for adding a translucent image or text watermark to an image (2)

Source: Internet
Author: User
In the previous article, I talked about how to add text watermarks to images Uploaded By C #. This section describes how to add translucent image watermarks. Parameter: http://hi.baidu.com/1987raymond/blog/item/017a6a4ee9a8ca01b2de0543 . Html

The following is the implementation of the ImageWatermarkPainter class:

 

Using System;
Using System. Text;
Using System. IO;
Using System. Drawing;
Using System. Drawing. Imaging;

Namespace Juice. Common. Drawing
{
   /// <Summary>
   /// Image watermark painter
   /// </Summary>
   Public sealed class ImageWatermarkPainter: WatermarkPainter
   {
       /// <Summary>
       /// Constructor
       /// </Summary>
       /// <Param name = "rawImagePath"> </param>
       /// <Param name = "alpha"> </param>
       /// <Param name = "watermarkImage"> </param>
       Public ImageWatermarkPainter (string rawImagePath, int alpha, string watermarkImage)
           : Base (rawImagePath, alpha)
       {
           WatermarkImagePath = watermarkImage;
       }

       Private string m_WatermarkImagePath;

       /// <Summary>
       /// Watermark image path
       /// </Summary>
       Public string WatermarkImagePath
       {
           Get {return m_WatermarkImagePath ;}
           Set {m_WatermarkImagePath = value ;}
       }

       Private Image m_WatermarkImage;
       /// <Summary>
       /// Watermark image object
       /// </Summary>
       Public Image WatermarkImage
       {
           Get
           {
               If (null = m_WatermarkImage)
                   M_WatermarkImage = Image. FromFile (WatermarkImagePath );
               Return m_WatermarkImage;
           }
       }

       /// <Summary>
       /// Add a watermark
       /// </Summary>
       /// <Param name = "graphics"> </param>
       /// <Param name = "newImage"> </param>
       /// <Returns> </returns>
       Protected override bool AddWatermark (Graphics graphics, Bitmap newImage)
       {
           If (string. IsNullOrEmpty (WatermarkImagePath ))
               Return false;
           If (! System. IO. File. Exists (WatermarkImagePath ))
               Throw new FileNotFoundException (string. Format ("file {0} not found.", WatermarkImagePath ));
           If (WatermarkImage. Width + 10)> newImage. Width | (WatermarkImage. Height + 10)> newImage. Height)
               Return false;
           Using (ImageAttributes attributes = new ImageAttributes ())
           {
               Float alpha = WatermarkAlpha/255.0f;
               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, alpha, 0.0f },
  New float [] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
};
               ColorMatrix colorMatrix = new ColorMatrix (colorMatrixElements );
               /// Set the color adjustment Matrix
               Attributes. SetColorMatrix (colorMatrix, ColorMatrixFlag. Default, ColorAdjustType. Bitmap );
      

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.