I have requirement to upload images to the server from my web form and also RESTRICTING THE IMAGE SIZE i:e setting max limit for height and width.The check should be done before I upload the image. I tried one solution for this in the code behind and
this worked fine for me while I was developing and running my application from localhost.
But the actual problem came when I hosted my application on the server and I am not able to upload any image. I feel the problem is because of the way I am checking the size of the before being uploaded.
Here is sample of the code behind.
lstrHttpPostedFile = Request.Files(0)
If Not (lstrHttpPostedFile Is Nothing Or lstrHttpPostedFile.FileName = "" Or lstrHttpPostedFile.ContentLength < 1) Then
'Get the name of the file
lstrfilename = lstrHttpPostedFile.FileName limg = System.Drawing.Image.FromFile(lstrfilename) lintWidth = limg.Width lintHeight = limg.Height
If lintWidth <= 170 And lintHeight <= 100 Then
' select the drirectory on server and upload the file
Else
'don't upload and show an error
15 End if
16 End If
Line related to checking of the image size is in BOLD. when I debugged (on my machine) the varaible lstrfilename gets the filename as c:\myFolder\myImage.gif. Now system.drawing.Image class might try to locate this on the server and might not get initialized. And hence the error is coming. This is my assumption as I can't debug on the main server. ( On my local machine this works and uploads file, but the image is also at the same machine).
any help or suggestion for different way to do this is appreciated. Thanks
Thanks for your reply. well all my images files that I wish to upload is less than 4 MB.(infact max size in my scenario is 1.5 MB). But my problem is not because of the size. I want to restrict the
IMAGE size i:e its Height and width. The sample code above is how I was able to achieve it in code behind on server side. But it fails to do so when I deploy on the actual server.
Yes definitely you are very much correct.And this is exactly what I did for the very first time. But My requirement is kind of in between this. My client will upload image within a specified max range of height and witdth. So I can't specify a static or
fix size. AS if i do this some the image's will get distorted if little different size. And my client needs a way where in they can upload the image within specified range. So this prob won't be there. So I want to remove the height and width of the image
tag and want to restrict it while they upload.
mehulmistry
Member
133 Points
102 Posts
How to restrict Image Size while upload?
Jul 02, 2007 09:04 AM|LINK
Hi all,
I have requirement to upload images to the server from my web form and also RESTRICTING THE IMAGE SIZE i:e setting max limit for height and width.The check should be done before I upload the image. I tried one solution for this in the code behind and this worked fine for me while I was developing and running my application from localhost.
But the actual problem came when I hosted my application on the server and I am not able to upload any image. I feel the problem is because of the way I am checking the size of the before being uploaded.
Here is sample of the code behind.
Line related to checking of the image size is in BOLD. when I debugged (on my machine) the varaible lstrfilename gets the filename as c:\myFolder\myImage.gif. Now system.drawing.Image class might try to locate this on the server and might not get initialized. And hence the error is coming. This is my assumption as I can't debug on the main server. ( On my local machine this works and uploads file, but the image is also at the same machine).
any help or suggestion for different way to do this is appreciated. Thanks
Thanks,
Mehul Mistry
If you feel your question is answered then please click "Mark As Answer" to the post that answered it. Thanks :-)
naturehermit
Star
14610 Points
3046 Posts
Re: How to restrict Image Size while upload?
Jul 02, 2007 11:31 AM|LINK
The upload control by default allows only 4 MB but you can increase the size like so in web.config
mehulmistry
Member
133 Points
102 Posts
Re: How to restrict Image Size while upload?
Jul 02, 2007 12:20 PM|LINK
Hi naturehermit,
Thanks for your reply. well all my images files that I wish to upload is less than 4 MB.(infact max size in my scenario is 1.5 MB). But my problem is not because of the size. I want to restrict the IMAGE size i:e its Height and width. The sample code above is how I was able to achieve it in code behind on server side. But it fails to do so when I deploy on the actual server.
Thanks,
Mehul Mistry
If you feel your question is answered then please click "Mark As Answer" to the post that answered it. Thanks :-)
naturehermit
Star
14610 Points
3046 Posts
Re: How to restrict Image Size while upload?
Jul 02, 2007 12:55 PM|LINK
in the actual place where you are displaying the image set the image height and width..
for e.g
<img src="SoureOfImage" height="yourheight" widht="yourwidth">
or <asp:Image width="" height="" and ImageUrl="yoururl">
because you will need to use some image control to display it, isnt it..?
mehulmistry
Member
133 Points
102 Posts
Re: How to restrict Image Size while upload?
Jul 02, 2007 02:19 PM|LINK
Yes definitely you are very much correct.And this is exactly what I did for the very first time. But My requirement is kind of in between this. My client will upload image within a specified max range of height and witdth. So I can't specify a static or fix size. AS if i do this some the image's will get distorted if little different size. And my client needs a way where in they can upload the image within specified range. So this prob won't be there. So I want to remove the height and width of the image tag and want to restrict it while they upload.
Thanks,
Mehul Mistry
If you feel your question is answered then please click "Mark As Answer" to the post that answered it. Thanks :-)
naturehermit
Star
14610 Points
3046 Posts
Re: How to restrict Image Size while upload?
Jul 02, 2007 02:35 PM|LINK
Hi there,
If you want to perform true image re-sizing then read this..
http://aspnet.4guysfromrolla.com/articles/011503-1.aspx
and
http://aspnet.4guysfromrolla.com/articles/012203-1.aspx
http://aspnet.4guysfromrolla.com/articles/081303-1.aspx
however you will still need to decide on a scale.
and http://west-wind.com/weblog/posts/283.aspx
If you want to use handle images before they display then consider using httphandler..here is how
http://www.c-sharpcorner.com/UploadFile/desaijm/HTTPHandlersForImages11152005062705AM/HTTPHandlersForImages.aspx
http://www.codeproject.com/aspnet/CatalogViewPhotoGallery.asp
Haissam
All-Star
37421 Points
5632 Posts
Re: How to restrict Image Size while upload?
Jul 02, 2007 08:38 PM|LINK
Check out the below link
Create Image thumbnails
HC
MCAD.NET
| Blog |
mehulmistry
Member
133 Points
102 Posts
Re: How to restrict Image Size while upload?
Jul 03, 2007 06:53 AM|LINK
hi naturehermit,
Well thanks for the links. They are really nice articles. I think I change my approach of what I am trying to achieve. Links will be helpfull.
Thanks.
Thanks,
Mehul Mistry
If you feel your question is answered then please click "Mark As Answer" to the post that answered it. Thanks :-)