This is a migrated thread and some comments may be shown as answers.

radupload check image width and height

6 Answers 187 Views
Upload (Obsolete)
This is a migrated thread and some comments may be shown as answers.
sener
Top achievements
Rank 1
sener asked on 07 Jan 2011, 11:06 AM
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 ??

6 Answers, 1 is accepted

Sort by
0
sener
Top achievements
Rank 1
answered on 07 Jan 2011, 02:08 PM
addition to post that Can any one tell me how to determine image width and height if no one have idea about the error?
0
Cori
Top achievements
Rank 2
answered on 07 Jan 2011, 02:53 PM
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.
0
Cori
Top achievements
Rank 2
answered on 07 Jan 2011, 03:00 PM

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.
0
sener
Top achievements
Rank 1
answered on 08 Jan 2011, 12:42 PM
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

 

0
sener
Top achievements
Rank 1
answered on 09 Jan 2011, 04:15 PM
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.

 

0
Dan Hardin
Top achievements
Rank 1
answered on 25 Mar 2011, 08:30 PM
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?
Tags
Upload (Obsolete)
Asked by
sener
Top achievements
Rank 1
Answers by
sener
Top achievements
Rank 1
Cori
Top achievements
Rank 2
Dan Hardin
Top achievements
Rank 1
Share this question
or