C # is shared by the example in this article. NET add image watermark method, for everyone's reference, the specific contents are as follows
Using System;
Using System.Drawing;
Using System.Drawing.Drawing2D;
Using System.Drawing.Imaging;
Using System.IO; namespace Pub.class {///<summary>///adding a watermark class only supports adding picture watermarks///</summary> public class Watermark {#region
Private member privately string modifyimagepath=null;
private string Drawedimagepath=null;
private int rightspace;
private int bottoamspace;
private int lucencypercent=70;
private string Outpath=null;
#endregion #region Constructor///<summary>///Constructor///</summary> public watermark () {} #endregion #region Properties///<summary>///Gets or sets the image path to modify///</summary> public string Modifyimagepath {Get{return T
His.modifyimagepath;}
Set{this.modifyimagepath=value;} ///<summary>///Gets or sets the picture path (watermark picture)///</summary> public string Drawedimagepath {Get{return this.dr
Awedimagepath;}
Set{this.drawedimagepath=value;} ///<summary>///Gets or sets the watermark in the Modify picture to the right margin///</summary> public int RightspaCE {Get{return this.rightspace;}
Set{this.rightspace=value;} ///<summary>///Gets or sets the height of the watermark at the bottom of the Modify picture///</summary> public int Bottoamspace {Get{return This.bottoa
Mspace;}
Set{this.bottoamspace=value;} ///<summary>///Gets or sets the transparency of the watermark to be drawn, note 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 image///</summary> public string Outpath {Get{return This.outpath;}
Set{this.outpath=value;}
#endregion #region Start drawing watermarks DrawImage///<summary>///start to draw watermarks///</summary>///<example>
<code>///watermark wm = new watermark (); Wm.
Drawedimagepath= Server.MapPath ("") + "/upfile/" + "backlogo.gif"; Wm.
Modifyimagepath=path; Wm.
rightspace=184; Wm.
bottoamspace=81; Wm.
lucencypercent=50; Wm. Outpath=server.mappath (""+ "/upfile/" + fileName + "_new" + extension; Wm.
DrawImage ();
//Save the image after the watermark, delete the original picture///Mfilename=filename + "_new" + extension;
if (file.exists (path)) {file.delete (path);}
</code>///</example> public void DrawImage () {Image modifyimage=null;
Image Drawedimage=null;
Graphics G=null; try {modifyimage=image.fromfile (this. Modifyimagepath);//Create drawing Object Drawedimage=image.fromfile (this.
Drawedimagepath);
G=graphics.fromimage (Modifyimage);
int x=modifyimage.width-this.rightspace;//gets to draw the graph coordinates int y=modifyimage.height-this.bottoamspace; Float[][] Matrixitems ={//set color matrix 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); G.drawimage (//Draw shadow image Drawedimage, New Rectangle (X,y,drawedimage.width,drawedimage.height), 0,0,drawedimage.width,
Drawedimage.height, graphicsunit.pixel,imgattr); String[] allowimagetype={". jpg", ". gif", ". png", ". bmp", ". Tiff", ". Wmf", ". ico"};//save file FileInfo file=new FileInfo (this .
Modifyimagepath);
ImageFormat Imagetype=imageformat.gif; Switch (file.
Extension.tolower ()) {case ". jpg": imagetype=imageformat.jpeg; 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 above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.