Getting the size of a GIF/JPG/PNG? 
Author Message
 Getting the size of a GIF/JPG/PNG?

Hi!

I need code to get the size of an image in VB. I found similar code for Delphi, but haven't gotten around to converting it to VB - it uses some assembler code and
while delphi allows merging this code, VB doesn't. To make matters worse, I don't know Delphi that well.
Maybe there's a dynamic link library or ActiveX for these purposes?

Thanks a lot in advance!

Here's that weird code :-)

---

unit ImgSize; interface uses Classes; procedure GetJPGSize(const sFile: string; var wWidth, wHeight: word);
procedure GetPNGSize(const sFile: string; var wWidth, wHeight: word);
procedure GetGIFSize(const sGIFFile: string; var wWidth, wHeight: word); implementation uses SysUtils; function ReadMWord(f: TFileStream): word;
type
  TMotorolaWord = record
    case byte of
      0: (Value: word);
      1: (Byte1, Byte2: byte);
  end;
var
  MW: TMotorolaWord;
begin
  { It would probably be better to just read these two bytes in normally }
  { and then do a small ASM routine to swap them.  But we aren't talking }
  { about reading entire files, so I doubt the performance gain would be }
  { worth the trouble. }
  f.Read(MW.Byte2, SizeOf(Byte));
  f.Read(MW.Byte1, SizeOf(Byte));
  Result := MW.Value;
end; procedure GetJPGSize(const sFile: string; var wWidth, wHeight: word);
const
  ValidSig : array[0..1] of byte = ($FF, $D8);
  Parameterless = [$01, $D0, $D1, $D2, $D3, $D4, $D5, $D6, $D7];
var
  Sig: array[0..1] of byte;
  f: TFileStream;
  x: integer;
  Seg: byte;
  Dummy: array[0..15] of byte;
  Len: word;
  ReadLen: LongInt;
begin
  FillChar(Sig, SizeOf(Sig), #0);
  f := TFileStream.Create(sFile, fmOpenRead);
  try
    ReadLen := f.Read(Sig[0], SizeOf(Sig));     for x := Low(Sig) to High(Sig) do
      if Sig[x] <> ValidSig[x] then ReadLen := 0;     if ReadLen > 0 then
    begin
      ReadLen := f.Read(Seg, 1);
      while (Seg = $FF) and (ReadLen > 0) do
      begin
        ReadLen := f.Read(Seg, 1);
        if Seg <> $FF then
        begin
          if (Seg = $C0) or (Seg = $C1) or (Seg = $C2) then
          begin
            ReadLen := f.Read(Dummy[0], 3); { don't need these bytes }
            wHeight := ReadMWord(f);
            wWidth := ReadMWord(f);
          end else begin
            if not (Seg in Parameterless) then
            begin
              Len := ReadMWord(f);
              f.Seek(Len-2, 1);
              f.Read(Seg, 1);
            end else
              Seg := $FF; { Fake it to keep looping. }
          end;
        end;
      end;
    end;
  finally
    f.Free;
  end;
end; procedure GetPNGSize(const sFile: string; var wWidth, wHeight: word);
type
  TPNGSig = array[0..7] of byte;
const
  ValidSig: TPNGSig = (137,80,78,71,13,10,26,10);
var
  Sig: TPNGSig;
  f: tFileStream;
  x: integer;
begin
  FillChar(Sig, SizeOf(Sig), #0);
  f := TFileStream.Create(sFile, fmOpenRead);
  try
    f.Read(Sig[0], SizeOf(Sig));
    for x := Low(Sig) to High(Sig) do
      if Sig[x] <> ValidSig[x] then exit;
    f.Seek(18, 0);
    wWidth := ReadMWord(f);
    f.Seek(22, 0);
    wHeight := ReadMWord(f);
  finally
    f.Free;
  end;
end; procedure GetGIFSize(const sGIFFile: string; var wWidth, wHeight: word);
type
  TGIFHeader = packed record
    Sig: array[0..5] of char;
    ScreenWidth, ScreenHeight: word;
    Flags, Background, Aspect: byte;
  end;   TGIFImageBlock = packed record
    Left, Top, Width, Height: word;
    Flags: byte;
  end;
var
  f: file;
  Header: TGifHeader;
  ImageBlock: TGifImageBlock;
  nResult: integer;
  x: integer;
  c: char;
  DimensionsFound: boolean;
begin
  wWidth  := 0;
  wHeight := 0;   if sGifFile = '' then
    exit;   {$I-}
  FileMode := 0;   { read-only }
  AssignFile(f, sGifFile);
  reset(f, 1);
  if IOResult <> 0 then
    { Could not open file }
    exit;   { Read header and ensure valid file. }
  BlockRead(f, Header, SizeOf(TGifHeader), nResult);
  if (nResult <> SizeOf(TGifHeader)) or (IOResult <> 0) or
     (StrLComp('GIF', Header.Sig, 3) <> 0) then
  begin
    { Image file invalid }
    close(f);
    exit;
  end;   { Skip color map, if there is one }
  if (Header.Flags and $80) > 0 then
  begin
    x := 3 * (1 SHL ((Header.Flags and 7) + 1));
    Seek(f, FilePos(f) + x);
    if IOResult <> 0 then
    begin
      { Color map thrashed }
      close(f);
      exit;
    end;
  end;   DimensionsFound := False;
  FillChar(ImageBlock, SizeOf(TGIFImageBlock), #0);
  { Step through blocks. }
  BlockRead(f, c, 1, nResult);
  while (not EOF(f)) and (not DimensionsFound) do
  begin
    case c of
      ',': { Found image }
        begin
          BlockRead(f, ImageBlock, SizeOf(TGIFImageBlock), nResult);
          if nResult <> SizeOf(TGIFImageBlock) then begin
            { Invalid image block encountered }
            close(f);
            exit;
          end;
          wWidth  := ImageBlock.Width;
          wHeight := ImageBlock.Height;
          DimensionsFound := True;
        end;
      '?' : { Skip }
        begin
          { NOP }
        end;
    { nothing else.  just ignore }
    end;
    BlockRead(f, c, 1, nResult);
  end;   close(f);
  {$I+}
end; end.



Mon, 07 Jun 2004 20:16:48 GMT  
 Getting the size of a GIF/JPG/PNG?

Quote:

>I need code to get the size of an image in VB. I found similar code for
>Delphi, but haven't gotten around to converting it to VB - it uses some
>assembler code and while Delphi allows merging this code, VB doesn't.
>To make matters worse, I don't know Delphi that well.
>Maybe there's a dynamic link library or ActiveX for these purposes?

Euh... I can remember that there was an old VB code (VB3) and 16-bit DLL
combination available for free. It was written by Joe Oliphant. Of
course, you won't be able to use the DLL in combination with 32 bit
VB's, but at least, the VB code should be easily adaptable. And the DLL
only contained the routines to display the images, the rest, such as
determining the image sizes, was in plain VB.

... I found a page for downloading it:
<http://www.programmersheaven.com/search/download.asp?FileID=119>

Oh, when adapting the code, you'll see that VB3 did not have the BYTE
data type yet. A workaround using "string * 1" was used, with Chr$() to
change it and Asc() to get out the byte value.

Apparently, PiNG wan't widely used back then, if it already existed at
all.

--
        Bart.



Mon, 07 Jun 2004 21:23:45 GMT  
 Getting the size of a GIF/JPG/PNG?


Quote:
>I need code to get the size of an image in VB.

CImageInfo from www.davidcrowell.com

Gives you the code for getting image type, dimensions, and color depth
from JPG, PNG, BMP, PCX, TIFF, and GIF files.

--
Richard Mason



Mon, 07 Jun 2004 20:47:23 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Getting size of jpg/gif files

2. statistics as png/gif/jpg

3. Control wanted: display PNG/BMP/JPG/GIF

4. *** ImgX 2 Released *** JPG, GIF, BMP, TGA, TIF, PCX, PNG, EMF and WMF

5. How to get size of GIF/JPG?

6. help with gif and jpg size

7. GIF/JPG image size

8. Convert PNG to JPG or BMP

9. save as gif or png

10. save gif or png file indexed color

11. GIF, PNG creation dll?

12. SavePicture in GIF or PNG format?

 

 
Powered by phpBB® Forum Software