Results 1 to 2 of 2

Thread: [RESOLVED] Detecting Invalid Images?

  1. #1

    Thread Starter
    PowerPoster formlesstree4's Avatar
    Join Date
    Jun 2008
    Posts
    3,250

    Resolved [RESOLVED] Detecting Invalid Images?

    Ok, I have a program that parses INI files (yes yes, I know, but I'm stuck with using them, not my own request), and it downloads external images (if the INI contains the path) to the program and cache's them.

    Recently, I've run into a snag with the system. Most of the images use Imageshack for storage, but one of them has suddenly 404'd, and whenever I set it to the picturebox to the downloaded image, I get an out of memory error rather quickly (and the file itself is 1kb).

    I guess I need to implement a checking system offline to find out if the image is actually an image or just a '404' text...but is there a way to not have to implement this clientside, and have it be checked when I download it?

    Here's the downloading code:
    vb.net Code:
    1. Public Function DownloadImageStream(ByVal ImageURL As String) As MemoryStream
    2.         Dim objImage As MemoryStream 'Create a MemoryStream
    3.         Dim objwebClient As WebClient 'Create a WebClient
    4.         Dim sURL As String = Trim(ImageURL) 'Remove any leading/ending spaces
    5.         Try
    6.             'Add the http:// if needed
    7.             If Not sURL.ToLower().StartsWith("http://") Then sURL = "http://" & sURL
    8.             'Initiate the new WebClient
    9.             objwebClient = New WebClient()
    10.             Dim url As Uri
    11.             url = New Uri(sURL)
    12.             'New MemoryStream from downloading the image
    13.             'from the specified url
    14.             objImage = New MemoryStream(objwebClient.DownloadData(url))
    15.             'Return the Image from the downloaded Stream
    16.             Return objImage
    17.         Catch ex As Exception
    18.             'custom error handling, don't worry about this.
    19.             Dim err As New default_ErrorDialog
    20.             err.ThrowError(ex.Message, ex.StackTrace, ex.InnerException.ToString)
    21.             Return Nothing
    22.         End Try
    23.     End Function

  2. #2

    Thread Starter
    PowerPoster formlesstree4's Avatar
    Join Date
    Jun 2008
    Posts
    3,250

    Re: Detecting Invalid Images?

    Double Posting with solution.

    I just created another sub that quickly reads the first line of the image. If it's "404", then I just don't load the Image:
    vb.net Code:
    1. Public Function CheckImage(ByVal imagePath As String) As Boolean
    2.         Try
    3.             Dim str As String
    4.             Using SR As New StreamReader(imagePath)
    5.                 str = SR.ReadLine()
    6.             End Using
    7.             If str = "404" Then Return False Else Return True
    8.         Catch ex As Exception
    9.             MessageBox.Show(ex.ToString)
    10.             Return False
    11.         End Try
    12.     End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width