asp.net上传图片(好)

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

时间: 2024-08-02 12:37:24

asp.net上传图片(好)的相关文章

asp.net 上传图片,并且显示在指定的地方。

问题描述 asp.net 上传图片,并且显示在指定的地方. 上传一张logo,需要的时候可以在某个页面更换首页logo.求大神给个具体的代码.谢谢 解决方案 http://www.cnblogs.com/yc-755909659/archive/2013/04/17/3026409.html 解决方案二: asp.net中图片的上传与显示asp.net上传图片并显示ASP.NET的图片上传和显示 解决方案三: 代码你可以到一些网站上去弄 在线 解决方案四: http://download.csd

Asp.Net上传图片同时生成高清晰缩略图_实用技巧

在asp.net中,上传图片功能或者是常用的,生成缩略图也是常用的.baidu或者google,c#的方法也是很多的,但是一用却发现缩略图不清晰啊,缩略图片太大之类的事情,下面是我在处理图片上的代码,效果不错,所以拿出来分享,(效果能达到一些绘图软件的效果) 代码如下: /// <summary> /// asp.net上传图片并生成缩略图 /// </summary> /// <param name="upImage">HtmlInputFile控

asp.net上传图片并作处理水印与缩略图的实例代码_实用技巧

方法类: 复制代码 代码如下: upFileClass.cs using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlC

大家有没有ASP.NET上传图片给图片加水印例子没有啊

问题描述 大家有没有ASP.NET上传图片给图片加水印例子没有啊?在网上没找到呢?有例子可以给我发一个嘛谢谢.273752751@qq.com 解决方案 解决方案二:usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls.WebParts;usingSystem.

asp.net上传图片

问题描述 不知道怎么的,只在一个文件夹里才能读取图片,而在其他文件夹却找不到图片.但是路径是对的.却弹出来的错却说找不大该文件.真是晕啊,不知道哪里出错了,请高手指点一二.谢了......//获取图片的路径stringstrFilePath=this.fileTest.PostedFile.FileName.ToString();//分割图片的路径与名字varuploadFilePath=this.fileTest.PostedFile.FileName.Split(newstring[]{"\

asp防止上传图片木马原理解析

首先判断文件大小: if file.filesize<10 then Response.Write("<script>alert('您没有选择上传文件')</script>") Response.Write("<script>history.go(-1)</script>") Response.End() end if 将文件上传到服务器后,判断用户文件中的危险操作字符: set MyFile = server.

asp防止上传图片木马原理解析_木马相关

首先判断文件大小: if file.filesize<10 then Response.Write("<script>alert('您没有选择上传文件')</script>") Response.Write("<script>history.go(-1)</script>") Response.End() end if 将文件上传到服务器后,判断用户文件中的危险操作字符: set MyFile = server.

asp.net上传图片并同时生成缩略图

asp.net|上传|上传图片|缩略图 <script language="VB" runat="server">Sub UploadFile(sender As Object, e As EventArgs) If FileUp.PostedFile.ContentLength = 0 ThenFileInfo.Visible = FalseExit SubElseFileInfo.Visible = TrueFDisplay1.Visible = T

Asp.net上传图片同时生成文字水印图,图片水印图,缩略图

  .aspx <body>     <form id="form1" runat="server">     <div>         <asp:FileUpload ID="FileUpload1" runat="server" /><br />         <asp:Button ID="Button1" runat="