冷风.NET

    ---默默無聞
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

给上传的JPG文件加水印(2004-10-5 10:52完成)用时1.30分

Posted on 2004-10-12 08:02  冷风.net  阅读(867)  评论(2编辑  收藏  举报
using System;
using System.Web;
using System.IO;
using System.Data;
using System.Drawing;
using System.Drawing.Text;
using WebFrame.Config;

namespace TaiPedia.Services
{
#region 上傳文件類型
public enum UpType
{
image=1, //圖片
sound, //聲音
firm, //影片
publish, //出版
error //為空
}
#endregion

/// <summary>
/// UploadFile 的摘要描述。
/// </summary>
public class UploadFile
{
private string ServerPath = ""; //上傳文件存放的服務器目錄
private UpType _UpFileType; //上傳文件類型
private string _UpFileExt=null; //上傳文件捩展名
private string _UpFileName=null; //上傳文件名(絕對路徑)
private System.Web.HttpPostedFile _UpFile; //上傳文件數據
private static float UpImageSize = 1024000; //上傳圖片的大小
private static float UpImageWidth = 200; //上傳圖片的寬
private static float UpImageHeight = 300; //上傳圖片的高
private static float UpSoundSize = 1024000000; //上傳聲音大小
private static float UpFirmSize =1024000000; //上傳影片大小
public string UpError = null; //上傳問題

public UploadFile()
{
ServerPath = GetPath(CBaseConfig.AppRoot + "UpFile/");
_UpFileType = UpType.error;
}

#region 上傳文件
public string UpLoadFile(System.Web.HttpPostedFile _file,ref TaiPedia.Services.UpType upType)
{
_UpFile = _file;
_UpFileType = upType; //指定文件類型加這個
_UpFileExt = GetFile_Ext(_file.FileName);
_UpFileName = GetFileName();
if(_UpFileName==null)
return null;
//上傳圖片
if (upType == UpType.image)
{
if(_UpFile.ContentLength>UpImageSize)
{
this.UpError = "上傳圖片大於1M";
return null;
}
}
//上傳音文件
else if(upType == UpType.sound)
{
if(_UpFile.ContentLength>UpSoundSize)
{
this.UpError = "上傳的聲音文件過大";
return null;
}
}
//上傳影文件
else if(upType == UpType.firm)
{
if(_UpFile.ContentLength>UpFirmSize)
{
this.UpError = "上傳的影片文件過大";
return null;
}
}
//發表格式
else if(upType == UpType.publish)
{
if(_UpFile.ContentLength>UpFirmSize)
{
this.UpError = "上傳的發表文件過大";
return null;
}
}
if(_UpFileType != UpType.error)
{
if(_UpFileExt == "jpg")
MakeWater("台灣百科全書",3);
else
_UpFile.SaveAs(_UpFileName);
upType = _UpFileType;
if(this.UpError==null)
return _UpFileName.Substring(_UpFileName.LastIndexOf("/")+1);
else
return null;
}
else
return null;
}
#endregion


#region 取得文件名
private string GetFileName()
{
string path = ServerPath;
string name = TaiPedia.Services.FPublic.RndNum(15) + "." + _UpFileExt;
//指定文件類型加 && _UpFileType==UpType.image
if(_UpFileExt=="jpg" && _UpFileType==UpType.image)
{
path += "Images/";
_UpFileType = UpType.image;
}
else if((_UpFileExt=="mp3" || _UpFileExt=="wma" || _UpFileExt=="wav") && _UpFileType==UpType.sound)
{
path += "Sound/";
_UpFileType = UpType.sound;
}
else if((_UpFileExt=="mpeg" || _UpFileExt=="avi" || _UpFileExt=="wmv") && _UpFileType==UpType.firm)
{
path += "Firm/";
_UpFileType = UpType.firm;
}
else if((_UpFileExt=="bmp" || _UpFileExt=="tiff") && _UpFileType==UpType.publish)
{
path += "Publish/";
_UpFileType = UpType.publish;
}
else
{
this.UpError = "請選擇正確的文件類型上傳";
_UpFileType = UpType.error;
return null;
}
path = GetPath(path);
if(path==null)
return null;
if(ExistsFile(path + name))
return null;
return path + name;
}
#endregion

#region 獲得目錄
private string GetPath(string _path)
{
if(System.IO.Directory.Exists(_path))
{
return _path;
}
else
{
try
{
_path = System.IO.Directory.CreateDirectory(_path).FullName;
}
catch
{
_path = null;
}
return _path;
}
}
#endregion

#region 判斷文件是否存在
private bool ExistsFile(string _file)
{
if(System.IO.File.Exists(_file))
{
return true;
}
else
{
return false;
}
}
#endregion

#region 刪除指定的文件
private void DelFile(string _path)
{
System.IO.File.Delete(_path);
}
#endregion

#region 獲得文件的擴展名的小寫形式
private string GetFile_Ext(string filename)
{
return filename.Substring(filename.LastIndexOf(".")+1).ToLower();
}
#endregion

#region 給JPG圖片加水印
private void MakeWater(string fontName, int add)
{
//string _path = ServerPath+ "/Images/" + _filename;

string fontFamily = "寧體"; //水印字體名稱
string fontColor = "#000000"; //水印字體顏色
string backColor = "#ffffff"; //背景色
float fontSize = 14; //水印字體大小
float iTop = 5; //顯示字體y坐標
float iLeft = 5; //顯示字體X坐標
System.Drawing.SizeF objFontSize; //字體在繪圖區中所占的長寬

Font waterFont = new Font(fontFamily,fontSize);

//System.Drawing.Image waterImage = System.Drawing.Image.FromFile(_path);
System.Drawing.Image waterImage = System.Drawing.Image.FromStream(_UpFile.InputStream);
System.Drawing.Graphics objGraphics = Graphics.FromImage(waterImage);

objGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
objFontSize = objGraphics.MeasureString(fontName,waterFont);
switch(add)
{
case 1:
iTop = 0;
iLeft = 0;
break;
case 2:
iTop = (waterImage.Height - objFontSize.Height)/2;
iLeft = (waterImage.Width - objFontSize.Width)/2;
break;
case 3:
iTop = waterImage.Height - objFontSize.Height;
iLeft = waterImage.Width - objFontSize.Width;
break;
default:
iTop = 5;
iLeft = 5;
break;
}

System.Drawing.Brush objBrush = new SolidBrush(System.Drawing.ColorTranslator.FromHtml(backColor));
objGraphics.DrawString(fontName,waterFont,objBrush,iLeft+1,iTop+1);
objBrush = new SolidBrush(ColorTranslator.FromHtml(fontColor));
objGraphics.DrawString(fontName,waterFont,objBrush,iLeft,iTop);
//if(ExistsFile(_path))
// DelFile(_path);
waterImage.Save(_UpFileName);
waterImage.Dispose();
objGraphics.Dispose();
}
#endregion

}
}