2014年7月13日 星期日

[RESOLVED] Question about the FileUpload control


I do have a form that upload pictures files with the FileUpload control. At the present time the code look like this :


'Find image file length in bytes
Dim FileSize As Long = UploadedFile.FileContent.Length
If FileSize > 200000 Then
errorLabelDisplay.Text = "
" + "Your image size cannot be greater than 200 KB. " + "
" + "Please reduce the size and try again." + "
"

strMessage = "Your image size cannot be greater than 200 KB. "
strMessage += "Please reduce the size and try again."

'Call separate class to open a alert window, this class is in the App_Code
AlertWindow.CreateMessageAlert(Me, strMessage, "strKey2")


Exit Sub
End If

'This part limit the image physical dimensions
Dim image As System.Drawing.Image = System.Drawing.Image.FromStream(UploadedFile.PostedFile.InputStream)
Dim w As Single = image.PhysicalDimension.Width
Dim h As Single = image.PhysicalDimension.Height

If w > 5000 Then
errorLabelDisplay.Text = "
" + "Your image width cannot be greater than 5000 px. " + "
"
errorLabelDisplay.Text = errorLabelDisplay.Text + "Please reduce your picture and try again. Thanks."

strMessage = "Your image width cannot be greater than 5000 px. "
strMessage += "Please reduce your picture and try again. Thanks."

'Call separate class to open a alert window, this class is in the App_Code
AlertWindow.CreateMessageAlert(Me, strMessage, "strKey3")


Exit Sub
ElseIf h > 5000 Then
errorLabelDisplay.Text = "
" + "Your image height cannot be greater than 5000 px. " + "
"
errorLabelDisplay.Text = errorLabelDisplay.Text + "Please reduce your picture and try again. Thanks."

strMessage = "Your image height cannot be greater than 5000 px. "
strMessage += "Please reduce your picture and try again. Thanks."

'Call separate class to open a alert window, this class is in the App_Code
AlertWindow.CreateMessageAlert(Me, strMessage, "strKey4")

Exit Sub
End If


Everything is ok and functioning well, however, is a burden for websites users to receive these error messages and ask them to change the picture size, width and heigth in order to upload their picture file. What I want instead is, does not matter the size, width or heigth, I would like a piece of code here that do it automatically for the user. Something like this:


'Find image file length in bytes
Dim FileSize As Long = UploadedFile.FileContent.Length
If FileSize > 200000 Then

REDUCE THE FILE SIZE WITHOUT ALTERING THE PICTURE TO LESS THAN OR EQUAL TO 200000

Exit Sub
End If

'This part limit the image physical dimensions
Dim image As System.Drawing.Image = System.Drawing.Image.FromStream(UploadedFile.PostedFile.InputStream)
Dim w As Single = image.PhysicalDimension.Width
Dim h As Single = image.PhysicalDimension.Height

If w > 5000 Then

REDUCE THE FILE WIDTH WITHOUT ALTERING THE PICTURE TO LESS THAN OR EQUAL TO 5000
Exit Sub


ElseIf H > 5000 Then

REDUCE THE FILE HEIGHT SIZE WITHOUT ALTERING THE PICTURE TO LESS THAN OR EQUAL TO 5000
Exit Sub

End If


Thanks for reading this post. I hope that someone can help me.



Just to inform all of you that read this post and couldn't solved it, there is a previous post on this topic. I am reading this post now and I will post here whether or not this code work. The link is : http://www.scribd.com/doc/7598768/Resize-an-Image-Using-ASP


Thanks to all of you that tried to help. I wll let you know later if this is working.



Well, I think that I am very close to solve the problem. I am usig the code that is in the link that I posted before this one. The only problem that I am facing now is this. I do have the following piece of code:


realPicture.Save(baseDirectory & FileName & "Pic" & i, System.Drawing.Imaging.ImageFormat.Gif)

The last part of the code is "Gif", however what I need is "jpg" and there is not way that ImageFormat allow me to enter this. Is there any way that I can play with this and enter it. Here is the complete code:


 


              If FileSize > 200000 Then
Dim extension As String = Path.GetExtension(UploadedFile.PostedFile.FileName).ToLower()
Dim UploadedFileName As String = UploadedFile.FileName.ToString & extension

'Here we create an instance of the class DealingWithUploadImage in order
'to use one of its functions, ResizeImage, the purpose of this is, as the name indicate
'to manipulate the picture size and dimensions.
Dim imageSizeAndDimensionAdjustment As New DealingWithUploadedImage()
Dim realPicture As Bitmap = imageSizeAndDimensionAdjustment.ResizeImage(UploadedFile.PostedFile.InputStream, 500, 695)
realPicture.Save(baseDirectory & FileName & "Pic" & i, System.Drawing.Imaging.ImageFormat.Gif )

realPicture.Dispose()

Else ...............

Thanks







vstorpedo



Well, I think that I am very close to solve the problem. I am usig the code that is in the link that I posted before this one. The only problem that I am facing now is this. I do have the following piece of code:
realPicture.Save(baseDirectory & FileName & "Pic" & i, System.Drawing.Imaging.ImageFormat.Gif)


 Well, since the last part of the code is not working, sometimes we have to be a little bit creative and find strange ways to do things. I include the ".jpg" at the end of the file name and is working fine. so I am going to leave it like that. This is the code and thanks to all that read the post and tried to solve the problem.


 realPicture.Save(baseDirectory & FileName & "Pic" & i & ".jpg", System.Drawing.Imaging.ImageFormat.Gif)


 






沒有留言:

張貼留言