一个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}