Use. net to generate watermark files based on transparency

Source: Internet
Author: User
/// <Summary>
/// Generate a watermark. You can generate a file based on the top left, bottom left, top right, bottom right, center, and transparency. It is only valid for jpeg or jpg format!
/// </Summary>
/// <Param name = "sourceFile"> basemap </param>
/// <Param name = "waterMarkFile"> watermark image </param>
/// <Param name = "saveFile"> file to be saved </param>
/// <Param name = "local"> position: Top left (1), bottom left (2), top right (3), bottom right (4), center (5) </param>
/// <Param name = "alpha"> Transparency (1-100) </param>
/// <Returns> bool, whether it is successful </returns>
Public static bool MakeWaterImage (string sourceFile, string waterMarkFile, string saveFile, int local, int alpha)
{
Bool result;
  
If (! File. Exists (sourceFile) |! File. Exists (waterMarkFile) // if the source image or watermark image does not exist
Return false;
  
FileInfo fi = new FileInfo (sourceFile );
// Determine whether the file type is valid
If (fi. Extension. ToLower ()! = ". Jpg" & fi. Extension. ToLower ()! = ". Jpeg ")
Return false;
  
Try
{
// Source Image
Bitmap sImage = new Bitmap (sourceFile );
Int sWidth = sImage. Width;
Int sHeight = sImage. Height;
  
// Watermark image
Bitmap wImage = new Bitmap (waterMarkFile );
Int wWidth = wImage. Width;
Int wHeight = wImage. Height;

// Make Graphics.
Graphics g = Graphics. FromImage (sImage );
Int x; // Temporary Variable
Int y; // monitoring variable
Int x1 = 0; // The width difference between the source image and the watermark image, that is, the X position of the start drawing.
Int y1 = 0; // the height difference between the source image and the watermark image, that is, the Y position of the Start plot.
Int w = 0; // The width of the watermark image generated, that is, the X position of the ending drawing.
Int h = 0; // The height of the watermark image generated, that is, the Y position of the ending plot.
Int al; // alpha
Int rl; // Red
Int gl; // Green
Int bl; // Blue

// Verify transparency
If (alpha <1 || alpha> 100)
Al = 80;
Else
Al = alpha;
If (sWidth> wWidth & sHeight> wHeight) // if the source image is larger than the watermark image
{
Switch (local)
{
Case 1: // top left
X1 = 0;
Y1 = 0;

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.