Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, September 14, 2012 5:26 PM
For Each f As String In FileIO.FileSystem.GetFiles(My.Settings.folder)
'What do I put here?
Next
I don't want a massive if statement. just like a function or something...
I'm sorry if I misspelled something or my grammar is wrong, I've never done well in those subjects.
All replies (3)
Friday, September 14, 2012 5:37 PM âś…Answered
Hi,
follow this url and get the answer -
Filetype check in VB.NET?
http://stackoverflow.com/questions/3557874/filetype-check-in-vb-net
or view the codes
Function IsValidImage(filename As String) As Boolean
Try
Dim img As System.Drawing.Image = System.Drawing.Image.FromFile(filename)
Catch generatedExceptionName As OutOfMemoryException
' Image.FromFile throws an OutOfMemoryException
' if the file does not have a valid image format or
' GDI+ does not support the pixel format of the file.
'
Return False
End Try
Return True
End Function
You use it as follows:
If IsValidImage("c:\path\to\your\file.ext") Then
'do something
'
Else
'do something else
'
End If
Edit:
I don't recommend you check file extensions. Anyone can save a different file (text document for instance) with a .jpg extension and trick you app into beleiving it is an image.
The best way is to load the image using the function above or to open the first few bytes and check for the JPEG signature.
JPEG Header Format
http://www.fastgraph.com/help/jpeg_header_format.html
thanks.
Pl. Mark/Proposed as Answer if found your solution Vote as Helpful if related to your topic. Always Motivate others by performing this Action.
Saturday, September 15, 2012 12:50 AM
I don't know what you mean by "I don't want a massive if statement. just like a function or something"
I have code but its almost 50 lines, sorry. It has several If's as well.
Saturday, September 15, 2012 1:45 AM
If you want simple code to test a filename against a list of possible extensions, the code below will do it. Testing a file to see whether or not it contains an image is an entirely different process.
Dim Extensions() As String = {".jpg", ".jp2", ".png", ".gif", ".bmp", ".tif" } 'etc
For Each f As String In FileIO.FileSystem.GetFiles(My.Settings.folder)
Dim FI As New IO.FileInfo(f)
If Extensions.Contains(FI.Extension.ToLower) Then
lstImage.Items.Add(f)
Else
lstOther.Items.Add(f)
End If
Next