Copyright Protection of images (adding watermarks)

Source: Internet
Author: User

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 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
}
}


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.