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("http://")+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+"http://"+imagename);
//顯示原圖, imagesource為圖片控件
//imagesource.imageurl = "uploadfiles/"+imagename;
//為上傳的圖片建立引用
image=system.drawing.image.fromfile(mpath+"http://"+imagename);
//生成縮略圖
newimage=image.getthumbnailimage(200,200,callb,new system.intptr());
//把縮略圖保存到指定的虛擬路徑
newimage.save(server.mappath("uploadfiles")+"http://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();
}
新聞熱點
疑難解答
圖片精選