asp.net上傳圖片(好)

來源:互聯網
上載者:User
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")+"\\"+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: 在此處添加建構函式邏輯
//
}
}
}



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.