asp.net code to add watermarks (tested) _ Practical Tips

Source: Internet
Author: User
Tags save file
The Watermark function code is shown below
Copy Code code as follows:

<summary>
Image modification class, mainly used to protect the copyright of the picture, the copyright belongs to the original author all
</summary>
public class Picmark
{
#region "Member Fields"
private string modifyimagepath = null;
private string drawedimagepath = null;
private int rightspace;
private int bottoamspace;
private int lucencypercent = 70;
private string outpath = null;
#endregion
Public Picmark ()
{
}
#region "Propertys"
<summary>
Gets or sets the image path to modify
</summary>
public string Modifyimagepath
{
get {return this.modifyimagepath;}
set {This.modifyimagepath = value;}
}
<summary>
Gets or sets the picture path in the drawing (watermark picture)
</summary>
public string Drawedimagepath
{
get {return this.drawedimagepath;}
set {This.drawedimagepath = value;}
}
<summary>
Gets or sets the right margin of the watermark in the modified picture
</summary>
public int Rightspace
{
get {return this.rightspace;}
set {this.rightspace = value;}
}
Gets or sets the height of the watermark at the bottom of the Modify picture
public int Bottoamspace
{
get {return this.bottoamspace;}
set {this.bottoamspace = value;}
}
<summary>
Gets or sets the transparency of the watermark to be drawn, noting the percentage of the original picture transparency
</summary>
public int Lucencypercent
{
get {return this.lucencypercent;}
Set
{
if (value >= 0 && value <= 100)
This.lucencypercent = value;
}
}
<summary>
Gets or sets the path to output the image
</summary>
public string Outpath
{
get {return this.outpath;}
set {This.outpath = value;}
}
#endregion
#region "Methods"
<summary>
Start Drawing watermarks
</summary>
public void DrawImage ()
{
Image modifyimage = null;
Image drawedimage = null;
Graphics g = null;
Try
{
Create a drawing object
Modifyimage = Image.FromFile (this. Modifyimagepath);
Drawedimage = Image.FromFile (this. Drawedimagepath);
g = Graphics.fromimage (modifyimage);
Get graph coordinates to draw
int x = Modifyimage.width-this.rightspace;
int y = modifyimage.height-this. Bottoamspace;
Set 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. lucencypercent/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 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 foreground code looks like this
Copy Code code 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 ">
<title></title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:fileupload id= "FileUpload1" runat= "Server"/>
<br/>
<asp:button id= "Button1" runat= "Server" onclick= "Button1_Click" text= "button"/>
</div>
</form>
</body>

The code for the CS class is shown below
Copy Code code as follows:

protected void Button1_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);
Instantiating classes
Picmark wm = new Picmark ();
Wm. Drawedimagepath = Server.MapPath ("/upfile/" + "backlogo.gif");
Wm. Modifyimagepath = Savepath;
Wm. Rightspace = 145;
Wm. Bottoamspace = 17;
Wm. Lucencypercent = 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 image after the watermark, delete the original picture
if (file.exists (Savepath))
{
File.delete (Savepath);
File.delete (Wm. Outpath);
}
Related Article

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.