********************* Created by Chen *************** ***********
* If you think yourArticleYes. Please respect the fruits of the work of the author.
* Source and original author, thank you !!! Email: aishen944-sohu.com
**************************************** ***************************/
Using system;
Using system. drawing;
Using system. Drawing. drawing2d;
Using system. Drawing. imaging;
Using system. IO;
Namespace imagedrawing
{
/// <Summary>
/// Image modification class, mainly used to protect the copyright of images
/// </Summary>
Public class imagemodification
{
# 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 imagemodification ()
{
}
# 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 = 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
}
}