|
-
Jun 28th, 2003, 03:37 PM
#1
Thread Starter
Frenzied Member
How to verify file type? (JPG)
How you can verify the file types? For example if the file introduced to your application has the .JPG extention how can you decide if it is really a jpg file or just has jpg extension. I guess it will be a hard task because of lots of file types available, so i just need to know about JPGs.
Thanks.
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
Jun 28th, 2003, 06:05 PM
#2
Sleep mode
I believe it needs more code than this but you can try something like this :
VB Code:
Dim jp As Drawing.Image
jp.Equals(blah blah)
'or something like this
jp.Flags.CompareTo (blah)
I'm sure it won't work though ...lol . Maybe API function can determine that correctly . I dunno ...lol
-
Jun 28th, 2003, 06:29 PM
#3
PowerPoster
Re: How to verify file type? (JPG)
Originally posted by Lunatic3
How you can verify the file types? For example if the file introduced to your application has the .JPG extention how can you decide if it is really a jpg file or just has jpg extension. I guess it will be a hard task because of lots of file types available, so i just need to know about JPGs.
Thanks.
what is your exact problem? Are you retrieving a .JPG file from the web, but a html file is being downloaded instead? (you can verify it by changing the extension and opening it with notepad.) If this is in deed your problem, just change the file type to .txt, then open the file using a text reader. If works, then it wasn't a jpg.
-
Jun 28th, 2003, 11:51 PM
#4
Thread Starter
Frenzied Member
The problem is that i want the user to upload his/her picture that should be in JPG format to his profile in web page. I know how to do it using asp.net, but the user may upload a picture that is not realy a picture, lets say an exe file with the extension changed to JPG. I ust dont want to have that image place holder shown in the web page without any pic in it. So i am searching for a way to verify if the file is realy JPG and if not inform the user to upload a correct file. As i am doing the ASP.NET pages in VB.NET so i decided to ask in this forum.
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
Jun 29th, 2003, 12:00 AM
#5
PowerPoster
I don't know if this would work at all, but this is what I thought of. I didn't even verify it or debug any of it, I just threw it together in this post...
Dim myBitMap As Bitmap
Dim isImage As Bool = True
Try
myBitMap = New Bitmap("C:\myImg.jpg")
If myBitMap.Height > 1 Then
isImage = False
End If
Catch
'Don't think it is a image.
isImage = False
End Try
If isImage = True Then
'It is an image.
Else
'It is not an image.
End If
-
Jun 29th, 2003, 01:17 AM
#6
Sleep mode
I'm sure there is an API Function for that . The only silly work around I can think of is : let the user browse his picture , then load it invisibly (at the background) in a picturebox control , if error raised , then it's not valid picture , otherwise , it's fine . I don't know if ASP.NET can do such stuff but it should do it .
-
Jun 29th, 2003, 02:37 AM
#7
Thread Starter
Frenzied Member
It seems image control does not raise an error if it can not show the source file, it just does not show anything. I did it once, and that was the result, hiowever i should check it more!
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
Jun 29th, 2003, 04:06 AM
#8
Sleep mode
I don't know how you did it . This code worked for me . I change 11 kb exe file to jpg . It raised error .
VB Code:
'Return True if it's ok
Public Function ChkFile() As Boolean
Dim picbrowse As New OpenFileDialog
picbrowse.ShowDialog()
Try
PictureBox1.Image = PictureBox1.Image.FromFile(picbrowse.FileName)
Return True
Catch ex As Exception
MessageBox.Show("Wrong file type")
Return False
End Try
End Function
'Usage
Private Sub Button2_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles Button2.Click
If ChkFile() Then
MessageBox.Show("ok")
Else
MessageBox.Show("not ok")
End If
End Sub
-
Jun 29th, 2003, 04:09 AM
#9
Sleep mode
However , changing jpg (or whatever image file) to exe file will not make any error . This is not a problem I think and not dangerous too . Just a note .
-
Jun 29th, 2003, 02:53 PM
#10
yay gay
well but still, if the user gives a .bmp image it will open it with no error thrown and i think that he doesnt want it..the way to do it the RIGHT way can be somewhat hard but if u want you can see the JPEG format here : http://www.wotsit.org/download.asp?f=itu-1150PDF, http://www.wotsit.org/download.asp?f=jpeg_c , http://www.wotsit.org/download.asp?f=jfif , http://www.wotsit.org/download.asp?f=itu-1150 , http://www.cis.ohio-state.edu/hypert...g-faq/top.html , http://www.wotsit.org/download.asp?f=jpeg
its all taken from here http://www.wotsit.org/
hope this helps
edit: the site doenst allow direct linking to it so just do a search by "jpg" and u'll find all the links i've posted here
Last edited by PT Exorcist; Jun 29th, 2003 at 02:56 PM.
\m/  \m/
-
Jun 29th, 2003, 03:05 PM
#11
Sleep mode
Yea , I know that . I was talking about all image files (true image file ) against Fake file type (like exe to jpg not the opposite though) .
-
Jun 29th, 2003, 11:22 PM
#12
Lively Member
I dont have the answer to your question but I had a thought. When you save a JPEG is there any header infomation or metadata insert into the picture file when it is saved. If there is you can check the file that is being uploaded for the header info.
Let me know what you think
-
Jun 30th, 2003, 08:57 AM
#13
Thread Starter
Frenzied Member
Thanks guys. I think I found the solution.
VB Code:
Dim img as Image
Try
img = Image.FromFile("the_file_path")
If (img.RawFormat.Equals(Imaging.ImageFormat.Jpeg)) Then
MessageBox.Show("Jpeg format")
Else
MessageBox.Show("not a Jpeg")
End If
Catch ex As Exception
MessageBox.Show("Not a picture")
End Try
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|