Asp.net 文件上傳類(lèi)(取得文件后綴名,保存文件,加入文字水印)
更新時(shí)間:2008年11月10日 13:18:10 作者:
Asp.net 取得文件后綴名,保存文件,加入文字水印的代碼類(lèi)
復(fù)制代碼 代碼如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
namespace EC
{
/// <summary>
/// 上傳類(lèi)
/// </summary>
public class UploadObj
{
public UploadObj()
{
//
// TODO: 在此處添加構(gòu)造函數(shù)邏輯
//
}
/// <summary>
/// 允許文件上傳的類(lèi)型枚舉
/// </summary>
public enum FileType
{
jpg,gif,bmp,png
}
#region 取得文件后綴
/// <summary>
/// 取得文件后綴
/// </summary>
/// <param name="filename">文件名稱(chēng)</param>
/// <returns></returns>
public static string GetFileExtends(string filename)
{
string ext = null;
if (filename.IndexOf('.') > 0)
{
string[] fs = filename.Split('.');
ext = fs[fs.Length - 1];
}
return ext;
}
#endregion
#region 檢測(cè)文件是否合法
/// <summary>
/// 檢測(cè)上傳文件是否合法
/// </summary>
/// <param name="fileExtends">文件后綴名</param>
/// <returns></returns>
public static bool CheckFileExtends(string fileExtends)
{
bool status = false;
fileExtends = fileExtends.ToLower();
string[] fe = Enum.GetNames(typeof(FileType));
for (int i = 0; i < fe.Length; i++)
{
if (fe[i].ToLower() == fileExtends)
{
status = true;
break;
}
}
return status;
}
#endregion
#region 保存文件
/// <summary>
/// 保存文件
/// </summary>
/// <param name="fpath">全路徑,Server.MapPath()</param>
/// <param name="myFileUpload">上傳控件</param>
/// <returns></returns>
public static string PhotoSave(string fpath,FileUpload myFileUpload)
{
string s = "";
string fileExtends = "";
string fileName = myFileUpload.FileName;
if (fileName != "")
{
//取得文件后綴
fileExtends = EC.UploadObj.GetFileExtends(fileName);
if (!EC.UploadObj.CheckFileExtends(fileExtends))
{
EC.MessageObject.ShowPre("上傳文件類(lèi)型不合法");
}
Random rd = new Random();
s = EC.RandomObject.DateRndName(rd) + "." + fileExtends;
string file = fpath + "\\" + s;
try
{
myFileUpload.SaveAs(file);
}
catch (Exception ee)
{
throw new Exception(ee.ToString());
}
}
return s;
}
#endregion
#region 加入文字水印
/// <summary>
/// 加入文字水印
/// </summary>
/// <param name="fileName">文件名稱(chēng)路徑(全路徑)</param>
/// <param name="text">文件</param>
public void AddTextToImg(string fileName, string text)
{
if (!File.Exists(fileName))
{
throw new FileNotFoundException("文件不存在");
}
if (text == string.Empty)
{
return;
}
//判斷文件類(lèi)型是否為圖像類(lèi)型
System.Drawing.Image image = System.Drawing.Image.FromFile(fileName);
Bitmap bitmap = new Bitmap(image, image.Width, image.Height);
Graphics g = Graphics.FromImage(bitmap);
float fontSize = 12.0f;//字體大小
float textWidth = text.Length * fontSize;//文本的長(zhǎng)度
//下面定義一個(gè)矩形區(qū)域,以后在這個(gè)矩形里面畫(huà)上白底黑字
float rectX = 0;
float rectY = 0;
float rectWidth = text.Length * (fontSize + 8);
float rectHeight = fontSize + 8;
//聲明矩形域
RectangleF textArea = new RectangleF(rectX, rectY, rectWidth, rectHeight);
Font font = new Font("宋體", fontSize);//定義字體
Brush whiteBrush = new SolidBrush(Color.White);//白筆刷,畫(huà)文字用
Brush blackBrush = new SolidBrush(Color.Black);//黑筆刷,畫(huà)背景用
g.FillRectangle(blackBrush, rectX, rectY, rectWidth, rectHeight);
g.DrawString(text, font, whiteBrush, textArea);
MemoryStream ms = new MemoryStream();
bitmap.Save(ms, ImageFormat.Jpeg);
//輸出處理后的圖像,這里為了演示方便,我將圖片顯示在頁(yè)面中了
//Response.Clear();
//Response.ContentType = "image/jpeg";
//Response.BinaryWrite(ms.ToArray());
g.Dispose();
bitmap.Dispose();
image.Dispose();
}
#endregion
}
}
您可能感興趣的文章:
- C#(.net)水印圖片的生成完整實(shí)例
- asp.net下用Aspose.Words for .NET動(dòng)態(tài)生成word文檔中的圖片或水印的方法
- asp.net 添加水印的代碼(已測(cè)試)
- .net生成縮略圖及水印圖片時(shí)出現(xiàn)GDI+中發(fā)生一般性錯(cuò)誤解決方法
- asp.net文件上傳功能(單文件,多文件,自定義生成縮略圖,水印)
- asp.net下GDI+的一些常用應(yīng)用(水印,文字,圓角處理)技巧
- .net c# gif動(dòng)畫(huà)如何添加圖片水印實(shí)現(xiàn)思路及代碼
- asp.net如何在圖片上加水印文字具體實(shí)現(xiàn)
- Asp.net簡(jiǎn)單實(shí)現(xiàn)給圖片增加文字水印
- .NET生成水印更好的方法實(shí)例代碼
相關(guān)文章
c# rabbitmq 簡(jiǎn)單收發(fā)消息的示例代碼
這篇文章主要介紹了c# rabbitmq 簡(jiǎn)單收發(fā)消息的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08aspnetpager重寫(xiě)url(偽靜態(tài))配置實(shí)例
這幾天要用到AspNetPager來(lái)做偽靜態(tài)分頁(yè),找了些資料并把修改過(guò)程記錄下來(lái)。2013-04-04asp.net自定義控件回發(fā)數(shù)據(jù)實(shí)現(xiàn)方案與代碼
在實(shí)現(xiàn)asp.net的自定義控件中,若要實(shí)現(xiàn)數(shù)據(jù)的回發(fā)或者post數(shù)據(jù),那自義控件必須實(shí)現(xiàn)IPostBackDataHandler接口, 在該接口中有兩個(gè)方法一個(gè)是LoadPostData,另一個(gè)是RaisePostDataChangedEvent,需要的朋友可以了解下2012-12-12手把手教你AspNetCore WebApi數(shù)據(jù)驗(yàn)證的實(shí)現(xiàn)
這篇文章主要介紹了手把手教你AspNetCore WebApi數(shù)據(jù)驗(yàn)證的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10.NET Core 2.0遷移小技巧之MemoryCache問(wèn)題修復(fù)解決的方法
這篇文章主要給大家介紹了關(guān)于.NET Core 2.0遷移小技巧之MemoryCache問(wèn)題修復(fù)解決的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08