The watermark Function Code is as follows:
Copy codeThe Code is as follows:
/// <Summary>
/// Image modification class, mainly used to protect the copyright of the image. The copyright belongs to the original author.
/// </Summary>
Public class picmark
{
# Region "member fields"
Private string modifyImagePath = null;
Private string drawedImagePath = null;
Private int rightSpace;
Private int bottoamSpace;
Private int percent cypercent = 70;
Private string outPath = null;
# Endregion
Public picmark ()
{
}
# Region "propertys"
/// <Summary>
/// Obtain or set the image path to be modified
/// </Summary>
Public string ModifyImagePath
{
Get {return this. modifyImagePath ;}
Set {this. modifyImagePath = value ;}
}
/// <Summary>
/// Obtain or set the path of the image (watermark image)
/// </Summary>
Public string DrawedImagePath
{
Get {return this. drawedImagePath ;}
Set {this. drawedImagePath = value ;}
}
/// <Summary>
/// Obtain or set the right margin of the watermark in the Modified Image
/// </Summary>
Public int RightSpace
{
Get {return this. rightSpace ;}
Set {this. rightSpace = value ;}
}
// Obtain or set the watermark height from the bottom of the Modified Image
Public int BottoamSpace
{
Get {return this. bottoamSpace ;}
Set {this. bottoamSpace = value ;}
}
/// <Summary>
/// Obtain or set the watermark transparency. Note that the original image transparency percentage
/// </Summary>
Public int percent cypercent
{
Get {return this. Your cypercent ;}
Set
{
If (value> = 0 & amp; value <= 100)
This. Repeated cypercent = value;
}
}
/// <Summary>
/// Obtain or set the path of the image to be output
/// </Summary>
Public string OutPath
{
Get {return this. outPath ;}
Set {this. outPath = value ;}
}
# Endregion
# Region "methods"
/// <Summary>
/// Start to draw the watermark
/// </Summary>
Public void DrawImage ()
{
Image modifyImage = null;
Image drawedImage = null;
Graphics g = null;
Try
{
// Create a graphic object
ModifyImage = Image. FromFile (this. ModifyImagePath );
DrawedImage = Image. FromFile (this. DrawedImagePath );
G = Graphics. FromImage (modifyImage );
// Obtain the coordinates of the image to be drawn
Int x = modifyImage. Width-this. rightSpace;
Int y = modifyImage. Height-this. BottoamSpace;
// Sets the color matrix.
Float [] [] matrixItems = {
New float [] {1, 0, 0, 0, 0 },
New float [] {0, 1, 0, 0, 0 },
New float [] {0, 0, 1, 0, 0 },
New float [] {0, 0, 0, (float) this. Your cypercent/100f, 0 },
New float [] {0, 0, 0, 0, 1 }};
ColorMatrix colorMatrix = new ColorMatrix (matrixItems );
ImageAttributes imgAttr = new ImageAttributes ();
ImgAttr. SetColorMatrix (colorMatrix, ColorMatrixFlag. Default, ColorAdjustType. Bitmap );
// Draw a shadow Image
G. DrawImage (
DrawedImage,
New Rectangle (x, y, drawedImage. Width, drawedImage. Height ),
0, 0, drawedImage. Width, drawedImage. Height,
GraphicsUnit. Pixel, imgAttr );
// Save the file
String [] allowImageType = {". jpg ",". gif ",". png ",". bmp ",". tiff ",". wmf ",". ico "};
FileInfo file = new FileInfo (this. ModifyImagePath );
ImageFormat imageType = ImageFormat. Gif;
Switch (file. Extension. ToLower ())
{
Case ". jpg ":
ImageType = ImageFormat. Jpeg;
Break;
Case ". gif ":
ImageType = ImageFormat. Gif;
Break;
Case ". png ":
ImageType = ImageFormat. Png;
Break;
Case ". bmp ":
ImageType = ImageFormat. Bmp;
Break;
Case ". tif ":
ImageType = ImageFormat. Tiff;
Break;
Case ". wmf ":
ImageType = ImageFormat. Wmf;
Break;
Case ". ico ":
ImageType = ImageFormat. Icon;
Break;
Default:
Break;
}
MemoryStream MS = new MemoryStream ();
ModifyImage. Save (MS, imageType );
Byte [] imgData = ms. ToArray ();
ModifyImage. Dispose ();
DrawedImage. Dispose ();
G. Dispose ();
FileStream fs = null;
If (this. OutPath = null | this. OutPath = "")
{
File. Delete (this. ModifyImagePath );
Fs = new FileStream (this. ModifyImagePath, FileMode. Create, FileAccess. Write );
}
Else
{
Fs = new FileStream (this. OutPath, FileMode. Create, FileAccess. Write );
}
If (fs! = Null)
{
Fs. Write (imgData, 0, imgData. Length );
Fs. Close ();
}
}
Finally
{
Try
{
DrawedImage. Dispose ();
ModifyImage. Dispose ();
G. Dispose ();
}
Catch {;}
}
}
# Endregion
}
The front-end code is as follows:
Copy codeThe Code is as follows:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "WebForm1.aspx. cs" Inherits = "demo. WebForm1" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Asp: FileUpload ID = "FileUpload1" runat = "server"/>
<Br/>
<Asp: Button ID = "Button1" runat = "server" onclick = "button#click" Text = "Button"/>
</Div>
</Form>
</Body>
</Html>
The cs class code is as follows:
Copy codeThe Code is as follows:
Protected void button#click (object sender, EventArgs e)
{
String extension = Path. GetExtension (this. FileUpload1.FileName). ToUpper ();
String fileName = Guid. NewGuid (). ToString ();
String savePath = Server. MapPath ("../upfile/" + fileName + extension );
If (! Directory. Exists (Path. GetDirectoryName (savePath )))
{
Directory. CreateDirectory (Path. GetDirectoryName (savePath ));
}
This. FileUpload1.SaveAs (savePath );
// Instantiate the class
Picmark wm = new picmark ();
Wm. DrawedImagePath = Server. MapPath ("/upfile/" + "backlogo.gif ");
Wm. ModifyImagePath = savePath;
Wm. RightSpace = 145;
Wm. BottoamSpace = 17;
Wm. cypercent = 50;
Wm. OutPath = Server. MapPath ("/upfile/" + fileName. Replace ("-", ""). ToUpper () + extension );
Wm. DrawImage ();
// FileName = "_ New _" + fileName;
// String sPath = Server. MapPath ("../upfile/" + fileName + extension );
// This. FileUpload1.SaveAs (sPath );
// Save the watermark image and delete the original image
If (File. Exists (savePath ))
{
File. Delete (savePath );
// File. Delete (wm. OutPath );
}