图像高度宽度验证和弹出窗口

本文关键字:窗口 验证 高度 图像 | 更新日期: 2023-09-27 17:55:15

我有一个带有FileUpload控件的.aspx页面。 我想实现将图像上传到网站。 但在此之前,我需要在以下条件下检查图像高度和宽度:

  • 如果图像高度为>768 或宽度为>1024,则显示弹出消息以继续...。(是/否)

  • 如果图像高度为 <768 或宽度为 <1024,则显示一条弹出消息以继续...。(是/否)

    所以,到目前为止,我已经完成了图像上传代码,但是如何实现这一点? 任何类型的帮助/建议将不胜感激。

           <asp:RegularExpressionValidator ID="revUploaderMainPopup" runat="server" ControlToValidate="UploaderMainPopup"
                            ErrorMessage="*" ValidationGroup="MainPopUploadvlg" ToolTip="Only .jpg, .bmp, .png are valid."
                            ForeColor="Red" Display="Dynamic"  ValidationExpression="(.*'.([Jj][Pp][Gg])|.*'.([Bb][Mm][Pp])|.*'.([pP][nN][gG])$)">
           </asp:RegularExpressionValidator> 
    

图像高度宽度验证和弹出窗口

如果不使用自定义验证程序,您将无法执行此操作。而且肯定必须是服务器端。客户端代码将需要Javascript显示"是或否"对话框(这可以通过javascript: confirm('yes?');对话框来完成。

此外,如果您想保持 UI 看起来像上传从未发生过并且您完全在客户端验证它(由于部分回发),则需要一些 ajax。

请参阅以获取图像的尺寸,而无需读取整个文件,以便您可以将该 ajax 请求的几毫秒推开。

祝你好运!

请尝试以下操作

        using (System.Drawing.Image image = System.Drawing.Bitmap.FromStream(fs))
        {
            int iWidth = (int)Math.Round((decimal)image.Width);
            int iHeight = (int)Math.Round((decimal)image.Height);
           if(iWidth > 1024 || iHeight > 768)
           {
                // here you can throw your message
           }
        }

问候

我通过以下方式完成了:我取了两个变量(图像和字符串)一个带有弹出窗口的div,具有拖曳按钮。验证图像后,我正在将display:block设置为来自服务器的div。
单击"你们"按钮,我正在保存它。

 <div id="dvPopup" class="popup" runat="server" style="display: none">
    <asp:Panel ID="pnlpopup" runat="server" Style="display: block; position: absolute; width:400px;
        margin: auto" CssClass="modalConfirmation">
        <div style="width: 400px; height: 30px;" class="MessageHeaderError">
            <div class="modalHeader">
                Confirmation
            </div>
        </div>
        <br />
        <div style="color: Black">
            <span runat="server" id="spnMessge" style="font-weight:bold">Picture is smaller than 1024 x 768 and will be stretched.</span>
        </div>
        <div class="small">
        </div>
        <div>
            <div style="display: table-cell; height: 40px; padding-left: 80px; vertical-align: middle;
                width: 80px;">
                <asp:ImageButton ID="btnYes" ImageUrl="~/images/correct.png" runat="server" AlternateText="YES"
                    ToolTip="Yes" OnClick="btnYes_Click" />
            </div>
            <div style="display: table-cell; height: 40px; vertical-align: middle;">
                <asp:ImageButton ID="btnNo" ImageUrl="~/images/delete.png" runat="server" AlternateText="NO"
                    ToolTip="No" Style="margin-left: 100px;" OnClick="btnNo_Click" />
            </div>

.cs FIE 中:

 private bool ValidateImageSize(System.Drawing.Image imgPopup, string strSource)
    {
        //Messure height & width and show popup;
        if ((strSource == "MainPopup") && (imgPopup.Height > 768 || imgPopup.Width > 1024))
        {
            return true;
        }
        else if (strSource == "SmallPopup" && imgPopup.Height > 300 || imgPopup.Width > 400)
        {
            return true;
        }
        return false;
    }