Ver Mensaje Individual
  #4  
Antiguo 10-03-2008
Avatar de seoane
[seoane] seoane is offline
Miembro Premium
 
Registrado: feb 2004
Ubicación: A Coruña, España
Posts: 3.717
Reputación: 26
seoane Va por buen camino
El código está un poco sucio pero quedaría mas o menos así:
Código Delphi [-]
type
  // No estan todos los elementos de la estructura
  TSOF = packed record
    Precision: Byte;
    Height: Word;
    Width: Word;
  end;
  PSOF = ^TSOF;

function GetJpegSize(Src: TStream; var Width: Integer; var Height: Integer): Boolean;
var
  Buffer: PByteArray;
  Size: Integer;
begin
  Result:= FALSE;
  GetMem(Buffer,Sizeof(TSOF));
  try
    while TRUE do
    begin
      FillChar(Buffer^,4,#0);
      if Src.Read(Buffer^,4) <> 4 then Break;
      Size:= ((Buffer[2] shl 8) + Buffer[3]) - 2;
      case Buffer[1] of
        $01,$D0..$D8:
          Src.Seek(-2,soFromCurrent);
        $C0: begin
            FillChar(Buffer^,Sizeof(TSOF),#0);
            if Src.Read(Buffer^,Sizeof(TSOF)) = Sizeof(TSOF) then
            begin
              Result:= TRUE;
              Width:= Swap(PSOF(Buffer).Width);
              Height:= Swap(PSOF(Buffer).Height);
            end;
            break;
          end;
        else
          Src.Seek(Size,soFromCurrent);
      end;
    end;
  finally
    FreeMem(Buffer);
  end;
end;

function GetJpegSizeFromFile(Filename: String; var Width: Integer;
  var Height: Integer): Boolean;
var
  Stream: TFileStream;
begin
  Result:= FALSE;
  Stream:= TFileStream.Create(Filename,fmOpenRead);
  try
    try
      Result:= GetJpegSize(Stream,Width,Height);
    except
    end;
  finally
    Stream.Free;
  end;
end;

// Un ejemplo de como usar la funcion
var
  x,y: Integer;
begin
  if GetJpegSizeFromFile('d:\1.jpg',x,y) then
    ShowMessage(Format('%d,%d',[x,y]));
end;
Responder Con Cita