足球游戏_中国足彩网¥体育资讯$

C#图片处理工具类含绘制水印阴影保存等
来源:易贤网 阅读:918 次 日期:2014-08-20 15:14:20
温馨提示:易贤网小编为您整理了“C#图片处理工具类含绘制水印阴影保存等”,方便广大网友查阅!

一个C#写的图片处理基础类,功能包括获取或设置要修改的图像路径、获取或设置在画的图片路径(水印图片)、获取或设置水印在修改图片中的右边距、获取或设置水印在修改图片中距底部的高度、获取或设置要绘制水印的透明度,注意是原来图片透明度的百分比、获取或设置要输出图像的路径、绘制水印、绘制图形坐标、绘制阴影图像、保存文件等功能:

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 /// 获取或设置要修改的图像路径

027 ///

028 public string ModifyImagePath

029 {

030 get { return this.modifyImagePath; }

031 set { this.modifyImagePath = value; }

032 }

033 ///

034 /// 获取或设置在画的图片路径(水印图片)

035 ///

036 public string DrawedImagePath

037 {

038 get { return this.drawedImagePath; }

039 set { this.drawedImagePath = value; }

040 }

041 ///

042 /// 获取或设置水印在修改图片中的右边距

043 ///

044 public int RightSpace

045 {

046 get { return this.rightSpace; }

047 set { this.rightSpace = value; }

048 }

049 //获取或设置水印在修改图片中距底部的高度

050 public int BottoamSpace

051 {

052 get { return this.bottoamSpace; }

053 set { this.bottoamSpace = value; }

054 }

055 ///

056 /// 获取或设置要绘制水印的透明度,注意是原来图片透明度的百分比

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 /// 获取或设置要输出图像的路径

069 ///

070 public string OutPath

071 {

072 get { return this.outPath; }

073 set { this.outPath = value; }

074 }

075 #endregion

076 #region "methods"

077 ///

078 /// 开始绘制水印

079 ///

080 public void DrawImage()

081 {

082 Image modifyImage = null;

083 Image drawedImage = null;

084 Graphics g = null;

085 try

086 {

087 //建立图形对象

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

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

090 g = Graphics.FromImage(modifyImage);

091 //获取要绘制图形坐标

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

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

094 //设置颜色矩阵

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

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 //绘制阴影图像

105 g.DrawImage(

106 drawedImage,

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

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

109 GraphicsUnit.Pixel, imgAttr);

110 //保存文件

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.Gif;

121 break;

122 case ".png":

123 imageType = ImageFormat.Png;

124 break;

125 case ".bmp":

126 imageType = ImageFormat.Bmp;

127 break;

128 case ".tif":

129 imageType = ImageFormat.Tiff;

130 break;

131 case ".wmf":

132 imageType = ImageFormat.Wmf;

133 break;

134 case ".ico":

135 imageType = ImageFormat.Icon;

136 break;

137 default:

138 break;

139 }

140 MemoryStream ms = new MemoryStream();

141 modifyImage.Save(ms, imageType);

142 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);

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

168 g.Dispose();

169 }

170 catch { ;}

171 }

172 }

173 #endregion

174 }

175}

中国足彩网信息请查看IT技术专栏

中国足彩网信息请查看网络编程
由于各方面情况的不断调整与变化,易贤网提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!
关于我们 | 联系我们 | 人才招聘 | 网站声明 | 网站帮助 | 非正式的简要咨询 | 简要咨询须知 | 加入群交流 | 手机站点 | 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 足球游戏_中国足彩网¥体育资讯$ 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65317125(9:00—18:00) 获取招聘考试信息及咨询关注公众号:hfpxwx
咨询QQ:526150442(9:00—18:00)版权所有:易贤网
云南网警报警专用图标