程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> asp.net中生成縮略圖並添加版權實例代碼

asp.net中生成縮略圖並添加版權實例代碼

編輯:ASP.NET基礎

復制代碼 代碼如下:
//定義image類的對象
Drawing.Image image,newimage;
//圖片路徑
protected string imagePath;
//圖片類型
protected string imageType;
//圖片名稱
protected string imageName;
//提供一個回調方法,用於確定Image對象在執行生成縮略圖操作時何時提前取消執行
//如果此方法確定 GetThumbnailImage 方法應提前停止執行,則返回 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圖片,這裡只是舉個例子,並不一定必須是這兩種圖片
if("jpg" != imageType && "gif" != imageType)
{
Response.Write("<script language='javascript'> alert('對不起!請您選擇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對象占用的資源
image.Dispose();
//釋放newimage對象的資源
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; //文本的長度
//下面定義一個矩形區域,以後在這個矩形裡畫上白底黑字
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();
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved