亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

asp.net中生成縮略圖并添加版權(quán)實(shí)例代碼

 更新時(shí)間:2013年11月06日 16:37:42   作者:  
這篇文章介紹了asp.net中生成縮略圖并添加版權(quán)實(shí)例代碼,有需要的朋友可以參考一下

復(fù)制代碼 代碼如下:

//定義image類(lèi)的對(duì)象
Drawing.Image image,newimage;
//圖片路徑
protected string imagePath;
//圖片類(lèi)型
protected string imageType;
//圖片名稱(chēng)
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;
//取得圖片類(lèi)型
imageType= imagePath.Substring(imagePath.LastIndexOf(".")+1);
//取得圖片名稱(chēng)
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"); // 在圖片上加入信息說(shuō)明
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!");
}

//還需要判斷文件類(lèi)型是否為圖像類(lèi)型,這里就不贅述了

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è)矩形里畫(huà)上白底黑字
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類(lèi)型
bitmap.Save(ms,ImageFormat.Jpeg);

//輸出處理后的圖像,這里為了演示方便,我將圖片顯示在頁(yè)面中了
/**//* 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();
}

相關(guān)文章

最新評(píng)論