asp.net上传图片(好)

80酷酷网    80kuku.com

  asp.net|上传|上传图片

///
/// 按上传图片的时候
///
///
///
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")+");
string newfilename=p.DrawWord("Ez2cn.Com",Server.MapPath("../../upload/03"),filename1,0,0,0,0);p=null;

string file1=Server.MapPath("../../upload/03")+";
string file2=Server.MapPath("../../upload/03")+";

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 +";
//
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+");
}
else
{bitmap.Save(filedir+");}

    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: 在此处添加构造函数逻辑
//
}
}
}

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: