在後端取得圖片的寬及高

摘要:在後端取得圖片的寬及高

第一次發文 , 要讓各位前輩見笑了

 

因為工作上的需要 , 還得顧及到 Javascript 不能用或停用的窘境

就沒辦法使用喵大所提供的方法來實作 ( 詳情請看這篇)

因此 , 自己試著在後端作一些圖片的基本處理

小小的範例如下

aspx

<input type="file" id="upfile" runat="server" name="file1" /><asp:Button 
                    ID="btnUpload" runat="server" Text="上傳" onclick="GetPicWnH" />

 

C#

    protected void GetPicWnH(object sender, EventArgs e)
    {
        if (Request.Files.Count > 0)
        {
            
            if (!Object.Equals(UploadedFile.FromHttpPostedFile(Request.Files[upfile.UniqueID]), null))
            {
                UploadedFile file = UploadedFile.FromHttpPostedFile(Request.Files[upfile.UniqueID]);
                String targetName = file.GetName();
                String targetFolder = Server.MapPath("/Resource/img");
                String fullPath = Path.Combine(targetFolder, targetName);
                file.SaveAs(fullPath, true);
                System.Drawing.Image img = System.Drawing.Image.FromStream(file.InputStream);
                ViewState["uppath"] = String.Format("/Resource/img/{0}", targetName);
                if (img.Width < 94)
                {
                    imgLinkPic.Width = img.Width;
                }
                if (img.Height < 51)
                {
                    imgLinkPic.Height = img.Height;
                }
                img.Dispose();
                imgLinkPic.ImageUrl = (String)ViewState["uppath"];
            }
        }
    }

在下的拙作 , 請各位前輩指教了

===================

大家好  , 我叫芋宅宅

我很菜 , 請各位前輩指教