We sometimes need to add a copyright watermark to a picture on our site, and the. NET Graphics class can be easily implemented.
Effect Diagram: (the top of the picture is the line that we added the copyright watermark)
Implementation code:
/**////<summary>
Generate a picture with a copyright watermark
</summary>
<param name= "iconimg" > File address to generate a watermark picture </param>
private void Makewatermark (string iconimg)
{
Define output bitmap
Bitmap bmp = New Bitmap (640, 480);
Defining drawing objects
Graphics g = graphics.fromimage (BMP);
Describe the drawing rectangle area
Rectangle newrect = new Rectangle (0, 0, 640, 480);
Set Background color
G.fillrectangle (New SolidBrush (Color.White), newrect);
Get a picture to generate a watermark
Bitmap Newbitmap = new Bitmap (iconimg);
G.drawimage (Newbitmap, 16, 16);
Defines the font for a watermark word
Font font = new Font (
"Arial",
12,
FontStyle.Bold
);
Define the format brush for the watermark Word
LinearGradientBrush brush = new LinearGradientBrush (
New Rectangle (0, 0, 71, 23),
Color.Red,
Color.Blue,
30f,
True
);
To mark a picture with a watermark
g.DrawString ("Copyright by Test." + System.DateTime.Now.ToShortDateString (), font, brush, 0, 0);
Save as a new output picture
System.IO.MemoryStream ms = new System.IO.MemoryStream ();
Bmp. Save (MS, Imageformat.jpeg);
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.