locked
image height and width from fileupload and textbox field RRS feed

  • Question

  • User2069481228 posted

     i tried to get image height and width from TextBox id and FileUpload id using folowing code,

    but always display "Image size is not right" eventhough size is right(912*90). Please let me know the

    information to check the image height and width from TextBox id and FileUpload id.

    if ((uxBannerURL.Height != 90) && (uxBannerURL.Width != 912))

    {

    uxMessage.Text = "Image size is not right";

    uxMessage.Visible = true;

    }

    else

    {

    uxMessage.Text = "Image size right";

    uxMessage.Visible = true;

    }

    if ((uxUpBanner.Height != 90) && (uxUpBanner.Width != 912))

    {

    uxMessage.Text = "Image size is not right";

    uxMessage.Visible = true;

    }

    else

    {

    uxMessage.Text = "Image size is right";

    uxMessage.Visible = true;

    }

     

    <td align="right" valign="top" class="sidepane"><strong>Banner</strong>: </td>

     

    <td align="left"> <asp:FileUpload runat="server" ID="uxUpBanner"/><br />Dimensions:&nbsp;&nbsp;912 X 90,&nbsp;&nbsp;.jpg,&nbsp;.jpeg,&nbsp;.gif,&nbsp;.png&nbsp;</td></tr>

     

    <tr><td style="color:Blue; font-size:20px" align="center" class="sidepane" valign="top">

    <strong>OR</strong></td></tr>

     

    <tr><strong>Banner url</strong>:<td align="left" valign="top"><asp:TextBox ID="uxBannerURL" runat="server" ></asp:TextBox>

    <br /><br /><asp:Label runat="server" ID="uxMessage"></asp:Label></td>

     

     

    please help me as soon as possible.

    thanks,

    Mohfeza

    Wednesday, July 9, 2008 8:45 PM

Answers

  • User-1763611275 posted

    Hi

    try this example to allow user upload image with height and width of 100 px

    .ASPX

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default-CS.aspx.cs" Inherits="DefaultCs" %>
    <!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>Uploading Images</title>
    </head>
    <body>
      <form id="form1" runat="server">
        <div>
            <strong>Allow user to upload Image with type jpg , jpeg , gif or bmp<br />
                and with High 100 px and Width 100 px<br />
                we put height and width of image in web.config</strong><br />
            <br />
          <asp:FileUpload ID="FileUpload1" runat="server" />
            <br />
            <br />
          <asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Upload File" Font-Bold="True" />
            <br />
          <br />
          <asp:RequiredFieldValidator ID="valFileUpload1" ControlToValidate="FileUpload1" ErrorMessage="You must select a file first." runat="server" Display="Dynamic" Font-Bold="True" /><br />
            <br />
          <asp:CustomValidator ID="valInvalidFile" runat="server" ErrorMessage="The file you uploaded doesn't appear to be a valid image." Display="Dynamic" Font-Bold="True"></asp:CustomValidator>
            <br />
            <br />
          <asp:CustomValidator ID="valInvalidDimensions" runat="server" ErrorMessage="The image you uploaded has incorrect dimensions. Please select a file with a height of {0}px and a width of {1}px." Display="Dynamic" EnableViewState="false" Font-Bold="True"></asp:CustomValidator>
            <br />
            <br />
          <asp:Label ID="lblSucces" runat="server" Text="The file you uploaded has been saved to disk successfully." Visible="False" EnableViewState="False" Font-Bold="True"></asp:Label>
        </div>
      </form>
    </body>
    </html>
    

     .ASPX.CS

    using System;
    using System.Configuration;
    using System.IO;
    
    public partial class DefaultCs : System.Web.UI.Page
    {
    
      private int height;
      private int width;
    
      protected void Page_Load(object sender, EventArgs e)
      {
        if (Page.IsPostBack)
        {
          height = Convert.ToInt32(ConfigurationManager.AppSettings.Get("RequiredHeight"));
          width = Convert.ToInt32(ConfigurationManager.AppSettings.Get("RequiredWidth"));
        }
      }
    
      protected void btnUpload_Click(object sender, EventArgs e)
      {
        Page.Validate();
        if (Page.IsValid)
        {
          if (FileUpload1.HasFile)
          {
            string extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
            switch (extension.ToLower())
            {
              // Only allow uploads that look like images.
              case ".jpg":
              case ".jpeg":
              case ".gif":
              case ".bmp":
                try
                {
                  if (ValidateFileDimensions())
                  {
                    string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
                    string saveAsName = Path.Combine(Server.MapPath("~/Uploads/"), fileName);
                    FileUpload1.PostedFile.SaveAs(saveAsName);
                    lblSucces.Visible = true;
                  }
                  else
                  {
                    valInvalidDimensions.IsValid = false;
                    valInvalidDimensions.ErrorMessage = String.Format(valInvalidDimensions.ErrorMessage, height, width);
                  }
                }
                catch
                {
                  // Unable to read the file dimensions. The uploaded file is probably not an image.
                  valInvalidFile.IsValid = false;
                }
                break;
    
              default: // The uploaded file has an incorrect extension
                valInvalidFile.IsValid = false;
                break;
            }
          }
        }
      }
    
      /// <summary>
      /// Validates an uploaded image by looking at its dimensions.
      /// The image is considered valid when it has the height and width as specified in the web.config file.
      /// </summary>
      /// <returns>True when the uploaded image has the dimensions specified in the web.config file, and false otherwise.</returns>
      public bool ValidateFileDimensions()
      {
        using (System.Drawing.Image myImage = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream))
        {
          return (myImage.Height == height && myImage.Width == width);
        }
      }
    }
    

     

    and in web.config:

    <appSettings>
        <!--Image Height and Width-->
        <add key="RequiredHeight" value="100"/>
        <add key="RequiredWidth" value="100"/>
      </appSettings>

    Good Luck

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 12, 2008 1:15 PM

All replies

  • User356519686 posted

    Bcause, uxBannerURL is not an imagefile , it is the ID of the TextBox.

    <asp:TextBox ID="uxBannerURL" runat="server" ></asp:TextBox>

    Get the imageFile along with the path and then try

    If you need more info, please let us know.

    Thanks.

    Thursday, July 10, 2008 12:05 AM
  • User-1763611275 posted

    Hi

    try this example to allow user upload image with height and width of 100 px

    .ASPX

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default-CS.aspx.cs" Inherits="DefaultCs" %>
    <!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>Uploading Images</title>
    </head>
    <body>
      <form id="form1" runat="server">
        <div>
            <strong>Allow user to upload Image with type jpg , jpeg , gif or bmp<br />
                and with High 100 px and Width 100 px<br />
                we put height and width of image in web.config</strong><br />
            <br />
          <asp:FileUpload ID="FileUpload1" runat="server" />
            <br />
            <br />
          <asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Upload File" Font-Bold="True" />
            <br />
          <br />
          <asp:RequiredFieldValidator ID="valFileUpload1" ControlToValidate="FileUpload1" ErrorMessage="You must select a file first." runat="server" Display="Dynamic" Font-Bold="True" /><br />
            <br />
          <asp:CustomValidator ID="valInvalidFile" runat="server" ErrorMessage="The file you uploaded doesn't appear to be a valid image." Display="Dynamic" Font-Bold="True"></asp:CustomValidator>
            <br />
            <br />
          <asp:CustomValidator ID="valInvalidDimensions" runat="server" ErrorMessage="The image you uploaded has incorrect dimensions. Please select a file with a height of {0}px and a width of {1}px." Display="Dynamic" EnableViewState="false" Font-Bold="True"></asp:CustomValidator>
            <br />
            <br />
          <asp:Label ID="lblSucces" runat="server" Text="The file you uploaded has been saved to disk successfully." Visible="False" EnableViewState="False" Font-Bold="True"></asp:Label>
        </div>
      </form>
    </body>
    </html>
    

     .ASPX.CS

    using System;
    using System.Configuration;
    using System.IO;
    
    public partial class DefaultCs : System.Web.UI.Page
    {
    
      private int height;
      private int width;
    
      protected void Page_Load(object sender, EventArgs e)
      {
        if (Page.IsPostBack)
        {
          height = Convert.ToInt32(ConfigurationManager.AppSettings.Get("RequiredHeight"));
          width = Convert.ToInt32(ConfigurationManager.AppSettings.Get("RequiredWidth"));
        }
      }
    
      protected void btnUpload_Click(object sender, EventArgs e)
      {
        Page.Validate();
        if (Page.IsValid)
        {
          if (FileUpload1.HasFile)
          {
            string extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
            switch (extension.ToLower())
            {
              // Only allow uploads that look like images.
              case ".jpg":
              case ".jpeg":
              case ".gif":
              case ".bmp":
                try
                {
                  if (ValidateFileDimensions())
                  {
                    string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
                    string saveAsName = Path.Combine(Server.MapPath("~/Uploads/"), fileName);
                    FileUpload1.PostedFile.SaveAs(saveAsName);
                    lblSucces.Visible = true;
                  }
                  else
                  {
                    valInvalidDimensions.IsValid = false;
                    valInvalidDimensions.ErrorMessage = String.Format(valInvalidDimensions.ErrorMessage, height, width);
                  }
                }
                catch
                {
                  // Unable to read the file dimensions. The uploaded file is probably not an image.
                  valInvalidFile.IsValid = false;
                }
                break;
    
              default: // The uploaded file has an incorrect extension
                valInvalidFile.IsValid = false;
                break;
            }
          }
        }
      }
    
      /// <summary>
      /// Validates an uploaded image by looking at its dimensions.
      /// The image is considered valid when it has the height and width as specified in the web.config file.
      /// </summary>
      /// <returns>True when the uploaded image has the dimensions specified in the web.config file, and false otherwise.</returns>
      public bool ValidateFileDimensions()
      {
        using (System.Drawing.Image myImage = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream))
        {
          return (myImage.Height == height && myImage.Width == width);
        }
      }
    }
    

     

    and in web.config:

    <appSettings>
        <!--Image Height and Width-->
        <add key="RequiredHeight" value="100"/>
        <add key="RequiredWidth" value="100"/>
      </appSettings>

    Good Luck

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 12, 2008 1:15 PM