简体   繁体   English

如果Delphi应用程序有许多TImage组件,我怎么能保持它的大小?

[英]How can I keep the size of a Delphi application small when it has many TImage components?

I want to make an application using Delphi. 我想用Delphi制作一个应用程序。 The problem is I am going to have a lot of TImage components in it - which will make the exe size extremely large. 问题是我将有很多TImage组件 - 这将使exe大小非常大。 So far my exe is 20MB large and I have only completed the home page, by my calculations the exe alone will be more than 10GB in size - which is way too much. 到目前为止,我的exe是20MB大,我只完成了主页,根据我的计算,exe单独的大小将超过10GB - 这太过分了。

How can I make an app with hundreds/thousands of images and yet let the exe be small in size? 如何制作具有数百/数千张图像的应用程序,同时让exe的尺寸变小?

Don't put the images in the executable. 不要将图像放在可执行文件中。 Keep them in external files and load them at runtime. 将它们保存在外部文件中并在运行时加载它们。

This will require you to stop loading the images into the TImage controls at design-time and instead use dynamic code to load the image, and transfer to the TImage control. 这将要求您在设计时停止将图像加载到TImage控件中,而是使用动态代码加载图像,然后传输到TImage控件。 There are many advantages of doing so: 这样做有很多好处:

  1. You keep your executable down to a reasonable size, one that can fit in the virtual address space. 您可以将可执行文件保持在合理的大小,可以放入虚拟地址空间。
  2. You can use the most appropriate compression format for the images. 您可以为图像使用最合适的压缩格式。 If I recall correctly, a Delphi TImage will persists to a .dfm file in a very inefficient way. 如果我没记错的话,Delphi TImage将以非常低效的方式持久存在.dfm文件中。 You can choose to use PNG or JPEG compression. 您可以选择使用PNG或JPEG压缩。
  3. Keeping the files external allows you to better managed them when developing. 保持文件外部允许您在开发时更好地管理它们。 You can keep them as separate files in your revision control system. 您可以将它们作为单独的文件保存在修订控制系统中。 Storing images in .dfm files makes for painstaking updates when you need to change the images. 将图像存储在.dfm文件中可以在需要更改图像时进行艰苦的更新。 You have to do lots of interaction with the IDE. 您必须与IDE进行大量交互。 Storing as files allows you to overwrite the file, and commit to the repository. 存储为文件允许您覆盖文件,并提交到存储库。

If you don't need to keep your images in their original size, you can read them in at program startup and scale them to their "working" size. 如果您不需要将图像保持原始大小,可以在程序启动时读取它们并将它们缩放到“工作”大小。 This will reduce your memory requirements immensely - for example, reducing an image to half its original width and height reduces memory requirements to a quarter of the original image. 这将极大地降低您的内存需求 - 例如,将图像缩小到原始宽度和高度的一半会将内存需求降低到原始图像的四分之一。

If the images don't change, you can do this offline in a separate build process and reduce the load time a lot too. 如果图像没有更改,您可以在单独的构建过程中脱机执行此操作,并减少加载时间。 Otherwise, you can cache the reduced images during the load process to speed it up the next time the program is started. 否则,您可以在加载过程中缓存缩小的图像,以便在下次启动程序时加快速度。

You might find the binary code includes all the images as PNG file format, so it will be compile extremely application size for you. 您可能会发现二进制代码包含所有图像为PNG文件格式,因此它将为您编译极其适用的应用程序。

I use the images in runtime, as JPG, so in the form's OnCreate events you can load from the Package. 我在运行时使用图像作为JPG,因此在表单的OnCreate事件中,您可以从Package加载。

Use the "Project" Menu "Deployment" to include your JPG files to the APK. 使用“项目”菜单“部署”将您的JPG文件包含到APK。

You can read more here: http://blogs.embarcadero.com/davidi/2013/11/23/43005/ 你可以在这里阅读更多内容: http//blogs.embarcadero.com/davidi/2013/11/23/43005/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM