文件上传画水印
文件上傳:
//把相對路徑變成絕對路徑。
string absolutePath = Server.MapPath(relativePath);
FileUpload控件:
屬性:
FileName:文件名
HasFile:bool 是否選中了文件
FileBytes:要上傳文件的二進制數據
方法:
SaveAs(string 絕對路徑):上傳,另存為。
一、上傳到硬盤文件夾
(一)傳單個文件
第一步:準備好文件及路徑:
//把之前在客戶端的文件名給取出來
string fileName = FileUpload1.FileName;
//防止文件重名
fileName = DateTime.Now.ToString("yyyyMMddhhmmsss") + fileName;
//把相對路徑轉化為絕對路徑
string path = Server.MapPath("uploads/" + fileName);
第二步:執行上傳:
//上傳
FileUpload1.SaveAs(path); //參數必須根路徑
注意:
1.如何防止文件重名?
2.如何防止同一時間點不同用戶傳統一文件名?
(二)傳多個文件:
思路:遍歷表單中所有的FileUpload控件,如果選擇文件就上傳
int index = 0;
foreach (Control ctrl in form1.Controls)
{
if (ctrl is FileUpload)
{
index++;
//取得每個上傳控件
FileUpload upload = ctrl as FileUpload;
//上傳控件中選上文件了
if (upload.HasFile)
{
//做文件路徑出來
string path = Server.MapPath("uploads/" + DateTime.Now.ToString("yyyyMMddhhmmss") + index.ToString("00") + upload.FileName);
//上傳
upload.SaveAs(path);
}
}
}
二、上傳到數據庫Image字段:
(一)傳到數據庫去
1.做數據庫的操作代碼。DA Data
Image字段對應在程序里是byte[]類型
2.做界面上的代碼。
a.把界面的值取出來
FileUpload1.FileBytes - 用來獲得上傳文件的二進制數據。
b.送到數據庫去
(二)從數據庫中找出來,顯示出來
法一:會生成垃圾文件
在服務端生成一個JPG,把這個JPG的路徑賦給Image控件
法二:單獨做一個用來顯示圖片二進制數據的頁面。把這個頁面賦給Image控件。
?
上傳圖片加水印:
//一、從上傳數據中,轉化成圖片對象。
Stream s = FileUpload1.FileContent;
System.Drawing.Image img = System.Drawing.Image.FromStream(s);
//二、對圖片對象進行畫水印
//1.造筆
SolidBrush brush = new SolidBrush(Color.Yellow);
//2.造字體
Font font = new Font("Comic Sans MS", 18);
//3.找到圖像繪圖區域
Graphics g = Graphics.FromImage(img);
g.DrawString("http://www.itNBA.com", font, brush, 0, 0);
//三、圖片對象另存到硬盤上去
string fileName = FileUpload1.FileName;
string path = Server.MapPath("uploads/" + fileName);
img.Save(path);
?
轉載于:https://www.cnblogs.com/thq1218/p/4541898.html
總結
- 上一篇: Android中dispatchTouc
- 下一篇: 第五十九天 how can I 坚持 -