About C # operations on pictures (watermarks, transparency)

Source: Internet
Author: User

<summary>

watermarking for processing watermark images

</summary>

<param name= "path" > Image path with watermark </param>

<returns> return image handling good watermark </returns>

Private Image Operationphoto (string path)

{

Image image = null;

Xmlcollectphotoset xmlcollectphotoset = new Xmlcollectphotoset ();

Collectphotosetinfo collectphotosetinfo = new Collectphotosetinfo ();

Collectphotosetinfo = Xmlcollectphotoset.get ();

String Pathnew=collectphotosetinfo.converphoto;

if (Collectphotosetinfo.cutwater = = "Yes")

{

Capturing watermarks

Image=cutwater (path);

}

else if (Collectphotosetinfo.cutwater = = "No")

{

Image Overlay Watermark

if (Collectphotosetinfo.converwater = = "Yes")

{

Image image1 = image.fromfile (path);

Image imagenew = Image.FromFile (Collectphotosetinfo.converphoto);

Image = Converwater (Image1, imagenew);

Image1. Dispose ();

Imagenew.dispose ();

}

else if (Collectphotosetinfo.converwater = = "No")

{

Image image1 = image.fromfile (path);

image = Image1;

}

}

Image overlay Watermark and add image watermark There is no relationship can exist at the same time can be separated

if (Collectphotosetinfo.usingd = = "Yes")

{

Working with image images

if (image = = null)

{

Image image1 = image.fromfile (path);

image = Image1;

}

Image imagewater = image;

String rmarkimgpath=collectphotosetinfo.waterphoto;//watermark Picture

String textstr = collectphotosetinfo.watertext;//Watermark Text

Font f = new Font (collectphotosetinfo.fontfamily, collectphotosetinfo.fontsize);

Brush B = new SolidBrush (Collectphotosetinfo.fontcolor);

float opacity = collectphotosetinfo.opacity;

Image = Bulidwatermarkphoto (image, Rmarkimgpath, opacity, textstr, F, b);// Iamge manipulated picture Rmarkimgpath watermark Picture 0.6f transparency textstr text f font B font Color

}

return image;

}


<summary>

Remove watermark

</summary>

<param name= "path" > Images with watermarks </param>

<returns> Remove the image after Watermark </returns>

Private Image Cutwater (string path)

{

Bitmap Sourcebitmap = new Bitmap (path);

int width = sourcebitmap.width;

int height = sourcebitmap.height;

Bitmap Resultbitmap = new Bitmap (width, height);

using (Graphics g = graphics.fromimage (Resultbitmap))

{

Rectangle resultrectangle = new Rectangle (0, 0, width, height);

Rectangle sourcerectangle = new Rectangle (0 + 1, 0 + 1,width-200, height);

G.drawimage (Sourcebitmap, Resultrectangle, Sourcerectangle, GraphicsUnit.Pixel);

}

return resultbitmap;

}


<summary>

Overlay a watermark with a picture

</summary>

<param name= "path" > Watermark image </param>

<param name= "pathnew" > Overlay pictures </param>

<returns> overlay images with watermarks </returns>

Private image Converwater (image image, Image imagenew)

{

Try

{

int width = image. Width;

int height = image. Height;

Graphics g = graphics.fromimage (image);

G.drawimage (Imagenew, New Rectangle (width-230, height-180, $), 0, 0, image. Width, image. Height, GraphicsUnit.Pixel);

G.dispose ();

}

catch (Exception ex)

{

}

return image;

}


<summary>

Making watermark images (text, pictures or text and pictures)

</summary>

<param name= "Picimage" > Original picture </param>

<param name= "Rmarkimgpath" > Desired watermark Image </param>

<param name= "Opacityvalue" > Transparency </param>

<param name= "Textstr" > Text </param>

<param name= "Font" > Fonts </param>

<param name= "Brush" > Brushes </param>

Public Image Bulidwatermarkphoto (image picimage, string rmarkimgpath, float opacityvalue, string textstr, font font, Brush Brush

{

dictionary<string, string> dicpaths = new dictionary<string, string> ();

Parse the path of a watermark image

if (Rmarkimgpath! = null && rmarkimgpath.length > 0)

{

string[] paths = Rmarkimgpath.split (';');

for (int i = 0; i < paths. Length; i++)

{

String path = Paths[i]. Split (', ') [0];

String name = Paths[i]. Split (', ') [1];

Dicpaths.add (name, path);

}

}

dictionary<string, string> dictexts = new dictionary<string, string> ();

Parse text

if (textstr! = null && textstr.length > 0)

{

String[] Texts=textstr.split (';');

for (int i = 0; i < texts. Length; i++)

{

string text = Texts[i]. Split (', ') [0];

String name = Texts[i]. Split (', ') [1];

Dictexts.add (name, text);

}

}

Image image = Picimage;

dictionary<string, string> keyValue = Datacache.keyvalue;

foreach (string key in Keyvalue.keys)

{

Graphics g = graphics.fromimage (image);

int x = Convert.ToInt32 (Keyvalue[key]. Split (', ') [0]);

int y = Convert.ToInt32 (Keyvalue[key]. Split (', ') [1]);

if (dicpaths! = null && dicpaths.count > 0)//Determine if there is a watermark picture

{

Image copyimage = Image.FromFile (Dicpaths[key]);//Watermark Picture Path

Copyimage = Changeopacity (Copyimage, opacityvalue);//processed transparent pictures

G.drawimage (Copyimage, New Rectangle (x, y, $, a), 0, 0, image. Width, image. Height, GraphicsUnit.Pixel);//painting watermark Picture

}

if (dictexts! = null && dictexts.count > 0)//Determine if there is a watermark word

{

Image txtimage = Textimage (Dictexts[key], font, brush);//Text

Txtimage = Changeopacity (Txtimage, Opacityvalue);

G.drawimage (Txtimage, New Rectangle (x, y, +, +), +, +, image. Width, image. Height, GraphicsUnit.Pixel);//Draw on the watermark text

}

G.dispose ();

}

return image;

}


<summary>

Change the transparency of a picture

</summary>

<param name= "img" > Pictures </param>

<param name= "Opacityvalue" > Transparency </param>

<returns></returns>

public static Bitmap changeopacity (Image img, float opacityvalue)

{

Float[][] NArray ={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, opacityvalue, 0},

New float[] {0, 0, 0, 0, 1}};

ColorMatrix matrix = new ColorMatrix (NArray);

ImageAttributes attributes = new ImageAttributes ();

Attributes. SetColorMatrix (Matrix, Colormatrixflag.default, Coloradjusttype.bitmap);

Image srcimage = img;

Bitmap resultimage = new Bitmap (srcimage.width, srcimage.height);

Graphics g = graphics.fromimage (resultimage);

G.drawimage (Srcimage, New Rectangle (0, 0, Srcimage.width, srcimage.height), 0, 0, srcimage.width, Srcimage.height, GraphicsUnit.Pixel, attributes);

return resultimage;

}


<summary>

Make a transparent diagram with text

</summary>

<param name= "Textstr" > Text </param>

<param name= "Font" > Text style </param>

<param name= "Brush" > Brushes </param>

<returns> transparent diagram with text </returns>

Private Image Textimage (string textstr, font font, brush brush)

{

string path = [email protected] "\collectionphoto.jpg";

Image image = Image.FromFile (path);

Image = Changeopacity (image, 0.0f);

Graphics g = graphics.fromimage (image);

g.DrawString (textstr, font, brush, 60, 60);

G.dispose ();

Image imagenew = image;

return imagenew;

}


This article comes from the "learning program that reminds me of math:" blog, be sure to keep this provenance http://iammoon24.blog.51cto.com/2552968/1790017

About C # operations on pictures (watermarks, transparency)

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.