New watermark for pictures in C #

Source: Internet
Author: User

<summary>
Create a picture watermark on a picture
</summary>
<param name= "Path" > Original server picture Path </param>
<param name= "PATH_SYP" > Generated picture watermark with picture path </param>
<param name= "PATH_SYPF" > Watermark picture Path </param>
public static void Addwaterpic (String Path, String path_syp, String path_sypf,int x=0,int y=0)
{
using (Image srcimg = Image.FromFile (Path))
{
To load a watermark file into memory
using (Image WaterMark = Image.FromFile (PATH_SYPF))
{
Instantiate a piece of canvas
using (Graphics g = graphics.fromimage (srcimg))
{

if (x = = 0)
{
x = (int) ((double) srcimg.width-(double) watermark.width)/2);

}
if (y = = 0)
{
y = (int) ((double) Srcimg.height-((double) WATERMARK.HEIGHT/2))/2);
}
Write text
g.DrawString ("17CN Mall All rights reserved ...", New Font ("Microsoft Yahei"), New SolidBrush (color.red), 20,20);
Painting pictures

Given a watermark by picture size
if (Srcimg.width > 100000000)
//{
PATH_SYPF = PATH_SYPF. Replace ("Logo_1.png", "logo_1.png");
//}
else if (Srcimg.width > 500000)
//{
PATH_SYPF = PATH_SYPF. Replace ("Logo_1.png", "logo_1.png");
//}

Watermark compression when the width of the watermark is greater than the width of the image
if (Watermark.width > Srcimg.width)
{
x = 0;
int waterh = (int) (double) Srcimg.width/(Double) watermark.width * (double) watermark.height);
G.drawimage (Watermark.getthumbnailimage (Srcimg.width,waterh, NULL, IntPtr.Zero), New Rectangle (x, Y, Watermark.width , Watermark.height), 0, 0, watermark.width, Watermark.height, GraphicsUnit.Pixel);
}
Else
{
G.drawimage (WaterMark, New Rectangle (x, Y, Watermark.width, Watermark.height), 0, 0, watermark.width, Watermark.height, GraphicsUnit.Pixel);
}
Srcimg.save (PATH_SYP);
}
}

}

New watermark for pictures in C #

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.