0

I have created a simple Opendialog, Edit1 with show message

I don't know why my function return:

[DCC Error] Unit1.pas(112): E2010 Incompatible types: 'string' and 'tagSIZE'

The complete code is:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,Types, ExtDlgs;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    Label1: TLabel;
    Memo1: TMemo;
    OpenDialog1: TOpenDialog;
    procedure Button1Click(Sender: TObject);

  private
    { Private declarations }
  public
//      procedure GetGIFSize(const sGIFFile: string; var wWidth, wHeight: Word);

  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
function GetGIFSize(const FileName: string): Windows.TSize;
type
  // GIF header record
  TGIFHeader = packed record
    Sig: array[0..5] of AnsiChar;     // signature bytes
    ScreenWidth, ScreenHeight: Word;  // logical screen width and height
    Flags: Byte;                      // various flags
    Background: Byte;                 // background colour index
    Aspect: Byte;                     // pixel aspect ratio
  end;
  // GIF image block header record
  TGIFImageBlock = packed record
    Left, Top: Word;      // image top left
    Width, Height: Word;  // image dimensions
    Flags: Byte;          // flags and local colour table size
  end;
const
  cSignature: PAnsiChar = 'GIF';  // gif image signature
  cImageSep = $2C;                // image separator byte
var
  FS: Classes.TFileStream;      // stream onto gif file
  Header: TGIFHeader;           // gif header record
  ImageBlock: TGIFImageBlock;   // gif image block record
  BytesRead: Integer;           // bytes read in a block read
  Offset: Integer;              // file offset to seek to
  B: Byte;                      // a byte read from gif file
  DimensionsFound: Boolean;     // flag true if gif dimensions have been read
begin
  Result.cx := 0;
  Result.cy := 0;
  if (FileName = '') or not SysUtils.FileExists(FileName) then
    Exit;
  FS := Classes.TFileStream.Create(
    FileName, SysUtils.fmOpenRead or SysUtils.fmShareDenyNone
  );
  try
    // Check signature
    BytesRead := FS.Read(Header, SizeOf(Header));
    if (BytesRead <> SizeOf(TGIFHeader)) or
      (SysUtils.StrLComp(cSignature, Header.Sig, 3) <> 0) then
      // Invalid file format
      Exit;
    // Skip colour map, if there is one
    if (Header.Flags and $80) > 0 then
    begin
      Offset := 3 * (1 shl ((Header.Flags and 7) + 1));
      if Offset >= FS.Size then
        Exit;
      FS.Seek(Offset, Classes.soFromBeginning);
    end;
    DimensionsFound := False;
    FillChar(ImageBlock, SizeOf(TGIFImageBlock), #0);
    // Step through blocks
    FS.Read(B, SizeOf(B));
    while (FS.Position < FS.Size) and (not DimensionsFound) do
    begin
      if B = cImageSep then
      begin
        // We have an image block: read dimensions from it
        BytesRead := FS.Read(ImageBlock, SizeOf(ImageBlock));
        if BytesRead <> SizeOf(TGIFImageBlock) then
          // Invalid image block encountered
          Exit;
        Result.cx := ImageBlock.Width;
        Result.cy := ImageBlock.Height;
        DimensionsFound := True;
      end;
      FS.Read(B, SizeOf(B));
    end;
  finally
    FS.Free;
  end;
end;


procedure TForm1.Button1Click(Sender: TObject);
var
  Size: Windows.TSize;
begin
    Size := GetGIFSize('file.gif');
    ShowMessage(Size);
  end;
end.

I use simply:

GetGIFSize(path/to/filename);

But Filename is a string, have you any idea why is not working?

4

2 回答 2

3

问题出在您的TForm1.ButtonClick事件中,与ShowMessage电话有关。ShowMessage接受一个字符串参数(要显示的消息),而您将 a 传递给它Windows.TSize

您需要将TSize记录转换为字符串才能与ShowMessage. ATSize有两个维度 - 由 表示的宽度TSize.cx和由 表示的高度,TSize.cy因此您需要将这些维度转换为可显示的字符串表示形式:

procedure TForm1.Button1Click(Sender: TObject);
var
  Size: Windows.TSize;
const
  SizeDisplayMsg = 'Size - width (cx): %d height (cy): %d';
begin
    Size := GetGIFSize('file.gif');
    ShowMessage(Format(SizeDisplayMsg, [Size.cx, Size.cy]));
end;

当然,如果你想使用TOpenFileDialog来获取文件名,你应该使用它来代替:

procedure TForm1.Button1Click(Sender: TObject);
var
  Size: Windows.TSize;
const
  SizeDisplayMsg = 'Size - width (cx): %d height (cy): %d';
begin
  if OpenDialog1.Execute(Handle) then
  begin
    Size := GetGIFSize(OpenDialog1.FileName);
    ShowMessage(Format(SizeDisplayMsg, [Size.cx, Size.cy]));
  end;
end;
于 2013-05-11T00:49:59.687 回答
2

ShowMessage过程将其唯一的参数类型值作为其唯一的参数string类型值,但您试图向那里传递一条Windows.TSize记录。这就是为什么编译器拒绝使用此类消息进行编译的原因。此外,Windows.TSize记录类型由 2 个字段组成;fromcxcywhere each 都是数字类型,所以除了你需要单独传递它们之外,你需要在将它们传递到ShowMessage过程之前将它们的值转换为字符串。您可以通过多种方式做到这一点,例如:

1. 使用 Format 函数(首选方式)

procedure TForm1.Button1Click(Sender: TObject);
var
  Size: Windows.TSize;
begin
  Size := GetGIFSize('c:\File.gif');
  ShowMessage(Format('Width: %d; Height: %d', [Size.cx, Size.cy]));
end;

2.手动拼接字符串(可读性更差)

procedure TForm1.Button1Click(Sender: TObject);
var
  Size: Windows.TSize;
begin
  Size := GetGIFSize('c:\File.gif');
  ShowMessage('Width: ' + IntToStr(Size.cx) + '; Height: ' + IntToStr(Size.cy));
end;
于 2013-05-11T00:14:06.010 回答