Last post Jul 03, 2007 07:53 AM by mehulmistry
Jul 02, 2007 10:04 AM|mehulmistry|LINK
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
'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
Jul 02, 2007 12:31 PM|naturehermit|LINK
The upload control by default allows only 4 MB but you can increase the size like so in web.config
for your refrence: http://msdn2.microsoft.com/en-US/library/aa479405.aspx
Jul 02, 2007 01:20 PM|mehulmistry|LINK
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.
Jul 02, 2007 01:55 PM|naturehermit|LINK
in the actual place where you are displaying the image set the image height and width..
<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..?
Jul 02, 2007 03:19 PM|mehulmistry|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.
Jul 02, 2007 03:35 PM|naturehermit|LINK
If you want to perform true image re-sizing then read this..
however you will still need to decide on a scale.
If you want to use handle images before they display then consider using httphandler..here is how
Jul 02, 2007 09:38 PM|Haissam|LINK
Check out the below link
Create Image thumbnails
Jul 03, 2007 07:53 AM|mehulmistry|LINK
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.