Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Validate a jpg file

3,832 views
Skip to first unread message

Jesmar Raga

unread,
Dec 1, 2008, 10:29:21 PM12/1/08
to
Hello,

How can I validate a file to know if it is a valid jpg file and if it is not
corrupt?

I did this to know if it is a jpg file and it works fine,
ls_jpg = string(blobmid(lblb_Img, 1, 2), EncodingANSI!)
if ls_jpg = 'ÿØ' then
Return True
else
Return False
end if

But I don't know if it is not corrupt. When I set the setpicture property to
the picture object and the file is corrupt or is not a jpg file the
application crash.

Thanks,


Jerry Siegel [TeamSybase]

unread,
Dec 2, 2008, 7:26:57 AM12/2/08
to
This may be useful
http://stackoverflow.com/questions/210650/validate-image-from-file-in-c

"Jesmar Raga" <wr...@cantv.net> wrote in message
news:4934ab91@forums-1-dub...

Eddy Soeparmin

unread,
Dec 3, 2008, 4:39:32 PM12/3/08
to
Here's what I've done in the past:

/////////////////////////////////////////////////////////////////////////////////////////
//
// Function: of_isJPEGImageFile()
//
// Description:
// - This function will determine if current selected file is a true JPEG
file
// - Open the file in Hex, and read the four bytes
// - Summary of some of the image header file:
// File Type Header File
// =======================
// JPEG FF D8 FF E0
// TIFF 49 49 2A 00
// GIF 47 49 46 38
// BMP 42 4D 36 03
// ICO 00 00 01 00
//
// Argument:
// - ( String ) asv_ImageFileName
//
// Return Value:
// - ( Boolean )
/////////////////////////////////////////////////////////////////////////////////////////

// Image header file
Constant String IMG_JPEG_HEX_FORMAT = 'FFD8FFE0'
Constant String IMG_TIFF_HEX_FORMAT = '49492A'
Constant String IMG_GIFF_HEX_FORMAT = '47494638'
Constant String IMG_BMP_HEX_FORMAT = '424D3603'
Constant String IMG_ICO_HEX_FORMAT = '00000100'

Char lc_FourBytes[]
Blob lblob_ImageData
Long li_FileHandle, ll_TotalBytes
String ls_StringByte, ls_SingleByte, ls_HexHeader
Integer i, li_ReturnCode

// Open the selected image file to determine if it's a JPG format
li_FileHandle = FileOpen( asv_ImageFileName, StreamMode!, Read!, LockRead! )

// Read the first 4 bytes of the image file into binary ( 32k chunk )
li_ReturnCode = FileRead( li_FileHandle, Ref lblob_ImageData )

// check to see if there is any error ( SUCCESS when > 0 )
If ( li_ReturnCode > 0 ) Then
// Get the first 4 bytes
ls_StringByte = Left( String( lblob_ImageData ), 4 )
lc_FourBytes[] = ls_StringByte
ll_TotalBytes = Len( ls_StringByte )

// Loop through it to convert it to Hex
For i = 1 To ll_TotalBytes
ls_SingleByte = of_DecToHex( Asc( lc_FourBytes[i] ) )
ls_HexHeader += ls_SingleByte
Next
End If

// Close the image file
FileClose( li_FileHandle )

Return( ls_HexHeader = IMG_JPEG_HEX_FORMAT )

--
Eddy Soeparmin

"Jesmar Raga" <wr...@cantv.net> wrote in message
news:4934ab91@forums-1-dub...

jhr...@gmail.com

unread,
Dec 3, 2008, 7:20:34 PM12/3/08
to
Thanks Eddy,

I already did something pretty much like that, and I know if it is
JPG; but my real problem is that I want to know if the file is not
corrupt because if I set the setpicture property of a picture control
with this blob the application crash.

Can I do that? is there any way to know if the file is currupt? the
final bytes or something.

Again, Thanks you very much


On 3 dic, 17:39, "Eddy Soeparmin" <Soeparm...@MDAInc.com> wrote:
> Here's what I've done in the past:
>

> ///////////////////////////////////////////////////////////////////////////­//////////////


> //
> // Function: of_isJPEGImageFile()
> //
> // Description:
> //  - This function will determine if current selected file is a true JPEG
> file
> //  - Open the file in Hex, and read the four bytes
> //  - Summary of some of the image header file:
> //   File Type Header File
> //   =======================
> //   JPEG   FF D8 FF E0
> //   TIFF   49 49 2A 00
> //   GIF   47 49 46 38
> //   BMP   42 4D 36 03
> //   ICO   00 00 01 00
> //
> // Argument:
> //  - ( String ) asv_ImageFileName
> //
> // Return Value:
> //  - ( Boolean )

> ///////////////////////////////////////////////////////////////////////////­//////////////

> > Thanks,- Ocultar texto de la cita -
>
> - Mostrar texto de la cita -

Michael Kramer

unread,
Dec 4, 2008, 12:51:03 AM12/4/08
to
You'd have to investigate every block in the file to ensure that it is
correctly defined and the file doesn't end halfway through the final data
block.

See this URL for all the image file format you can imagine.
http://www.fileformat.info/mirror/egff/index.htm

HTH
/MicKr-
Michael Kramer, Denmark

<jhr...@gmail.com> skrev i en meddelelse
news:13ce9a72-9431-4c4e...@n10g2000yqm.googlegroups.com...

jhr...@gmail.com

unread,
Dec 4, 2008, 8:42:28 AM12/4/08
to
Thanks Michael,

That link is about BMP but I look for a similar page with JPEG
Headers, and it is just what a want. Now I have some work to do.

Thank you very much,

On 4 dic, 01:51, "Michael Kramer" <rilau...@gmail.com> wrote:
> You'd have to investigate every block in the file to ensure that it is
> correctly defined and the file doesn't end halfway through the final data
> block.
>

> See this URL for all the image file format you can imagine.http://www.fileformat.info/mirror/egff/index.htm


>
> HTH
> /MicKr-
> Michael Kramer, Denmark
>

> <jhr...@gmail.com> skrev i en meddelelsenews:13ce9a72-9431-4c4e...@n10g2000yqm.googlegroups.com...

> > - Mostrar texto de la cita -- Ocultar texto de la cita -

0 new messages