Common Tool Class 10-Upload watermark class

Source: Internet
Author: User

public class WaterMark
{
<summary>
Image watermark
</summary>
<param name= "Imgpath" > Server picture relative path </param>
<param name= "filename" > Save file name </param>
<param name= "Watermarkfilename" > Watermark file relative path </param>
<param name= "Watermarkstatus" > Picture watermark Position 0= not used 1 = upper Left 2 = upper 3 = upper Right 4 = Left middle 9 = right Bottom </param>
<param name= "Quality" > Additional watermark image quality,0-100</param>
<param name= "Watermarktransparency" > Opacity of the Watermark 1--10 10 is opaque </param>
public static void Addimagesignpic (string imgpath, string filename, string watermarkfilename, int watermarkstatus, int qua lity, int watermarktransparency)
{
if (! File.exists (Utils.getmappath (Imgpath)))
Return
byte[] _imagebytes = File.readallbytes (Utils.getmappath (Imgpath));
Image img = image.fromstream (new System.IO.MemoryStream (_imagebytes));
filename = utils.getmappath (filename);

if (Watermarkfilename.startswith ("/") = = False)
Watermarkfilename = "/" + watermarkfilename;
Watermarkfilename = Utils.getmappath (watermarkfilename);
if (! File.exists (Watermarkfilename))
Return
Graphics g = Graphics.fromimage (IMG);
Setting high-quality interpolation methods
G.interpolationmode = System.Drawing.Drawing2D.InterpolationMode.High;
Set high quality, low speed rendering smoothness
G.smoothingmode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
Image watermark = new Bitmap (watermarkfilename);

if (watermark. Height >= img. Height | | Watermark. Width >= img. Width)
Return

ImageAttributes imageattributes = new ImageAttributes ();
ColorMap ColorMap = new ColorMap ();

Colormap.oldcolor = Color.FromArgb (255, 0, 255, 0);
Colormap.newcolor = Color.FromArgb (0, 0, 0, 0);
Colormap[] remaptable = {COLORMAP};

Imageattributes.setremaptable (remaptable, Coloradjusttype.bitmap);

float transparency = 0.5F;
if (watermarktransparency >= 1 && watermarktransparency <= 10)
Transparency = (watermarktransparency/10.0f);


Float[][] Colormatrixelements = {
New float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
New float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
New float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
New float[] {0.0f, 0.0f, 0.0f, transparency, 0.0f},
New float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
};

ColorMatrix ColorMatrix = new ColorMatrix (colormatrixelements);

Imageattributes.setcolormatrix (ColorMatrix, Colormatrixflag.default, Coloradjusttype.bitmap);

int xpos = 0;
int ypos = 0;

Switch (watermarkstatus)
{
Case 1:
xpos = (int) (IMG. Width * (float). 01);
ypos = (int) (IMG. Height * (float). 01);
Break
Case 2:
xpos = (int) (IMG. Width * (float).-(Watermark. WIDTH/2));
ypos = (int) (IMG. Height * (float). 01);
Break
Case 3:
xpos = (int) (IMG. Width * (float).)-(watermark. Width));
ypos = (int) (IMG. Height * (float). 01);
Break
Case 4:
xpos = (int) (IMG. Width * (float). 01);
ypos = (int) (IMG. Height * (float).-(Watermark. HEIGHT/2));
Break
Case 5:
xpos = (int) (IMG. Width * (float).-(Watermark. WIDTH/2));
ypos = (int) (IMG. Height * (float).-(Watermark. HEIGHT/2));
Break
Case 6:
xpos = (int) (IMG. Width * (float).)-(watermark. Width));
ypos = (int) (IMG. Height * (float).-(Watermark. HEIGHT/2));
Break
Case 7:
xpos = (int) (IMG. Width * (float). 01);
ypos = (int) (IMG. Height * (float).)-Watermark. Height);
Break
Case 8:
xpos = (int) (IMG. Width * (float).-(Watermark. WIDTH/2));
ypos = (int) (IMG. Height * (float).)-Watermark. Height);
Break
Case 9:
xpos = (int) (IMG. Width * (float).)-(watermark. Width));
ypos = (int) (IMG. Height * (float).)-Watermark. Height);
Break
}

G.drawimage (Watermark, New Rectangle (xpos, ypos, Watermark. Width, Watermark. Height), 0, 0, Watermark. Width, Watermark. Height, GraphicsUnit.Pixel, ImageAttributes);

imagecodecinfo[] codecs = Imagecodecinfo.getimageencoders ();
ImageCodecInfo ici = null;
foreach (ImageCodecInfo codec in codecs)
{
if (codec. Mimetype.indexof ("JPEG") >-1)
ICI = codec;
}
EncoderParameters encoderparams = new EncoderParameters ();
long[] Qualityparam = new Long[1];
if (Quality < 0 | | quality > 100)
quality = 80;

Qualityparam[0] = quality;

Encoderparameter Encoderparam = new Encoderparameter (System.Drawing.Imaging.Encoder.Quality, Qualityparam);
Encoderparams.param[0] = Encoderparam;

if (ici! = null)
Img. Save (filename, ICI, encoderparams);
Else
Img. Save (filename);

G.dispose ();
Img. Dispose ();
Watermark. Dispose ();
Imageattributes.dispose ();
}

//<summary>
//Text watermark
//</summary>
//<param name= "Imgpath" > Server picture relative path </param
//<param name= "filename" > Save file name </param>
//<param name= "Watermarktext" > Watermark text </ Param>
//<param name= "Watermarkstatus" > Picture watermark Position 0= not using 1 = upper Left 2 = upper 3 = upper Right 4 = Left middle 9 = right lower </param>
//<par Am name= "Quality" > Additional watermark image quality, 0-100</param>
//<param name= "FontName" > Font </param>
//< param name= "fontsize" > Font size </PARAM>
public static void Addimagesigntext (string imgpath, string filename, string watermarktext, int watermarkstatus, int quality, string fontname, int fontsize)
{
byte[] _imagebytes = File . ReadAllBytes (Utils.getmappath (Imgpath));
Image img = image.fromstream (new System.IO.MemoryStream (_imagebytes));
filename = utils.getmappath (filename);

Graphics g = Graphics.fromimage (IMG);
Font drawfont = new Font (fontname, FontSize, Fontstyle.regular, GraphicsUnit.Pixel);
SizeF crsize;
Crsize = g.measurestring (Watermarktext, Drawfont);

float xpos = 0;
float ypos = 0;

Switch (watermarkstatus)
{
Case 1:
Xpos = (float) img. Width * (float). 01;
Ypos = (float) img. Height * (float). 01;
Break
Case 2:
Xpos = ((float) img. Width * (float).)-(CRSIZE.WIDTH/2);
Ypos = (float) img. Height * (float). 01;
Break
Case 3:
Xpos = ((float) img. Width * (float).)-Crsize.width;
Ypos = (float) img. Height * (float). 01;
Break
Case 4:
Xpos = (float) img. Width * (float). 01;
Ypos = ((float) img. Height * (float).)-(CRSIZE.HEIGHT/2);
Break
Case 5:
Xpos = ((float) img. Width * (float).)-(CRSIZE.WIDTH/2);
Ypos = ((float) img. Height * (float).)-(CRSIZE.HEIGHT/2);
Break
Case 6:
Xpos = ((float) img. Width * (float).)-Crsize.width;
Ypos = ((float) img. Height * (float).)-(CRSIZE.HEIGHT/2);
Break
Case 7:
Xpos = (float) img. Width * (float). 01;
Ypos = ((float) img. Height * (float).)-Crsize.height;
Break
Case 8:
Xpos = ((float) img. Width * (float).)-(CRSIZE.WIDTH/2);
Ypos = ((float) img. Height * (float).)-Crsize.height;
Break
Case 9:
Xpos = ((float) img. Width * (float).)-Crsize.width;
Ypos = ((float) img. Height * (float).)-Crsize.height;
Break
}

g.DrawString (Watermarktext, Drawfont, New SolidBrush (Color.White), xpos + 1, ypos + 1);
g.DrawString (Watermarktext, Drawfont, New SolidBrush (Color.Black), xpos, ypos);

imagecodecinfo[] codecs = Imagecodecinfo.getimageencoders ();
ImageCodecInfo ici = null;
foreach (ImageCodecInfo codec in codecs)
{
if (codec. Mimetype.indexof ("JPEG") >-1)
ICI = codec;
}
EncoderParameters encoderparams = new EncoderParameters ();
long[] Qualityparam = new Long[1];
if (Quality < 0 | | quality > 100)
quality = 80;

Qualityparam[0] = quality;

Encoderparameter Encoderparam = new Encoderparameter (System.Drawing.Imaging.Encoder.Quality, Qualityparam);
Encoderparams.param[0] = Encoderparam;

if (ici! = null)
Img. Save (filename, ICI, encoderparams);
Else
Img. Save (filename);

G.dispose ();
Img. Dispose ();
}
}

Common Tool Class 10-Upload watermark class

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.