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();  
    } 
复制代码

 

 

posted @   dreamfly_cc  阅读(494)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示