上传头像(上传图片).net 原码

<!--Page Language="C#" AutoEventWireup="true" CodeFile="Default1.aspx.cs" Inherits="Default4"--><!--CTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt-->

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default1.aspx.cs" Inherits="Default4" %>

<!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="FileUpimage" runat="server" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上传" />
<asp:Image ID="Image1" runat="server" ImageUrl="~/Fileupimage/b3.jpg" Visible="False" /></div>
</form>
</body>
</html>

后台代码: 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.IO; public partial class Default4 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } /// <summary> /// 周昕 2009-6-8 上传头像 /// </summary> protected void Button1_Click(object sender, EventArgs e) { //判断上传控件中是否有值 if (FileUpimage.HasFile == false) { Response.Write("<mce:script lanage='javascript'><!-- alert('请指定上传的头像') // --></mce:script>"); } else { string fType = FileUpimage.PostedFile.ContentType;//获取图像的类型 if (fType == "image/bmp" || fType == "image/gif" || fType == "image/pjpeg" || fType == "image/jpeg" || fType == "image/x-png") { //获取文件信息 FileInfo file = new FileInfo(FileUpimage.PostedFile.FileName); ///随机数据 Guid guid = Guid.NewGuid(); string stamp = guid.ToString("N"); //生成随机数 Random aa=new Random(); string number=aa.Next(10000).ToString(); //原始图片保存路径 string path = "~/Fileupimage/" + stamp + ".gif"; //缩略图保存路径 string spath = "~/Fileupimage/" + number + ".gif"; try { //原始图片保存 FileUpimage.SaveAs(Server.MapPath(path)); //缩略图保存 MakeThumbImage(Server.MapPath(path), Server.MapPath(spath), 200, 100); //给隐藏的图片控件赋值并显示 Image1.Visible = true; Image1.ImageUrl = spath; Response.Write("<mce:script lanage='javascript'><!-- alert('上传成功') // --></mce:script>"); } catch { Response.Write("<mce:script lanage='javascript'><!-- alert('上传失败') // --></mce:script>"); } } else { Response.Write("<mce:script lanage='javascript'><!-- alert('上传头像格式不正确') // --></mce:script>"); } } } ///<summary> ///创建日期:2009-6-08 ///创建人 :周昕 ///方法名称:MakeThumbImage ///内容摘要:生成缩略图 /// </summary> /// <param name="sPath">源图路径(物理路径)</param> /// <param name="stPath">缩略图路径(物理路径)</param> /// <param name="nWidth">缩略图宽度</param> /// <param name="nHeight">缩略图高度</param> private void MakeThumbImage(string sPath, string stPath, int nWidth, int nHeight) { System.Drawing.Image sImage = System.Drawing.Image.FromFile(sPath); int tw = nWidth; int th = nHeight; ///原始图片的宽度和高度 int sw = sImage.Width; int sh = sImage.Height; if (sw > tw) { sw = tw; } if (sh > th) { sh = th; } System.Drawing.Bitmap objPic, objNewPic; objPic = new System.Drawing.Bitmap(sPath); objNewPic = new System.Drawing.Bitmap(objPic, sw, sh); objNewPic.Save(stPath); sImage.Dispose(); objPic.Dispose(); objNewPic.Dispose(); } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值