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

Can VB detect JPG image sizes???

18 views
Skip to first unread message

Murray Macdonald

unread,
Oct 10, 1997, 3:00:00 AM10/10/97
to

Hello people,

I'm trying to use VB5 to determine the height and width of a jpg image.
Has anyone out there ever done this or know where I can get a definitive
jpg spec?

Thanks in advance...

Sincerely,

Murray.

John Carter

unread,
Oct 12, 1997, 3:00:00 AM10/12/97
to

Murray Macdonald <mur...@mha.ca> wrote:

>Hello people,
>
>I'm trying to use VB5 to determine the height and width of a jpg image.
>Has anyone out there ever done this or know where I can get a definitive
>jpg spec?
>

You can get the basic code for reading the image size from .gif and
jpg images on the PowerBasic source page at my home page - URL below.
You will need to modify the routines that parse the byte stream, since
there is a difference between PB and VB in how the bytes are numbered
- beginning at 0 versus beginning at 1.

I guess this should be translated and put on my VB page, but not today
- 5 hours cutting railroad ties and moving landscape timbers makes a
long afternoon...

Visit my source code page at http://www.mindspring.com/~johnecarter
Visit my favorite school at http://www.mindspring.com/~addison

Get PR-Tracker -- tracks problem reports, defects, bugs
INFORMATION: http://www.prtracker.com/info.html
DOWNLOAD: http://www.prtracker.com/download.html


Alex

unread,
Oct 12, 1997, 3:00:00 AM10/12/97
to

The easiest method is to load it into an invisible picturebox or image
control with autosize true or stretch false, respectively. Then get the
(scale)height and (scale)width. You could also set the Scalemode to
pixels to make things even easier.

The biggest problem with this is the time it can take to load a large
image, forgetting about the resources it consumes. Be sure to do
a =loadpicture() to unload the image and free those resources.

Regards,
Alex

--
Spam Alert: Please remove the 'X' in my email address when
replying to me.

Murray Macdonald wrote in message <343EB1DF...@mha.ca>...


>Hello people,
>
>I'm trying to use VB5 to determine the height and width of a jpg image.
>Has anyone out there ever done this or know where I can get a definitive
>jpg spec?
>

som...@somewhere.com

unread,
Oct 16, 1997, 3:00:00 AM10/16/97
to

On Sun, 12 Oct 1997 19:04:19 +0800, "Alex" <y...@Xiinet.net.au> wrote:

>The easiest method is to load it into an invisible picturebox or image
>control with autosize true or stretch false, respectively. Then get the
>(scale)height and (scale)width. You could also set the Scalemode to
>pixels to make things even easier.
>
>The biggest problem with this is the time it can take to load a large
>image, forgetting about the resources it consumes. Be sure to do
>a =loadpicture() to unload the image and free those resources.

It's going to take time anyway, if not woth a load picture, then with
the routine that would read and parse the file.

I agree wholeheartedly - load the picture into a control and just
measure the control. There is an added benefit here in doing this -
you don't have to write a new parsing routine every time you wish to
recognize a different picture format! You just use a control that
already knows how to load...blah blah blah


ту LEGITIMATE responses to midknyte at usa dot net
ЇЇ

Marcus Agrippa

unread,
Oct 16, 1997, 3:00:00 AM10/16/97
to

Actually it takes virtually no time to determine the size of a JPEG image. The
process is to scan for

FF followed by one of C0-C3, C5-CB, CD-CF

After you find that sequence. skip three bytes.

The next two bytes are the height followed by two bytes for the width.

The trick with the dimensions is that they byte order is reversed.


John - N8086N

------------------------------------------------
Doesn't the marketing person who decided
"Windows 4.0" should be called "Windows '95"
look really stupid right now?


Home Page:
http://home.att.net/~miano

Home of the Delphi Component Writers' FAQ

EMail Address:
|m.i.a.n.o @ |
|w.o.r.l.d.n.e.t . |
|a.t.t .|
|n.e.t |


Full Name:
-------------------
-J.o.h.n?M.i.a.n.o-
-------------------


John Carter

unread,
Oct 17, 1997, 3:00:00 AM10/17/97
to

som...@somewhere.com (No-I-Do-No...@Sam.Iam) wrote:

>On Sun, 12 Oct 1997 19:04:19 +0800, "Alex" <y...@Xiinet.net.au> wrote:
>

[snip]

>I agree wholeheartedly - load the picture into a control and just
>measure the control.

It takes a lot less time to open a file and read at most 512 bytes
(TIFF header, much less for GIF and JPEG) than to load a 30K file and
then get its size from the control.

som...@somewhere.com

unread,
Oct 20, 1997, 3:00:00 AM10/20/97
to

still, you need to know how the height and width information is stored
in the particular image file to parse it... Using a control still
affords him that ignorance.

Marcus Agrippa

unread,
Oct 20, 1997, 3:00:00 AM10/20/97
to

In article <344b8bec....@news.execpc.com>, som...@somewhere.com (No-I-Do-No...@Sam.Iam) wrote:
>
>still, you need to know how the height and width information is stored
>in the particular image file to parse it... Using a control still
>affords him that ignorance.

If you got to my web site listed below there is a C++ program that dumps the
contents of every type of block for huffman-encoded, progressive and
sequential JPEG images.


John - N8086N

------------------------------------------------
Is Janet Reno completely incompetent or
simply a crook?

Roger Smith

unread,
Oct 20, 1997, 3:00:00 AM10/20/97
to

Here's the source of a module I wrote which gets the size of a JPEG or
GIF image. Just call GetImageSize and pass it the filespec of the
image. It returns the height and width (or -1 for each if an error
occurs).

====

Attribute VB_Name = "ImageMan"
Option Explicit

Global Const M_SOF0 = &HC0
Global Const M_SOF1 = &HC1
Global Const M_SOF2 = &HC2
Global Const M_SOF3 = &HC3
Global Const M_SOF5 = &HC5
Global Const M_SOF6 = &HC6
Global Const M_SOF7 = &HC7
Global Const M_SOF9 = &HC9
Global Const M_SOF10 = &HCA
Global Const M_SOF11 = &HCB
Global Const M_SOF13 = &HCD
Global Const M_SOF14 = &HCE
Global Const M_SOF15 = &HCF
Global Const M_SOI = &HD8
Global Const M_EOI = &HD9
Global Const M_SOS = &HDA
Global Const M_COM = &HFE

' Type defs for GIF files
Private Type GIFHeader
GIF As String * 6
Width As Integer
Height As Integer
Flags As String * 1
Background As String * 1
Aspect As String * 1
End Type

Private Type ImageBlock
left As Integer
top As Integer
Width As Integer
Height As Integer
Flags As String * 1
End Type

Public Sub GetImageSize(ByVal filespec As String, _
Width As Integer, Height as Integer)
Dim f As Integer
Dim c6 As String * 6
Dim c4 As String * 4

f = -1
Height = -1
Width = -1
On Local Error GoTo GetImageSizeError
If FileExists(filespec) Then
f = FreeFile
Open filespec For Binary As #f
Get #f, 1, c6
Select Case c6
Case "GIF87a"
Close f
GetImageSizeGIF filespec, Width, Height
Case "GIF89a"
Close f
GetImageSizeGIF filespec, Width, Height
Case Else
Get #f, 7, c4
Close f
If c4 = "JFIF" Then
GetImageSizeJPG filespec, Width, Height
End If
End Select
End If
Close f
On Error GoTo 0
Exit Sub
GetImageSizeError:
Height = -1
Width = -1
If f >= 0 Then Close f
Exit Sub
End Sub

Public Sub GetImageSizeGIF(ByVal filespec As String, _
Width As Integer, Height As Integer)
Dim f As Integer
Dim i As Integer
Dim j As Integer
Dim c6 As String * 6
Dim C3 As String * 3
Dim h As GIFHeader
Dim ib As ImageBlock
Dim c As String * 1

f = -1
Height = -1
Width = -1
On Error GoTo GetImageSizeError87a
f = FreeFile
Open filespec For Binary As #f

Get #f, 1, h

Width = h.Width
Height = h.Height

i = Asc(h.Flags)
If i And &H80 Then
i = 2 ^ ((i And 7) + 1)
For j = 1 To i
Get #f, , C3
Next
End If

While Not EOF(f)
Get #f, , c
If (c = ",") Then
Get #f, , ib
Width = ib.Width
Height = ib.Height
Exit Sub
End If
Wend

Close f
Exit Sub
GetImageSizeError87a:
Height = -1
Width = -1
If f >= 0 Then Close f
Exit Sub
End Sub

Public Sub GetImageSizeJPG(ByVal filespec As String, _
Width As Integer, Height As Integer)
Dim f As Integer
Dim c6 As String * 6
Dim marker As Integer
Dim length As Integer
Dim precision As Integer

f = -1
Height = -1
Width = -1
On Error GoTo GetImageSizeErrorJPG
f = FreeFile
Open filespec For Binary As #f
If first_marker(f) <> M_SOI Then
Close f
Exit Sub
End If
Do
marker = next_marker(f)
Select Case marker
Case -1
Close f
Exit Sub
Case M_SOF0, M_SOF1, M_SOF2, M_SOF3, M_SOF5, M_SOF6, _
M_SOF7, M_SOF9, M_SOF10, M_SOF11, M_SOF13, _
M_SOF14, M_SOF15
length = GetInt(f)
precision = GetC(f)
Height = GetInt(f)
Width = GetInt(f)
Close f
Exit Sub
Case Else
Skip_Variable f
End Select
Loop
Close f
Exit Sub
GetImageSizeErrorJPG:
Height = -1
Width = -1
If f >= 0 Then Close f
Exit Sub
End Sub

Private Sub Skip_Variable(ByVal f As Integer)
Dim length As Integer

length = GetInt(f)
length = length - 2
Seek #f, Seek(f) + length
End Sub

Private Function GetInt(ByVal f As Integer) As Long
Dim c As Long, N As Long
c = GetC(f)
N = c * 256&
c = GetC(f)
N = N + c
GetInt = N
End Function

Private Function GetC(ByVal f As Integer) As Integer
Dim bt As String * 1
Get #f, , bt
GetC = Asc(bt)
End Function

Private Function first_marker(ByVal f As Integer) As Integer
Dim C1 As Integer, C2 As Integer
C1 = GetC(f)
C2 = GetC(f)
If C1 <> &HFF Or C2 <> M_SOI Then
first_marker = -1
Else
first_marker = C2
End If
End Function

Private Function next_marker(ByVal f As Integer) As Integer
Dim c As Integer, discarded_bytes As Integer

c = GetC(f)
While c <> &HFF
discarded_bytes = discarded_bytes + 1
c = GetC(f)
Wend
Do
c = GetC(f)
Loop While c = &HFF
If discarded_bytes <> 0 Then
next_marker = -1
Exit Function
End If
next_marker = c
End Function

====

(Remove the ns_ from my address to reply directly.)

-- Roger

-------------------------------
No list server? Manage an interactive mailing list
on your PC with Arrow! http://www.jadebox.com/arrow/

0 new messages