首页 > 编程 > .NET > 正文

asp.net上传图片(好)

2024-07-10 13:06:54
字体:
来源:转载
供稿:网友

///
/// 按上传图片的时候
///
///
///
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")+"//"+filename1);
string newfilename=p.drawword("ez2cn.com",server.mappath(@"../../upload/03"),filename1,0,0,0,0);p=null;

string file1=server.mappath(@"../../upload/03")+"//"+newfilename;
string file2=server.mappath(@"../../upload/03")+"//"+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 +"//"+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+"//windowscar.com_"+filename,system.drawing.imaging.imageformat.gif);
}
else
{bitmap.save(filedir+"//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: 在此处添加构造函数逻辑
//
}
}
}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表