radupload check image width and height

Thread is closed for posting
7 posts, 0 answers
  1. sener
    sener avatar
    4 posts
    Member since:
    Jan 2011

    Posted 07 Jan 2011 Link to this post

    protected void rauIMAGE_ValidatingFile(object sender, Telerik.Web.UI.Upload.ValidateFileEventArgs e)
            {
                //we will check image height and width
                if (rauIMAGE.UploadedFiles.Count > 0)
                {
                    foreach (UploadedFile theFile in rauIMAGE.UploadedFiles)
                    {
                        //Stream stream = theFile.InputStream; (when uncomment problem occurs)
                        //using (System.Drawing.Bitmap myImage = new System.Drawing.Bitmap(stream,false))
                        //{
                        //    if (myImage.Width > 128 || myImage.Height > 128)
                        //    {
                        //        master.ShowMessage("Image bigger than width :300px by height: 100px not allowed", null, "");
                        //        e.IsValid = false;
                        //    }
                        //}
                        //e.SkipInternalValidation = true;
                    }
                }
            }

    In the above code block I try to determine uploaded image width and height to be able to prevent from uploading image size larger than (128x128)
    but when use inputstream to create new image object I come across with strange error. Also

    System.Drawing.Bitmap myImage = new System.Drawing.Bitmap(stream,false) error
    System.Drawing.Bitmap myImage = new System.Drawing.Bitmap(theFile.InputStream,false) error
    System.Drawing.Bitmap myImage = new System.Drawing.Bitmap(128,128) no error when also comment this line--> (Stream stream = theFile.InputStream)

    Do you have any idea about that strange error ??
  2. sener
    sener avatar
    4 posts
    Member since:
    Jan 2011

    Posted 07 Jan 2011 Link to this post

    addition to post that Can any one tell me how to determine image width and height if no one have idea about the error?
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Cori
    Cori avatar
    562 posts
    Member since:
    Jul 2010

    Posted 07 Jan 2011 Link to this post

    Hello Sener,

    Try something like this:

    Image img = Image.FromStream(theFile.InputStream);
      
    if (img.Height > 128 || img.Width > 129)
    {
        // code
    }
      
    img.Dispose();

    I hope that helps.
  5. Cori
    Cori avatar
    562 posts
    Member since:
    Jul 2010

    Posted 07 Jan 2011 Link to this post

    After reading about the ValidatingFile event in the Telerik Help Documentation. I would have to assume this event is called for every uploaded file, so I would assume you shouldn't access the UploadedFiles collection from this event. I would suggest to use what I posted before but with some modifications. Instead of looping through the UploadedFiles, get InputStrem from e.UploadedFile.

    So the full code should look like this:

    Image img = Image.FromStream(e.UploadedFile.InputStream); 
        
    if (img.Height > 128 || img.Width > 129) 
        e.IsValid = false;
      
    img.Dispose();
      
    // if file is invalid, there is no need
     // to proceed with integrated validation
     e.SkipInternalValidation = !e.IsValid;

    I hope that helps.
  6. sener
    sener avatar
    4 posts
    Member since:
    Jan 2011

    Posted 08 Jan 2011 Link to this post

    Cori thanks for your replay,

    I try it but not solve the problem when I use inputsream the same strange error still occur when uncomment that line problem not occur. May be I forgot something about inputstream usage. I dont know??

    System.Drawing.

    Image myImage = new System.Drawing.Bitmap(128,128);-->no problem

     

    System.Drawing.

    Image myImage = new System.Drawing.Bitmap(e.UploadedFile.InputStream); ->problem occurd

     

  7. sener
    sener avatar
    4 posts
    Member since:
    Jan 2011

    Posted 09 Jan 2011 Link to this post

    Actual error is that;
    System.IO.IOException file used by another process

    and solution :

    //we will check image height and width

     

    if (rauIMAGE.UploadedFiles.Count > 0)

     

    {

     

    Stream file = e.UploadedFile.InputStream;

     

    System.Drawing.

    Image myImage = new System.Drawing.Bitmap(file);

     

    file.Close();

     

    if (myImage.Width > 128 || myImage.Height > 128)

     

    {

    master.ShowMessage(

    "Image bigger than width :128px by height: 128px not allowed", null, "");

     

    e.IsValid =

    false;

     

    }

     

     

    //e.SkipInternalValidation = !e.IsValid;

     

     

    //ResizeImage();//default resize uploaded image width bigger than 128px to 128px.

     

  8. Dan Hardin
    Dan Hardin avatar
    4 posts
    Member since:
    Jul 2009

    Posted 25 Mar 2011 Link to this post

    I have tried this and everything runs fine with no errors, I save this bytes into a database. however when I try to load the image from the database I get a "Parameter is not valid". If I remove the code that creates the Image object and checks the dimensions the upload and reading from the database works fine. Any ideas?
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017