asp.net上传图片问题

usingSystem;usingSystem.Configuration;usingSystem.Data;usingSystem.Linq;usingSystem.W... using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{

if (f1.FileName != "")
{
string file = f1.FileName; //得到文件名
f1.PostedFile.SaveAs(Server.MapPath("upfile/" + file)); //上传图片
Response.Write("<img src=upfile/" + file + " width='120' height='102' />");//输出图片
Response.Write("上传成功");
}
}

}
-------------------------------------

asp
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="f1" runat="server" />
                  
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="上传"" />
</div>
</form>
</body>
</html>

我需要在上传的时候限制图片大小 如果图片 宽大于800 就自动代称800 高大于1100 就自动改成1100

上面是我上传的代码~~~帮忙下下 修改图片大小
2楼

CS0246: 找不到类型或命名空间名称“QiXiu”(是否缺少 using 指令或程序集引用?)
展开
 我来答
86727515
推荐于2016-07-26 · TA获得超过201个赞
知道小有建树答主
回答量:227
采纳率:0%
帮助的人:142万
展开全部
//检查上传文件的格式是否有效
if (this.uploadfile.PostedFile.ContentType.ToLower().IndexOf("image") < 0)
{
Response.Write("<script> alert('上传图片格式无效'); </script>");
return;
}
//生成原图
Byte[] oFileByte = new byte[this.uploadfile.PostedFile.ContentLength];
System.IO.Stream oStream = this.uploadfile.PostedFile.InputStream;
System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream);

int oWidth = oImage.Width; //原图宽度
int oHeight = oImage.Height; //原图高度
int tWidth =800; //设置缩略图初始宽度 大于800自动取800
int tHeight =1000); //设置缩略图初始高度 大于1000自动取1000

//按比例计算出缩略图的宽度和高度
if (oWidth >= oHeight)
{
tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oWidth)));
}
else
{
tWidth = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(tHeight) / Convert.ToDouble(oHeight)));
}

//生成缩略原图
Bitmap tImage = new Bitmap(tWidth, tHeight);
Graphics g = Graphics.FromImage(tImage);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量插值法
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度
g.Clear(Color.Transparent); //清空画布并以透明背景色填充
g.DrawImage(oImage, new Rectangle(0, 0, tWidth, tHeight), new Rectangle(0, 0, oWidth, oHeight), GraphicsUnit.Pixel);

string fullFileName = this.uploadfile.PostedFile.FileName.ToString();
fullFileName = fullFileName.Remove(fullFileName.LastIndexOf('.'));
string filename = fullFileName.Substring(fullFileName.LastIndexOf("\\") + 1);

string strLocation = this.Server.MapPath(Page.Request.ApplicationPath) + "\\uploads\\"; //存储路径

}

Random r = new Random(); //防止重名
int i = (int)(r.NextDouble() * 10000);
if (!Directory.Exists(strLocation))
{
Directory.CreateDirectory(strLocation);
}

//string oFullName =strLocation +"x"+i.ToString() + filename+".jpg"; //保存原图的物理路径
string tFullName = strLocation + i.ToString() + filename + ".jpg"; //保存缩略图的物理路径

try
{
//以JPG格式保存图片
//oImage.Save(oFullName, System.Drawing.Imaging.ImageFormat.Jpeg);
tImage.Save(tFullName, System.Drawing.Imaging.ImageFormat.Jpeg);

}
catch (Exception ex)
{
throw ex;
}
finally
{
//释放资源
oImage.Dispose();
g.Dispose();
tImage.Dispose();
}
}
yjz_0209
2009-09-12 · 超过18用户采纳过TA的回答
知道答主
回答量:77
采纳率:0%
帮助的人:54万
展开全部
去网上找找 一大堆
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
我看这名能用不
2009-09-12 · 超过37用户采纳过TA的回答
知道小有建树答主
回答量:245
采纳率:0%
帮助的人:78万
展开全部
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.HtmlControls;
using System.Drawing;
using QiXiu.Model;
using QiXiu.DAL;

public partial class admin_AddNews : System.Web.UI.Page
{
string sm, bm;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["CurrentUser"] == null)
{
Response.Redirect("Login.aspx");
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
updata();
News news = new News();
news.Title = this.TextBox1.Text;
news.Content = this.FCKeditor1.Value;
news.Datetime = Convert.ToDateTime(DateTime.Now.ToString());
news.Imgurl = "images/small/" + sm;
if (NewService.AddNews(news))
{
Response.Write("<script>alert('添加成功');window.location='AddNews.aspx'</script>");
}
else
{
Response.Write("<script>alert('添加失败')</script>");
}
}
else
{
News news = new News();
news.Title = this.TextBox1.Text;
news.Content = this.FCKeditor1.Value;
news.Datetime = Convert.ToDateTime(DateTime.Now.ToString());
if (NewService.AddNews1(news))
{
Response.Write("<script>alert('添加成功');window.location='NewsList.aspx'</script>");
}
else
{
Response.Write("<script>alert('添加失败')</script>");
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("NewsList.aspx");
}

public void updata()
{
string strFileName = DateTime.Now.ToString("yyyyMMddHHmmss");
string name = FileUpload1.FileName;
string type = this.FileUpload1.PostedFile.ContentType; //获取文件类型;
string type2 = name.Substring(name.LastIndexOf(".") + 1); //获取文件的后缀名
string FilePath = Server.MapPath(@"..\images") + "\\" + strFileName + "." + type2;
string smallFilePath = Server.MapPath(@"..\images\small\") + "\\" + strFileName + "." + type2;
bm = strFileName + "." + type2;
sm = strFileName + "." + type2;
if (type2 == "jpg" || type2 == "gif" || type2 == "bmp" || type2 == "png")
{

FileUpload1.SaveAs(FilePath);
MakeThumbnail(FilePath, smallFilePath, 125, 125); //这的125是宽和高

}
else
{
Response.Write("<script>alert('上传的类型不对')</script>");
}
}

public void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);

int towidth = 0;
int toheight = 0;
if (originalImage.Width > width && originalImage.Height < height)
{
towidth = width;
toheight = originalImage.Height;
}

if (originalImage.Width < width && originalImage.Height > height)
{
towidth = originalImage.Width;
toheight = height;
}
if (originalImage.Width > width && originalImage.Height > height)
{
towidth = width;
toheight = height;
}
if (originalImage.Width < width && originalImage.Height < height)
{
towidth = originalImage.Width;
toheight = originalImage.Height;
}
int x = 0;//左上角的x坐标
int y = 0;//左上角的y坐标

//新建一个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, x, y, towidth, toheight);

try
{
//以jpg格式保存缩略图
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式