C#圖片處理工具類含繪制水印陰影保存等
來源:易賢網(wǎng) 閱讀:1038 次 日期:2014-08-20 15:14:20
溫馨提示:易賢網(wǎng)小編為您整理了“C#圖片處理工具類含繪制水印陰影保存等”,方便廣大網(wǎng)友查閱!

一個(gè)C#寫的圖片處理基礎(chǔ)類,功能包括獲取或設(shè)置要修改的圖像路徑、獲取或設(shè)置在畫的圖片路徑(水印圖片)、獲取或設(shè)置水印在修改圖片中的右邊距、獲取或設(shè)置水印在修改圖片中距底部的高度、獲取或設(shè)置要繪制水印的透明度,注意是原來圖片透明度的百分比、獲取或設(shè)置要輸出圖像的路徑、繪制水印、繪制圖形坐標(biāo)、繪制陰影圖像、保存文件等功能:

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 /// 獲取或設(shè)置要修改的圖像路徑

027 ///

028 public string ModifyImagePath

029 {

030 get { return this.modifyImagePath; }

031 set { this.modifyImagePath = value; }

032 }

033 ///

034 /// 獲取或設(shè)置在畫的圖片路徑(水印圖片)

035 ///

036 public string DrawedImagePath

037 {

038 get { return this.drawedImagePath; }

039 set { this.drawedImagePath = value; }

040 }

041 ///

042 /// 獲取或設(shè)置水印在修改圖片中的右邊距

043 ///

044 public int RightSpace

045 {

046 get { return this.rightSpace; }

047 set { this.rightSpace = value; }

048 }

049 //獲取或設(shè)置水印在修改圖片中距底部的高度

050 public int BottoamSpace

051 {

052 get { return this.bottoamSpace; }

053 set { this.bottoamSpace = value; }

054 }

055 ///

056 /// 獲取或設(shè)置要繪制水印的透明度,注意是原來圖片透明度的百分比

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 /// 獲取或設(shè)置要輸出圖像的路徑

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 //建立圖形對(duì)象

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

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

090 g = Graphics.FromImage(modifyImage);

091 //獲取要繪制圖形坐標(biāo)

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

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

094 //設(shè)置顏色矩陣

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}

更多信息請(qǐng)查看IT技術(shù)專欄

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:C#圖片處理工具類含繪制水印陰影保存等
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)