如何用Delphi获取图像的大小(以字节为单位)?

发布于 2024-08-11 21:18:26 字数 656 浏览 1 评论 0原文

我的应用程序有 3 个控件:TThumbnailList(来自 TMS 组件的图像查看器)、TImage 和 TLabel。我希望当我将图像从 TThumbnailList 拖动到 TImage 并将其放下时,TLabel 对象会显示该图像的大小(以字节为单位)。我如何得到这个? 提前致谢。

procedure TForm1.AssignImage;
begin
  //tl: TThumbnailList
  if (tl.ItemIndex>=0) then begin
    Image1.Picture.Assign(tl.Thumbnails.Items[tl.ItemIndex].Picture);
  end;
end;

procedure TForm1.Image1DragDrop(Sender, Source: TObject; X, Y: Integer);
begin
  if (Source is TThumbnailList) then AssignImage;
end;

procedure TForm1.Image1DragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState;
  var Accept: Boolean);
begin
  Accept:= Source is TThumbnailList;
end;

my app has 3 controls: TThumbnailList (image viewer from TMS components), TImage, and TLabel. I wish that when I drag an image from TThumbnailList to TImage, and drop it, the TLabel object shows the size in bytes for that image. How do I get this?
Thanks in advance.

procedure TForm1.AssignImage;
begin
  //tl: TThumbnailList
  if (tl.ItemIndex>=0) then begin
    Image1.Picture.Assign(tl.Thumbnails.Items[tl.ItemIndex].Picture);
  end;
end;

procedure TForm1.Image1DragDrop(Sender, Source: TObject; X, Y: Integer);
begin
  if (Source is TThumbnailList) then AssignImage;
end;

procedure TForm1.Image1DragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState;
  var Accept: Boolean);
begin
  Accept:= Source is TThumbnailList;
end;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤千羽 2024-08-18 21:18:26

TImage 没有任何方法来确定它在给定时间所持有的任何图形的大小。那不是它的工作。它的工作只是显示东西。 TGraphic 对象管理内存中的表示,并且还确定如何将自身绘制到给定的画布上。 TImage 真的什么都不知道。 TGraphic 可能会,但它不一定会跟踪文件大小;这可能与将数据存储在内存中所需的内存量不同。

确定文件大小的方法是拥有一个文件或类似文件的东西,例如流。正如您在评论中提到的,您可以将图像保存到流中,然后找出流的大小。

function GetGraphicSize(g: TGraphic): Integer;
var
  ms: TStream;
begin
  ms := TMemoryStream.Create;
  try
    g.SaveToStream(ms);
    Result := ms.Size;
  finally
    ms.Free;
  end;
end;

如果每次需要尺寸时计算成本太高,那么请记住第一次看到图像时的尺寸,这样您就不需要每次都重新计算。缩略图列表是如何开始获取图像的?如果它们来自文件,那么您可以在生成缩略图时简单地获取文件大小。

TImage does not have any way to determine the size of whatever graphic it happens to be holding at a given time. That's not its job. Its job is only to display things. The TGraphic object manages the in-memory representation, and it also determines how to draw itself onto a given canvas. TImage really knows nothing at all. TGraphic might, but it doesn't necessarily keep track of the file size; that might be different from the amount of memory necessary to have the data in memory.

The way to determine the size of a file is to have a file or something file-like, such as a stream. As you mentioned in your comment, you can save the image to a stream and then find out the size of the stream.

function GetGraphicSize(g: TGraphic): Integer;
var
  ms: TStream;
begin
  ms := TMemoryStream.Create;
  try
    g.SaveToStream(ms);
    Result := ms.Size;
  finally
    ms.Free;
  end;
end;

If that's too costly to compute each time you need the size, then remember the size from the first time you see the image so you don't need to computer it anew each time. How did the thumbnail list get its images to begin with? If they came from files, then you could have simply fetched the file size as you were generating the thumbnails.

箜明 2024-08-18 21:18:26

您可以使用以下函数来执行文件搜索并返回大小。

function FindFileSize(Filename:string):integer;
var
  sr : TSearchRec;
begin
  if FindFirst(filename,faAnyFile-faDirectory,sr) = 0 then
    Result := sr.Size
  else
    raise EFileNotFoundException.Create(filename+' not found.');
  FindClose(sr);
end;

You can use the following function to perform a search for the file and return the size.

function FindFileSize(Filename:string):integer;
var
  sr : TSearchRec;
begin
  if FindFirst(filename,faAnyFile-faDirectory,sr) = 0 then
    Result := sr.Size
  else
    raise EFileNotFoundException.Create(filename+' not found.');
  FindClose(sr);
end;
水染的天色ゝ 2024-08-18 21:18:26

等等,如果我没记错的话,这就是你想要的

// this will return the size of the bitmap in bytes!
function BitmapSize(ABitmap: TBitmap): Cardinal;
var ms : TMemoryStream;
begin
     ms := TMemoryStream.Create;
     ABitmap.savetostream(ms);
     result := ms.size;
     FreeAndNil(ms);
end;

玩得开心,别忘了访问 http:// delphigeist.blogspot.com/ 如果答案有帮助!

blah, if I'm not mistaken, this is what you want

// this will return the size of the bitmap in bytes!
function BitmapSize(ABitmap: TBitmap): Cardinal;
var ms : TMemoryStream;
begin
     ms := TMemoryStream.Create;
     ABitmap.savetostream(ms);
     result := ms.size;
     FreeAndNil(ms);
end;

Have fun, don't forget to visit http://delphigeist.blogspot.com/ if the answer is helpful!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文