C# 上传图片

界面:

前台所用控件:一个FileUpload用来浏览,一个Button用来上传,一个Image用来显示上传的图片

前台代码:

<div>
        <asp:FileUpload ID="FileUpimage" runat="server" />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上传" />
        <asp:Image ID="Image1" runat="server" ImageUrl="~/image/save.png" Visible="False" />
</div>

后台代码:

        /// <summary>
        /// 上传
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            //判断上传控件中是否有值  
            if (FileUpimage.HasFile == false)  
            {  
                Response.Write("<script>alert('请指定上传的头像')</script>");
            }  
            else  
            {  
                string fType = FileUpimage.PostedFile.ContentType;//获取图像的类型  
                if (fType == "image/bmp" || fType == "image/gif" || fType == "image/pjpeg" || fType == "image/jpeg" || fType == "image/x-png" || fType == "image/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 = "~/Files/" + stamp + ".gif";  
                    //缩略图保存路径  
                    string spath = "~/Files/" + 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("<script>alert('上传成功')</script>");
                    }  
                    catch  
                    {  

                        Response.Write("<script>alert('上传失败')</script>");
                    }  
                }  
                else  
                {   
                    Response.Write("<script>alert('上传头像格式不正确')</script>");
                }  
            }  
        }
    ///<summary>  
    ///方法名称: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
        objNewPic.Save(stPath);  
        sImage.Dispose();  //释放资源
        objPic.Dispose();  
        objNewPic.Dispose();  
    } 
天再高,踮起脚尖就能更接近阳光
原文地址:https://www.cnblogs.com/dreamflycc/p/2874897.html