How to validate height and width of image uploaded using RadAsyncUpload

6 posts, 0 answers
  1. Sweta
    Sweta avatar
    10 posts
    Member since:
    Apr 2012

    Posted 10 Apr 2012 Link to this post

    I am using RadAsyncUpload to upload image. I wanted to added client side validation of image for validating height and width of the image. There is property MaxFileSize of this control but it validates the whole size of image, but I want to validate height and width in pixels.

    Thanks in advance.
  2. Cat Cheshire
    Cat Cheshire avatar
    128 posts
    Member since:
    Jun 2010

    Posted 11 Apr 2012 Link to this post

    I doubt that you can make this with the RadAsyncUpload.
    You have to implement your own custom logic to measure the images in pixels.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Sweta
    Sweta avatar
    10 posts
    Member since:
    Apr 2012

    Posted 11 Apr 2012 Link to this post

    Yes I know that some custom logic needs to be build. But how ?
  5. Esteban Lezama
    Esteban Lezama avatar
    1 posts
    Member since:
    May 2010

    Posted 16 Apr 2012 Link to this post

    This is what seems to work for me to retrieve the width and height. Remember to dispose the image. Hope this helps to get you going.

           public void RadAsyncUpload1_FileUploaded(object sender, FileUploadedEventArgs e)
            {
                
                System.Drawing.Bitmap img = new System.Drawing.Bitmap(e.File.InputStream);

                int h = img.Height;
                int w = img.Width;

                img.Dispose();


    if(w > allowedWidth)
    e.IsValid = false;

    // etc. 
    }
  6. Sweta
    Sweta avatar
    10 posts
    Member since:
    Apr 2012

    Posted 18 Apr 2012 Link to this post

    Hi Estaben,

    Thanks for the reply. But this is server side code. How to do it clientside ?

  7. Max
    Max avatar
    34 posts
    Member since:
    Dec 2011

    Posted 19 Apr 2012 Link to this post

    In order to be able to do some logic with RadUploadedFile you have to postback, if I understand correctly, so save server side dimensions as esteban suggests
    or
     using (var image = System.Drawing.Image.FromStream(AsyncUpload.UploadedFiles[0].InputStream))
                            {
                               this.Height = image.Height;
                               this.Width = image.Width;
                            }

    and access them through <%= this.Height %> in js.

    If you have script resources you might want to create a custom control, where you will be directly exposing any requered variable to js through ScriptControlDescriptor.
    vincexu.blogspot.com/2010/02/aspnet-ajax-scriptcontrol-tutorial.html
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017