We sometimes need to add copyright watermarks to images on the website. The. Net Graphics class can easily implement this.
(The line at the top of the image is the copyright watermark we added)
Implementation Code : /**/ /// <Summary>
///Generate an image with a copyright watermark
/// </Summary>
/// <Param name = "iconimg">File address of the watermark image to be generated</Param>
Private Void Makewatermark ( String Iconimg)
{
// Define output bitmap
Bitmap BMP = New Bitmap ( 640 , 480 );
// Define a Drawing Object
Graphics g = Graphics. fromimage (BMP );
// Describes the rectangular area of a drawing.
Rectangle newrect = New Rectangle ( 0 , 0 , 640 , 480 );
// Set background color
G. fillrectangle ( New Solidbrush (color. White), newrect );
// Obtain the image to generate the watermark
Bitmap newbitmap = New Bitmap (iconimg );
G. drawimage (newbitmap, 16 , 16 );
// Define the font of the watermark
Font font = New Font (
" Arial " ,
12 ,
Fontstyle. Bold
);
// Define a style brush for water Printing
Lineargradientbrush brush = New Lineargradientbrush (
New Rectangle ( 0 , 0 , 71 , 23 ),
Color. Red,
Color. Blue,
30f,
True
);
// Watermarks a photo
G. drawstring ( " Copyright by test. " + System. datetime. Now. tow.datestring (), Font, brush, 0 , 0 );
// Save as a new output image
MS for system. Io. memorystream = New System. Io. memorystream ();
BMP. Save (MS, imageformat. JPEG );
// Output Image
Response. clearcontent ();
Response. contenttype = " Image/JPEG " ;
Response. binarywrite (Ms. toarray ());
}