asp.net實(shí)現(xiàn)生成縮略圖及給原始圖加水印的方法示例
本文實(shí)例講述了asp.net實(shí)現(xiàn)生成縮略圖及給原始圖加水印的方法。分享給大家供大家參考,具體如下:
using System.IO;
using System.Drawing.Imaging;
private void Button1_ServerClick(object sender, System.EventArgs e)
{
Graphics g=null;
System.Drawing.Image upimage=null;
System.Drawing.Image thumimg=null;
System.Drawing.Image simage=null;
Bitmap outputfile=null;
try
{
string extension = Path.GetExtension(File1.PostedFile.FileName).ToUpper();
string filename = DateTime.Now.ToString("yyyyMMddhhmmss");
string smallpath = Server.MapPath(".")+"/smallimg/";
string bigpath = Server.MapPath(".")+"/bigimg/";
int width,height,newwidth,newheight;
System.Drawing.Image.GetThumbnailImageAbort callb =new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
if(!Directory.Exists(smallpath))
Directory.CreateDirectory(smallpath);
if(!Directory.Exists(bigpath))
Directory.CreateDirectory(bigpath);
Stream upimgfile = File1.PostedFile.InputStream;
string simagefile = Server.MapPath("a8logo.jpg"); //要加水印的文件
simage=System.Drawing.Image.FromFile(simagefile);
upimage= System.Drawing.Image.FromStream(upimgfile); //上傳的圖片
width = upimage.Width;
height = upimage.Height;
if(width>height)
{
newwidth=200;
newheight =(int)((double)height/(double)width * (double)newwidth);
} else
{
newheight=200;
newwidth=(int)((double)width/(double)height * (double)newheight);
}
thumimg = upimage.GetThumbnailImage(newwidth,newheight,callb,IntPtr.Zero);
outputfile=new Bitmap(upimage);
g=Graphics.FromImage(outputfile);
g.DrawImage(simage,new Rectangle(upimage.Width-simage.Width,upimage.Height-simage.Height,upimage.Width,upimage.Height),0,0,upimage.Width,upimage.Height,GraphicsUnit.Pixel);
string newpath = bigpath + filename + extension; //原始圖路徑
string thumpath = smallpath + filename + extension; //縮略圖路徑
outputfile.Save(newpath);
thumimg.Save(thumpath);
outputfile.Dispose();
}
catch(Exception ex)
{
throw ex;
}
finally
{
if(g!=null)
g.Dispose();
if(thumimg!=null)
thumimg.Dispose();
if(upimage!=null)
upimage.Dispose();
if(simage!=null)
simage.Dispose();
}
}
public bool ThumbnailCallback()
{
return false;
}
更多關(guān)于asp.net相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《asp.net字符串操作技巧匯總》、《asp.net操作XML技巧總結(jié)》、《asp.net操作json技巧總結(jié)》、《asp.net文件操作技巧匯總》、《asp.net ajax技巧總結(jié)專題》及《asp.net緩存操作技巧總結(jié)》。
希望本文所述對大家asp.net程序設(shè)計(jì)有所幫助。
- ASP.NET簡單好用功能齊全圖片上傳工具類(水印、縮略圖、裁剪等)
- asp.net上傳圖片并作處理水印與縮略圖的實(shí)例代碼
- asp.net文件上傳功能(單文件,多文件,自定義生成縮略圖,水印)
- ASP.Net 上傳圖片并生成高清晰縮略圖
- asp.net圖片上傳生成縮略圖的注意事項(xiàng)
- asp.net 上傳圖片并同時生成縮略圖的代碼
- ASP.NET實(shí)現(xiàn)上傳圖片并生成縮略圖的方法
- Asp.net 文件上傳類(取得文件后綴名,保存文件,加入文字水印)
- asp.net 添加水印的代碼(已測試)
- asp.net下GDI+的一些常用應(yīng)用(水印,文字,圓角處理)技巧
- asp.net如何在圖片上加水印文字具體實(shí)現(xiàn)
相關(guān)文章
基于ABP框架實(shí)現(xiàn)RBAC(角色訪問控制)
本文詳細(xì)講解了基于ABP框架實(shí)現(xiàn)RBAC(角色訪問控制)的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12
MVC實(shí)現(xiàn)下拉框聯(lián)動效果(單選)
這篇文章主要為大家詳細(xì)介紹了MVC實(shí)現(xiàn)下拉框聯(lián)動效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
JQuery實(shí)現(xiàn)Repeater無刷新批量刪除(附后臺asp.net源碼)
JQuery實(shí)現(xiàn)Repeater無刷新批量刪除(附后臺asp.net源碼) ,學(xué)習(xí)jquery的朋友可以參考下。2011-09-09
.net framework中引進(jìn)的var對象類型聲明
安裝了vs 2008之后,在后臺代碼中,resharper插件對后臺所有局部變量進(jìn)行提示建議,顯示需要采用use implicitly typed local variable declaration,通過代碼修改建議2010-12-12
關(guān)于Asp.net頁面Page_Load被執(zhí)行兩次的問題分享
這篇文章介紹了關(guān)于Asp.net頁面Page_Load被執(zhí)行兩次的問題,有需要的朋友可以參考一下2013-09-09
asp.net通過js實(shí)現(xiàn)Cookie創(chuàng)建以及清除Cookie數(shù)組的代碼
asp.net Cookie創(chuàng)建以及清除Cookie數(shù)組2010-03-03
Asp.net core利用IIS在windows上進(jìn)行托管步驟詳解
這篇文章主要給大家介紹了關(guān)于Asp.net core利用IIS在windows上進(jìn)行托管步驟的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-03-03

