Asp.net 檔案上傳類(取得檔案尾碼名,儲存檔案,加入文字浮水印)

來源:互聯網
上載者:User

複製代碼 代碼如下: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>
/// 上傳類
/// </summary>
public class UploadObj
{

public UploadObj()
{
//
// TODO: 在此處添加建構函式邏輯
//
}
/// <summary>
/// 允許檔案上傳的類型枚舉
/// </summary>
public enum FileType
{
jpg,gif,bmp,png
}

#region 取得檔案尾碼
/// <summary>
/// 取得檔案尾碼
/// </summary>
/// <param name="filename">檔案名稱</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 檢測檔案是否合法
/// <summary>
/// 檢測上傳檔案是否合法
/// </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("上傳檔案類型不合法");
}
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">檔案名稱路徑(全路徑)</param>
/// <param name="text">檔案</param>
public void AddTextToImg(string fileName, string text)
{
if (!File.Exists(fileName))
{
throw new FileNotFoundException("檔案不存在");
}
if (text == string.Empty)
{
return;
}
//判斷檔案類型是否為映像類型

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;//文本的長度
//下面定義一個矩形地區,以後在這個矩形裡面畫上白底黑字
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);//白筆刷,畫文字用
Brush blackBrush = new SolidBrush(Color.Black);//黑筆刷,畫背景用
g.FillRectangle(blackBrush, rectX, rectY, rectWidth, rectHeight);
g.DrawString(text, font, whiteBrush, textArea);
MemoryStream ms = new MemoryStream();
bitmap.Save(ms, ImageFormat.Jpeg);
//輸出處理後的映像,這裡為了示範方便,我將圖片顯示在頁面中了
//Response.Clear();
//Response.ContentType = "image/jpeg";
//Response.BinaryWrite(ms.ToArray());
g.Dispose();
bitmap.Dispose();
image.Dispose();
}
#endregion
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.