- using System;
- using System.Data;
- using System.Configuration;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
-
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
-
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Reflection;
-
-
-
-
- public class FileUpload
- {
- private string BasePath;
-
- public FileUpload()
- {
-
-
-
- }
-
-
-
-
-
-
- public void MarkWater(string filePath, string waterFile)
- {
-
- int i = filePath.LastIndexOf(".");
- string ex = filePath.Substring(i, filePath.Length - i);
- if (string.Compare(ex, ".gif", true) == 0)
- {
- return;
- }
-
- string ModifyImagePath = BasePath + filePath;
- int lucencyPercent = 25;
- Image modifyImage = null;
- Image drawedImage = null;
- Graphics g = null;
- try
- {
-
- modifyImage = Image.FromFile(ModifyImagePath, true);
- drawedImage = Image.FromFile(BasePath + waterFile, true);
- g = Graphics.FromImage(modifyImage);
-
- int x = modifyImage.Width - drawedImage.Width;
- int y = modifyImage.Height - drawedImage.Height;
-
- float[][] matrixItems ={
- new float[] {1, 0, 0, 0, 0},
- new float[] {0, 1, 0, 0, 0},
- new float[] {0, 0, 1, 0, 0},
- new float[] {0, 0, 0, (float)lucencyPercent/100f, 0},
- new float[] {0, 0, 0, 0, 1}};
-
- ColorMatrix colorMatrix = new ColorMatrix(matrixItems);
- ImageAttributes imgAttr = new ImageAttributes();
- imgAttr.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
-
- g.DrawImage(drawedImage, new Rectangle(x, y, drawedImage.Width, drawedImage.Height), 10, 10, drawedImage.Width, drawedImage.Height, GraphicsUnit.Pixel, imgAttr);
-
- string[] allowImageType ={ ".jpg", ".gif", ".png", ".bmp", ".tiff", ".wmf", ".ico" };
- FileInfo fi = new FileInfo(ModifyImagePath);
- ImageFormat imageType = ImageFormat.Gif;
- switch (fi.Extension.ToLower())
- {
- case ".jpg":
- imageType = ImageFormat.Jpeg;
- break;
- case ".gif":
- imageType = ImageFormat.Gif;
- break;
- case ".png":
- imageType = ImageFormat.Png;
- break;
- case ".bmp":
- imageType = ImageFormat.Bmp;
- break;
- case ".tif":
- imageType = ImageFormat.Tiff;
- break;
- case ".wmf":
- imageType = ImageFormat.Wmf;
- break;
- case ".ico":
- imageType = ImageFormat.Icon;
- break;
- default:
- break;
- }
- MemoryStream ms = new MemoryStream();
- modifyImage.Save(ms, imageType);
- byte[] imgData = ms.ToArray();
- modifyImage.Dispose();
- drawedImage.Dispose();
- g.Dispose();
- FileStream fs = null;
- File.Delete(ModifyImagePath);
- fs = new FileStream(ModifyImagePath, FileMode.Create, FileAccess.Write);
- if (fs != null)
- {
- fs.Write(imgData, 0, imgData.Length);
- fs.Close();
- }
- }
- finally
- {
- try
- {
- drawedImage.Dispose();
- modifyImage.Dispose();
- g.Dispose();
- }
- catch { ;}
- }
- }
- }
posted on
2009-08-25 14:08
米高佐敦
阅读(
213)
评论()
编辑
收藏
举报