萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 編程語言綜合 >> C#圖片處理工具類 含繪制水印、陰影、保存等

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}

copyright © 萬盛學電腦網 all rights reserved