CopyCode The Code is 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>
/// Add a watermark to the image
/// </Summary>
/// <Param name = "filepath"> original image address </param>
/// <Param name = "waterfile"> watermark image address </param>
///
Public void markwater (string filepath, string waterfile)
{
// GIF without watermarks
Int I = filepath. lastindexof (".");
String EX = filepath. substring (I, filepath. Length-I );
If (string. Compare (ex, ". GIF", true) = 0)
{
Return;
}
String modifyimagepath = filepath; // the path of the modified image.
Int bytes cypercent = 25;
Image modifyimage = NULL;
Image drawedimage = NULL;
Graphics G = NULL;
Try
{
// Create a graphic object
Modifyimage = image. fromfile (modifyimagepath, true );
Drawedimage = image. fromfile (waterfile, true );
G = graphics. fromimage (modifyimage );
// Obtain the coordinates of the image to be drawn
Int x = modifyimage. Width-drawedimage. width;
Int y = modifyimage. Height-drawedimage. height; // sets the color matrix.
Float [] [] matrixitems = {New float [] {1, 0, 0, 0}, new float [] {0, 1, 0, 0, 0 }, new float [] {0, 0, 1, 0, 0}, new float [] {0, 0, 0, (float) 1_cypercent/100f, 0 }, new float [] {0, 0, 0, 0, 1 }};
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), 10, 10, drawedimage. width, drawedimage. height, graphicsunit. pixel, imgattr); // save the 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
{}
}
}
}
}