//定義image類的對(duì)象
Drawing.Image image,newimage;
//圖片路徑
protected string imagePath;
//圖片類型
protected string imageType;
//圖片名稱
protected string imageName;
//提供一個(gè)回調(diào)方法,用于確定Image對(duì)象在執(zhí)行生成縮略圖操作時(shí)何時(shí)提前取消執(zhí)行
//如果此方法確定 GetThumbnailImage 方法應(yīng)提前停止執(zhí)行,則返回 true;否則返回 false
System.Drawing.Image.GetThumbnailImageAbort callb = null;
private void sm_Click(object sender, System.EventArgs e)
{
string mPath;
if("" != File1.PostedFile.FileName) //File1為上傳文件控件
{
imagePath = File1.PostedFile.FileName;
//取得圖片類型
imageType= imagePath.Substring(imagePath.LastIndexOf(".")+1);
//取得圖片名稱
imageName = imagePath.Substring(imagePath.LastIndexOf("\\")+1);
//判斷是否是JPG或者GIF圖片,這里只是舉個(gè)例子,并不一定必須是這兩種圖片
if("jpg" != imageType "gif" != imageType)
{
Response.Write("script language='javascript'> alert('對(duì)不起!請(qǐng)您選擇jpg或者gif格式的圖片!');/script>");
return;
}
else
{
try
{
//建立虛擬路徑
mPath=Server.MapPath("UploadFiles");
//保存到虛擬路徑
File1.PostedFile.SaveAs(mPath+"\\"+imageName);
//顯示原圖, imageSource為圖片控件
//imageSource.ImageUrl = "UploadFiles/"+imageName;
//為上傳的圖片建立引用
image=System.Drawing.Image.FromFile(mPath+"\\"+imageName);
//生成縮略圖
newimage=image.GetThumbnailImage(200,200,callb,new System.IntPtr());
//把縮略圖保存到指定的虛擬路徑
newimage.Save(Server.MapPath("UploadFiles")+"\\small"+imageName);
//釋放image對(duì)象占用的資源
image.Dispose();
//釋放newimage對(duì)象的資源
newimage.Dispose();
//顯示縮略圖
AddTextToImg ("UploadFiles/"+"small"+imageName,"Pic Info"); // 在圖片上加入信息說明
Image1.ImageUrl = "UploadFiles/"+"small"+imageName;
Script.Alert("上傳成功!");
}
catch
{
Script.Alert("上傳失敗!");
}
} // end else
}
// 在圖片上加入自己的信息,
// AddTextToImg (physicPath,"Pic Info");
private void AddTextToImg(string fileName,string text)
{
//string sss = MapPath(fileName);
if ( !File.Exists ( fileName)) {
throw new FileNotFoundException("The file don't exist!");
}
//還需要判斷文件類型是否為圖像類型,這里就不贅述了
System.Drawing.Image image = System.Drawing.Image.FromFile(fileName);//MapPath(fileName));
Bitmap bitmap = new Bitmap(image,image.Width,image.Height);
Graphics g = Graphics.FromImage(bitmap);
float fontSize = 22.0f; //字體大小
float textWidth = text.Length*fontSize; //文本的長(zhǎng)度
//下面定義一個(gè)矩形區(qū)域,以后在這個(gè)矩形里畫上白底黑字
float rectX = 0;
float rectY = 0;
float rectWidth = text.Length*(fontSize+18);
float rectHeight = fontSize+18;
//聲明矩形域
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();
//保存為Jpg類型
bitmap.Save(ms,ImageFormat.Jpeg);
//輸出處理后的圖像,這里為了演示方便,我將圖片顯示在頁面中了
/**//* Response.Clear();
Response.ContentType = "image/jpeg";
Response.BinaryWrite( ms.ToArray() );
*/
FileStream fs=new FileStream(fileName, FileMode.OpenOrCreate);//.CreateNew);
fs.Write(ms.ToArray(),0,ms.ToArray().Length);
fs.Close();
Image1.ImageUrl = fileName; // 將圖片顯示在Image控件中
g.Dispose();
bitmap.Dispose();
image.Dispose();
}