日期:2011-08-18  浏览次数:20685 次

///
/// 按上传图片的时候
///
///
///
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();
strin