德尔福
我创建了一个简单的Opendialog,EDIT1与展会消息图像尺寸德尔福
我不知道为什么我的函数返回:
[DCC Error] Unit1.pas(112): E2010 Incompatible types: 'string' and 'tagSIZE'
完整的代码是:
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.
我用简单:
GetGIFSize(路径/到/文件名);
但文件名是一个字符串,你有任何想法,为什么不工作?
的ShowMessage
程序作为其唯一的参数string
类型值,但你试图通过那里Windows.TSize
记录。这就是为什么编译器拒绝使用此类消息进行编译的原因。此外,Windows.TSize
记录类型由2个字段组成;从cx
和cy
其中每个是数字式的,所以只是你需要通过他们分别,你需要将它们传递到ShowMessage
过程之前,它们的值转换为字符串。您可以通过多种方式进行操作,例如由:
1.使用格式化功能(优选方式)
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;
谢谢@TLama和所有的好回应。我使用类Windows,和简单的功能,2解决方案是好的,但如果我给一个简单的showmessage(大小); Size:= GetGIFSize('c:\ File.gif');错误消息[DCC Error] Unit1.pas(113):E2010不兼容的类型:'string'和'tagSIZE'为什么? – Kate 2013-05-11 00:28:44
等等,我的回答根据你的评论是错误的(更新)。你为什么接受它? – TLama 2013-05-11 00:37:02
对不起,因为你帮我知道问题在哪里 – Kate 2013-05-11 00:41:27
的问题是你的TForm1.ButtonClick
事件,与ShowMessage
通话。 ShowMessage
接受一个字符串参数(该消息显示),而你传递一个Windows.TSize
代替。
您需要将TSize
记录转换为字符串才能与ShowMessage
一起使用。一个TSize
有两个维度 - 宽度,通过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;
你试图到'GetGIFSize'函数的结果分配给'string'类型的变量,而不是'Windows.TSize'。这是我们在你的问题中缺少的一段代码。 – TLama 2013-05-11 00:10:22
@TLama打我给它 – 2013-05-11 00:10:47
请张贴你得到确切的**,整个编译器错误**,沿加衬它与发生的事情,和** **确切您使用的是调用函数的代码。说“我简单地使用”还不够好(正如@TLama指出的那样)。 –
2013-05-11 00:11:55