/// 
/// 按上傳圖片的時候
/// 
/// 
/// 
private void button1_click(object sender, system.eventargs e)
{
int towidth = 120; 
int toheight =90; 
int t=0;
page.validate();
if(!page.isvalid)
{
return;
}
istop="";s_filename="";
foreach(listitem each in stristop.items){if(each.selected){istop+=each.value;}}
//循環得到上傳的文件
system.web.httpfilecollection _files = system.web.httpcontext.current.request.files;
try
{
for ( system.int32 _ifile = 0; _ifile < _files.count; _ifile ++ ) 
{
// 檢查上傳文件是否為gif或jpg
system.web.httppostedfile _postedfile = _files[_ifile]; 
system.string _filename, _fileextension; 
_filename = system.io.path.getfilename(_postedfile.filename);
_fileextension = system.io.path.getextension(_filename);
if((_fileextension==".gif")||(_fileextension==".jpg"))
{ 
if(_postedfile.contentlength<=256000) //如果圖片的大小在規定的范圍內
{
if(t>0){istop="";}
classnews p=new classnews();
filename1=p.maketid("") + "-" + _ifile.tostring() +_fileextension; 
s_filename ="s_" + filename1;
_postedfile.saveas(server.mappath(@"../../upload/03")+"http://"+filename1);
string newfilename=p.drawword("ez2cn.com",server.mappath(@"../../upload/03"),filename1,0,0,0,0);p=null;
string file1=server.mappath(@"../../upload/03")+"http://"+newfilename;
string file2=server.mappath(@"../../upload/03")+"http://"+s_filename;
try
{
//p.makethumbnail(file1,file2,200,200,"w");
system.drawing.image originalimage = system.drawing.image.fromfile(file1); 
int x = 0; 
int y = 0; 
int ow = originalimage.width; 
int oh = originalimage.height; 
//下面得到縮略圖片的寬和高
//towidth = 120; 
//toheight = 90;
if(ow>=oh) //如果圖片的橫大于豎
{
towidth=120;
toheight=120*oh/ow;
if(toheight>90)
{
toheight = 90;
towidth = 90*towidth/toheight;
}
}
else if(ow {
toheight=90;
towidth = 90*ow/oh;
if(towidth>120)
{
towidth = 120;
toheight=120*toheight/towidth;
}
}
//結束得到寬高
//新建一個bmp圖片 
system.drawing.image bitmap = new system.drawing.bitmap(towidth,toheight); 
//新建一個畫板 
graphics g = system.drawing.graphics.fromimage(bitmap); 
//設置高質量插值法 
g.interpolationmode = system.drawing.drawing2d.interpolationmode.high; 
//設置高質量,低速度呈現平滑程度 
g.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality; 
//清空畫布并以透明背景色填充 
g.clear(color.transparent); 
//在指定位置并且按指定大小繪制原圖片的指定部分 
g.drawimage(originalimage, new rectangle(0, 0, towidth, toheight),new rectangle(x, y, ow,oh),graphicsunit.pixel); 
try 
{ 
//以jpg格式保存縮略圖 
bitmap.save(file2, system.drawing.imaging.imageformat.jpeg); 
} 
catch(system.exception ex) 
{ 
throw ex; 
} 
finally 
{ 
originalimage.dispose(); 
bitmap.dispose(); 
g.dispose(); 
} 
}
catch(system.exception ex) 
{ 
response.write(ex.tostring()); 
} 
filename1="03/"+newfilename; 
s_filename="03/"+s_filename; 
//把文件增加到數據庫中// 
add_photo();
t=t+1;
}
else
{
}
}
else //如果圖片格式不正確
{
}
}
}
catch
{ 
}
}
using system;
using system.drawing;
using system.drawing.imaging;
using system.io;
using system.text;
namespace system.inc.cs
{ 
/// 
/// 
public class classnews
{ int xp;
int yp;
int wsize;
public string maketid(string stri)
{  string stryear=system.datetime.now.year.tostring();
string strmonth=system.datetime.now.month.tostring(); 
string strday=system.datetime.now.day.tostring();
string strhour=system.datetime.now.hour.tostring();
string strminute=system.datetime.now.minute.tostring(); 
string strsecond=system.datetime.now.second.tostring(); 
return(stri+stryear+strmonth+strday+strhour+strminute+strsecond); 
} 
//文字水印
private bool drawposition( int imgwidth,int imgheight,int typecount)//imgwidth 圖片寬 imgheight圖片高 字體大小 
{
wsize=convert.toint16(imgwidth/40);
if (wsize+6<11){return(false);}
yp=imgheight-25-wsize;
xp=imgwidth-convert.toint16(wsize*typecount+20);
return(true);
}
/// 
/// 打上水印
/// 
/// 水印內容
/// 
/// 
/// 
/// 
/// 
/// 
/// 
public string drawword(string word,string filedir,string filename,int width,int height,int x,int y)
{ 
if((word==null)||(filename==null)){return(filename);}
if(x!=0){xp=x;}if(y!=0){yp=y;}
//
string path=filedir +"http://"+filename;
//
image image=system.drawing.image.fromfile(path); 
if(width==0){width=image.width;}
if(height==0){height=image.height;}
if(!(drawposition(width,height,13))){image.dispose();return(filename);};
//
bitmap bitmap=new bitmap(image); 
graphics graphic=system.drawing.graphics.fromimage(bitmap); 
font f=new font("comic sans ms",wsize); 
brush b=new solidbrush(color.fromargb(244,255,232));
graphic.drawstring(word,f,b,xp,yp);
graphic.dispose();
//
if(filename.tolower().indexof("gif")>-1) 
{
bitmap.save(filedir+"http://windowscar.com_"+filename,system.drawing.imaging.imageformat.gif);
}
else
{bitmap.save(filedir+"http://windowscar.com_"+filename,system.drawing.imaging.imageformat.jpeg);}
    f.dispose();
b.dispose();
image.dispose();
system.io.file.delete(path);
return("windowscar.com_"+filename);
}
//圖片水印
public string drawpicture(string pathsource,string path)
{
if((pathsource==null)||(path==null)){return(null);}
image image=system.drawing.image.fromfile(path);
image copyimage=system.drawing.image.fromfile(pathsource);
graphics graphic=graphics.fromimage(image);
graphic.drawimage(copyimage,new rectangle(image.width-copyimage.width,image.height-copyimage.height,copyimage.width,copyimage.height),0,0,copyimage.width,copyimage.height,graphicsunit.pixel);
graphic.dispose();
image.save(path);
image.dispose();
return(path);
}
  
public classnews()
{ xp=0;
yp=0;
//
// todo: 在此處添加構造函數邏輯
//
}
}
}
新聞熱點
疑難解答
圖片精選