Copy Code code as follows:
Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Drawing;
Using System.IO;
Using System.Drawing.Imaging;
Using System.Web;
Using System.Drawing.Drawing2D;
Using System.Reflection;
Namespace Chen
{
public class Warterpic
{
<summary>
To watermark the picture
</summary>
<param name= "filepath" > Original picture Address </param>
<param name= "Waterfile" > Watermark image Address </param>
///
public void Markwater (string filepath, string waterfile)
{
GIF no watermark
int i = filepath. LastIndexOf (".");
String ex = filepath. Substring (i, filepath. LENGTH-I);
if (String.Compare (ex, ". gif", true) = = 0)
{
Return
}
String Modifyimagepath = filepath;//Modified image path
int lucencypercent = 25;
Image modifyimage = null;
Image drawedimage = null;
Graphics g = null;
Try
{
Create a drawing object
Modifyimage = Image.FromFile (Modifyimagepath, true);
Drawedimage = Image.FromFile (Waterfile, true);
g = Graphics.fromimage (modifyimage);
Get graph coordinates to draw
int x = Modifyimage. Width-drawedimage. Width;
int y = modifyimage. Height-drawedimage. Height; 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) 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), ten, Drawedimage. Width, Drawedimage. Height, GraphicsUnit.Pixel, imgattr); Save File
String[] Allowimagetype ={". jpg", ". gif", ". png", ". bmp", ". Tiff", ". Wmf", ". ico"};
FileInfo fi = new FileInfo (Modifyimagepath);
ImageFormat imagetype = imageformat.gif;
Switch (FI. 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;
File.delete (Modifyimagepath);
FS = new FileStream (Modifyimagepath, FileMode.Create, FileAccess.Write);
if (fs!= null)
{
Fs. Write (imgdata, 0, Imgdata. Length);
Fs. Close ();
}
}
Finally
{
Try
{
Drawedimage. Dispose ();
Modifyimage. Dispose ();
G.dispose ();
}
Catch
{ }
}
}
}
}