C # Image Processing Tool class contains drawing watermark, shadow, save, etc.

Source: Internet
Author: User
Tags save file

A C # Written image processing base class, features include getting or setting the image path to modify, getting or setting the picture path (watermark picture) in the picture, getting or setting the watermark in the right margin in the modified picture, getting or setting the watermark's height at the bottom of the picture, getting or setting the transparency to draw the watermark, Note the percentage of the original picture transparency, get or set the path to output images, draw watermarks, draw graphics coordinates, draw shadow images, save files, and so on:

View Sourceprint?001using System;

002using System.Collections.Generic;

003using System.Linq;

004using System.Text;

005using System.IO;

006using System.Drawing;

007using System.Drawing.Drawing2D;

008using System.Drawing.Imaging;

009namespace CLB. Utility.tools

010{

011 public class Imagemodification

012 {

013 #region "Member fields"

014 private String modifyimagepath = null;

015 private String drawedimagepath = null;

016 private int rightspace;

017 private int bottoamspace;

018 private int lucencypercent = 70;

019 private String outpath = null;

020 #endregion

021 Public imagemodification ()

022 {

023}

024 #region "Propertys"

025///

026///Gets or sets the image path to modify

027///

028 public string Modifyimagepath

029 {

030 get {return this.modifyimagepath;}

031 set {This.modifyimagepath = value;}

032}

033///

034///Get or set in the picture path (watermark picture)

035///

036 public String Drawedimagepath

037 {

038 get {return this.drawedimagepath;}

039 set {This.drawedimagepath = value;}

040}

041///

042///Gets or sets the watermark to the right margin in the modified picture

043///

044 public int Rightspace

045 {

046 get {return this.rightspace;}

047 set {this.rightspace = value;}

048}

049//Get or set the watermark at the height of the bottom of the modified picture

050 public int Bottoamspace

051 {

052 get {return this.bottoamspace;}

053 set {this.bottoamspace = value;}

054}

055///

056///Gets or sets the transparency of the watermark to be drawn, note the percentage of the original picture transparency

057///

058 public int Lucencypercent

059 {

060 get {return this.lucencypercent;}

061 Set

062 {

063 if (value >= 0 && value <= 100)

064 this.lucencypercent = value;

065}

066}

067///

068///Gets or sets the path to output the image

069///

070 public String Outpath

071 {

072 get {return this.outpath;}

073 set {This.outpath = value;}

074}

075 #endregion

076 #region "Methods"

077///

078///start to draw the watermark

079///

080 public void DrawImage ()

081 {

082 Image modifyimage = null;

083 Image drawedimage = null;

084 Graphics g = null;

085 Try

086 {

087//Create drawing objects

088 Modifyimage = Image.FromFile (this. Modifyimagepath);

089 drawedimage = Image.FromFile (this. Drawedimagepath);

090 g = graphics.fromimage (modifyimage);

091//Get graph coordinates to draw

092 int x = modifyimage.width-this.rightspace;

093 int y = modifyimage.height-this. Bottoamspace;

094//Set color matrix

095 float[][] Matrixitems ={

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

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

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

099 New float[] {0, 0, 0, (float) this. lucencypercent/100f, 0},

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

ColorMatrix ColorMatrix = new ColorMatrix (matrixitems);

102 ImageAttributes imgattr = new ImageAttributes ();

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

104//Draw Shadow Image

G.drawimage (

Drawedimage,

The new Rectangle (x, Y, Drawedimage.width, drawedimage.height),

108 0, 0, drawedimage.width, Drawedimage.height,

109 GraphicsUnit.Pixel, imgattr);

110//Save file

String[] Allowimagetype = {". jpg", ". gif", ". png", ". bmp", ". Tiff", ". Wmf", ". ico"};

112 FileInfo File = new FileInfo (this. Modifyimagepath);

113 ImageFormat imagetype = imageformat.gif;

114 switch (file. Extension.tolower ())

115 {

116 case ". jpg":

117 imagetype = Imageformat.jpeg;

118 break;

119 case ". gif":

ImageType = Imageformat.gif;

121 break;

122 case ". png":

123 imagetype = Imageformat.png;

124 break;

The case ". bmp":

126 imagetype = imageformat.bmp;

127 break;

128 case ". tif":

129 imagetype = Imageformat.tiff;

130 break;

131 case ". Wmf":

132 imagetype = imageformat.wmf;

The break;

134 case ". ico":

135 imagetype = Imageformat.icon;

136 break;

137 Default:

138 break;

139}

140 MemoryStream ms = new MemoryStream ();

Modifyimage.save (MS, imagetype);

Byte[] Imgdata = Ms. ToArray ();

143 Modifyimage.dispose ();

144 Drawedimage.dispose ();

145 g.dispose ();

146 FileStream fs = null;

147 if (this. Outpath = = NULL | | This. Outpath = "")

148 {

149 File.delete (this. Modifyimagepath);

FS = new FileStream (this. Modifyimagepath, FileMode.Create, FileAccess.Write);

151}

152 Else

153 {

154 fs = new FileStream (this. Outpath, FileMode.Create, FileAccess.Write);

155}

156 if (fs!= null)

157 {

158 fs. Write (imgdata, 0, imgdata.length);

159 FS. Close ();

160}

161}

162 finally

163 {

164 try

165 {

166 Drawedimage.dispose ();

167 modifyimage.dispose ();

G.dispose ();

169}

170 catch {;}

171}

172}

173 #endregion

174}

175}

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.