C # image processing tools include watermarking, shadow, and storage

Source: Internet
Author: User

A basic class of C # write image processing, including obtaining or setting the image path to be modified, obtaining or setting the image path (watermark image) in the image), get or set the right margin of the watermark in the modified image, get or set the height of the watermark from the bottom of the modified image, and obtain or set the transparency of the watermark to be drawn, note the following functions:

View sourceprint? 001 using System;

002 using System. Collections. Generic;

003 using System. Linq;

004 using System. Text;

005 using System. IO;

006 using System. Drawing;

007 using System. Drawing. Drawing2D;

008 using System. Drawing. Imaging;

009 namespace 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 percent cypercent = 70;

019 private string outPath = null;

020 # endregion

021 public ImageModification ()

022 {

023}

024 # region "propertys"

025 ///

026 // obtain or set the image path to be modified

027 ///

028 public string ModifyImagePath

029 {

030 get {return this. modifyImagePath ;}

031 set {this. modifyImagePath = value ;}

032}

033 ///

034 // obtain or set the path of the image (watermark image)

035 ///

036 public string DrawedImagePath

037 {

038 get {return this. drawedImagePath ;}

039 set {this. drawedImagePath = value ;}

040}

041 ///

042 // obtain or set the right margin of the watermark in the Modified Image

043 ///

044 public int RightSpace

045 {

046 get {return this. rightSpace ;}

047 set {this. rightSpace = value ;}

048}

049 // obtain or set the watermark height from the bottom of the Modified Image

050 public int BottoamSpace

051 {

052 get {return this. bottoamSpace ;}

053 set {this. bottoamSpace = value ;}

054}

055 ///

056 // obtain or set the watermark transparency. Note that the original image transparency percentage

057 ///

058 public int percent cypercent

059 {

060 get {return this. Your cypercent ;}

061 set

062 {

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

064 this. Repeated cypercent = value;

065}

066}

067 ///

068 // obtain or set the path of the image to be output

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 a watermark

079 ///

080 public void DrawImage ()

081 {

082 Image modifyImage = null;

083 Image drawedImage = null;

084 Graphics g = null;

085 try

086 {

087 // create a graphic object

088 modifyImage = Image. FromFile (this. ModifyImagePath );

089 drawedImage = Image. FromFile (this. DrawedImagePath );

090g = Graphics. FromImage (modifyImage );

091 // obtain the coordinates of the image to be drawn

092 int x = modifyImage. Width-this. rightSpace;

093 int y = modifyImage. Height-this. BottoamSpace;

094 // set the 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. Your cypercent/100f, 0 },

100 new float [] {0, 0, 0, 0, 1 }};

101 ColorMatrix colorMatrix = new ColorMatrix (matrixItems );

102 ImageAttributes imgAttr = new ImageAttributes ();

103 imgAttr. SetColorMatrix (colorMatrix, ColorMatrixFlag. Default, ColorAdjustType. Bitmap );

104 // draw a shadow Image

105g. DrawImage (

106 drawedImage,

107 new Rectangle (x, y, drawedImage. Width, drawedImage. Height ),

108 0, 0, drawedImage. Width, drawedImage. Height,

109 GraphicsUnit. Pixel, imgAttr );

110 // save the file

111 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 ":

120 imageType = ImageFormat. GIFs;

121 break;

122 case ". png ":

123 imageType = ImageFormat. Png;

124 break;

125 case ". bmp ":

126 ImageFormat = ImageFormat. Bmp;

127 break;

128 case ". tif ":

129 ImageFormat = ImageFormat. Tiff;

130 break;

131 case ". wmf ":

132 imageType = ImageFormat. Wmf;

133 break;

134 case ". ico ":

135 ImageFormat = ImageFormat. Icon;

136 break;

Default 137:

138 break;

139}

140 MemoryStream MS = new MemoryStream ();

141 modifyImage. Save (MS, imageType );

142 byte [] imgData = ms. ToArray ();

143 modifyImage. Dispose ();

144 drawedImage. Dispose ();

145g. Dispose ();

146 FileStream fs = null;

147 if (this. OutPath = null | this. OutPath = "")

148 {

149 File. Delete (this. ModifyImagePath );

150 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 ();

168g. Dispose ();

169}

170 catch {;}

171}

172}

173 # endregion

174}

175}

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.