.net中fileupload控件上传图片设置宽和高
在aspx.net的C#中,fileupload控件上传图片时如何自动设置图片的宽和高。例如:设置上传的图片固定的宽和高,或按比例缩放图片的宽和高。请写出有效的解决方案与...
在aspx .net的C#中,fileupload控件上传图片时如何自动设置图片的宽和高。例如:设置上传的图片固定的宽和高,或按比例缩放图片的宽和高。请写出有效的解决方案与代码,功能能实现者有高悬赏,谢谢。
展开
3个回答
展开全部
public string UpImage(System.Web.UI.WebControls.FileUpload FileUpload1, string ImageUrl)
{
if (FileUpload1.PostedFile.ContentLength == 0)
{
//ShowUpLoadFile.InnerHtml = "上传失败或文件不存在!";
return "上传失败或文件不存在!";
}
else
{
//获取文件名
string[] temp = FileUpload1.PostedFile.FileName.Split('\\');
string FileName = temp[temp.Length - 1];
string exName;
//截取图片的后缀名
exName = FileUpload1.PostedFile.ContentType;
exName = exName.ToLower();
//exName = FileName.Substring(FileName.LastIndexOf(".") + 1).ToUpper();
if (exName == "image/bmp" || exName == "image/pjpeg" || exName == "image/gif")
{
//判断图片是否大于200k
if (FileUpload1.PostedFile.ContentLength > 204800)
{
return "对不起,你上传的图片太大,请转换后上传";
//Script.ShuChuScript("对不起,你上传的图片太大,请转换后上传");
}
}
else
{
//Script.ShuChuScript("对不起,请选择格式为 JPG 、 BMP 、 GIF 的图片!");
return "对不起,请选择格式为 JPG 、 BMP 、 GIF 的图片!";
}
//根据时间生成图片名
string SaveName;
SaveName = DateTime.Now.ToString("yyyyMMddhhmmssfff");
string fn;
fn = FileName;
//图片名加上图片后缀名
string strImageName;
strImageName = SaveName + fn.Substring(fn.LastIndexOf("."));
//保存文件
FileUpload1.PostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath(ImageUrl + strImageName));
//显示上传结果
//tubianname.Text = strImageName;
//tubian12.InnerHtml = @"<img src='../chanbininage/" + tubianname.Text + "' height ='118' width ='174'>";
//Script.ShuChuScript("图片上传成功!");
return strImageName;
}
}
/// <summary>
/// 为图片生成缩略图
/// </summary>
/// <param name="phyPath">原图片的路径</param>
/// <param name="width">缩略图宽</param>
/// <param name="height">缩略图高</param>
/// <returns></returns>
public static string GetThumbnail(string phyPath, int width, int height)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(phyPath);
int orignWidth = image.Width; //原图尺寸
int orignHeight = image.Height;
float toWidth = (float)width; //缩略图目标尺寸
float toHeight = (float)height;
float radio = (float)orignWidth / (float)orignHeight; //宽高比例
if (orignWidth >= orignHeight)
{ //宽度不变
toHeight = toWidth / radio;
}
else
{ //高度不变
toWidth = toHeight * radio;
}
System.Drawing.Image thumbImage = image.GetThumbnailImage(Convert.ToInt32(toWidth), Convert.ToInt32(toHeight), null, IntPtr.Zero);
string folder = Path.GetDirectoryName(phyPath);
string fileName = "\\s_" + Path.GetFileName(phyPath);
string savePath = folder + fileName;
try
{
thumbImage.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
finally
{
image.Dispose();
thumbImage.Dispose();
}
return savePath;
}
试一下这个
{
if (FileUpload1.PostedFile.ContentLength == 0)
{
//ShowUpLoadFile.InnerHtml = "上传失败或文件不存在!";
return "上传失败或文件不存在!";
}
else
{
//获取文件名
string[] temp = FileUpload1.PostedFile.FileName.Split('\\');
string FileName = temp[temp.Length - 1];
string exName;
//截取图片的后缀名
exName = FileUpload1.PostedFile.ContentType;
exName = exName.ToLower();
//exName = FileName.Substring(FileName.LastIndexOf(".") + 1).ToUpper();
if (exName == "image/bmp" || exName == "image/pjpeg" || exName == "image/gif")
{
//判断图片是否大于200k
if (FileUpload1.PostedFile.ContentLength > 204800)
{
return "对不起,你上传的图片太大,请转换后上传";
//Script.ShuChuScript("对不起,你上传的图片太大,请转换后上传");
}
}
else
{
//Script.ShuChuScript("对不起,请选择格式为 JPG 、 BMP 、 GIF 的图片!");
return "对不起,请选择格式为 JPG 、 BMP 、 GIF 的图片!";
}
//根据时间生成图片名
string SaveName;
SaveName = DateTime.Now.ToString("yyyyMMddhhmmssfff");
string fn;
fn = FileName;
//图片名加上图片后缀名
string strImageName;
strImageName = SaveName + fn.Substring(fn.LastIndexOf("."));
//保存文件
FileUpload1.PostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath(ImageUrl + strImageName));
//显示上传结果
//tubianname.Text = strImageName;
//tubian12.InnerHtml = @"<img src='../chanbininage/" + tubianname.Text + "' height ='118' width ='174'>";
//Script.ShuChuScript("图片上传成功!");
return strImageName;
}
}
/// <summary>
/// 为图片生成缩略图
/// </summary>
/// <param name="phyPath">原图片的路径</param>
/// <param name="width">缩略图宽</param>
/// <param name="height">缩略图高</param>
/// <returns></returns>
public static string GetThumbnail(string phyPath, int width, int height)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(phyPath);
int orignWidth = image.Width; //原图尺寸
int orignHeight = image.Height;
float toWidth = (float)width; //缩略图目标尺寸
float toHeight = (float)height;
float radio = (float)orignWidth / (float)orignHeight; //宽高比例
if (orignWidth >= orignHeight)
{ //宽度不变
toHeight = toWidth / radio;
}
else
{ //高度不变
toWidth = toHeight * radio;
}
System.Drawing.Image thumbImage = image.GetThumbnailImage(Convert.ToInt32(toWidth), Convert.ToInt32(toHeight), null, IntPtr.Zero);
string folder = Path.GetDirectoryName(phyPath);
string fileName = "\\s_" + Path.GetFileName(phyPath);
string savePath = folder + fileName;
try
{
thumbImage.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
finally
{
image.Dispose();
thumbImage.Dispose();
}
return savePath;
}
试一下这个
追问
请问一下:
string folder = Path.GetDirectoryName(phyPath);
string fileName = "\\s_" + Path.GetFileName(phyPath);
这Path和"\\s_"是什么啊?
这样调用 Image_HW.GetThumbnail("C:\\0909_2.jpg", 200, 150);图片尺寸没有改变。
展开全部
这只是能限制你上传的大小
string hig = image.Height.ToString();
string wid = image.Width.ToString();
if (int.Parse(hig) > 100 || int.Parse(wid) > 100)
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('上传的图片太大!');</script>");
}
else
{
Image1.ImageUrl = strBaseLocation + Temp1 + rightName;
TextBox16.Text = Image1.ImageUrl;
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('上传成功!');</script>");
}
string hig = image.Height.ToString();
string wid = image.Width.ToString();
if (int.Parse(hig) > 100 || int.Parse(wid) > 100)
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('上传的图片太大!');</script>");
}
else
{
Image1.ImageUrl = strBaseLocation + Temp1 + rightName;
TextBox16.Text = Image1.ImageUrl;
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('上传成功!');</script>");
}
追问
.net中fileupload上传图片自动设置图片的宽和高吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//这是按比例缩放图片
//页面:
//resizePic.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="resizePic.aspx.cs" Inherits="resizePic" %>
<!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>
</div>
</form>
</body>
</html>
//后台代码:
//resizePic.aspx.cs
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;
public partial class resizePic : System.Web.UI.Page
{
string picPath;
int w;
int h;
protected void Page_Load(object sender, EventArgs e)
{
try
{
picPath = Request.QueryString["src"];
w = int.Parse(Request.QueryString["w"]);
h = int.Parse(Request.QueryString["h"]);
}
catch
{
}
//Response.Write(Server.MapPath(picPath));
ReDraw();
}
private void ReDraw()
{
System.Drawing.Image image = null;
Bitmap bmp = null;
System.IO.MemoryStream ms = null;
try
{
if (picPath.Equals("") || picPath == null)
{
image = System.Drawing.Image.FromFile(Server.MapPath("images/picnotfound.jpg"));
}
else
{
image = System.Drawing.Image.FromFile(Server.MapPath(picPath));
}
Size size = ProportionSize(image.Width, image.Height, w, h);
bmp = new Bitmap(image, size.Width, size.Height);
ms = new System.IO.MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(ms.ToArray());
}
finally
{
if (image != null)
{
image.Dispose();
bmp.Dispose();
ms.Dispose();
}
}
}
/// <summary>
/// 按指定的容器相框大小原比例缩小图片
/// </summary>
/// <param name="width">原图宽</param>
/// <param name="height">原图高</param>
/// <param name="afterChange_W">缩小后图宽</param>
/// <param name="afterChange_H">缩小后图高</param>
private Size ProportionSize(float ori_W, float ori_H, int frame_W, int frame_H)
{
int icon_w = frame_W;
int icon_h = frame_H;
//原图宽小于高
if (ori_W < ori_H)
{
if (frame_W > frame_H)
//缩略图宽 = 原图宽 * 相框高 / 原图高
icon_w = (int)(ori_W * frame_H / ori_H);
else if (frame_W < frame_H)
//缩略图高 = 相框宽 * 原图高 / 原图宽
icon_h = (int)(frame_W * ori_H / ori_W);
}
//原图宽大于高
else if (ori_W > ori_H)
{
//相框宽大于高
if (frame_W > frame_H)
//缩略图高 = 相框宽 * 原图高 / 原图宽
icon_h = (int)(frame_W * ori_H / ori_W);
//相框宽小于高
else if (frame_W < frame_H)
//缩略图宽 = 原图宽 * 相框高 / 原图高
icon_w = (int)(ori_W * frame_H / ori_H);
}
return new Size(icon_w, icon_h);
}
}
//调用:
<img src="resizePic.aspx?src=图片路径&w=229&h=180" width="229" height="180"/>
//你可以根据自己的需要可以提取其中的算法出来
//页面:
//resizePic.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="resizePic.aspx.cs" Inherits="resizePic" %>
<!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>
</div>
</form>
</body>
</html>
//后台代码:
//resizePic.aspx.cs
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;
public partial class resizePic : System.Web.UI.Page
{
string picPath;
int w;
int h;
protected void Page_Load(object sender, EventArgs e)
{
try
{
picPath = Request.QueryString["src"];
w = int.Parse(Request.QueryString["w"]);
h = int.Parse(Request.QueryString["h"]);
}
catch
{
}
//Response.Write(Server.MapPath(picPath));
ReDraw();
}
private void ReDraw()
{
System.Drawing.Image image = null;
Bitmap bmp = null;
System.IO.MemoryStream ms = null;
try
{
if (picPath.Equals("") || picPath == null)
{
image = System.Drawing.Image.FromFile(Server.MapPath("images/picnotfound.jpg"));
}
else
{
image = System.Drawing.Image.FromFile(Server.MapPath(picPath));
}
Size size = ProportionSize(image.Width, image.Height, w, h);
bmp = new Bitmap(image, size.Width, size.Height);
ms = new System.IO.MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(ms.ToArray());
}
finally
{
if (image != null)
{
image.Dispose();
bmp.Dispose();
ms.Dispose();
}
}
}
/// <summary>
/// 按指定的容器相框大小原比例缩小图片
/// </summary>
/// <param name="width">原图宽</param>
/// <param name="height">原图高</param>
/// <param name="afterChange_W">缩小后图宽</param>
/// <param name="afterChange_H">缩小后图高</param>
private Size ProportionSize(float ori_W, float ori_H, int frame_W, int frame_H)
{
int icon_w = frame_W;
int icon_h = frame_H;
//原图宽小于高
if (ori_W < ori_H)
{
if (frame_W > frame_H)
//缩略图宽 = 原图宽 * 相框高 / 原图高
icon_w = (int)(ori_W * frame_H / ori_H);
else if (frame_W < frame_H)
//缩略图高 = 相框宽 * 原图高 / 原图宽
icon_h = (int)(frame_W * ori_H / ori_W);
}
//原图宽大于高
else if (ori_W > ori_H)
{
//相框宽大于高
if (frame_W > frame_H)
//缩略图高 = 相框宽 * 原图高 / 原图宽
icon_h = (int)(frame_W * ori_H / ori_W);
//相框宽小于高
else if (frame_W < frame_H)
//缩略图宽 = 原图宽 * 相框高 / 原图高
icon_w = (int)(ori_W * frame_H / ori_H);
}
return new Size(icon_w, icon_h);
}
}
//调用:
<img src="resizePic.aspx?src=图片路径&w=229&h=180" width="229" height="180"/>
//你可以根据自己的需要可以提取其中的算法出来
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |