Should I load PNG images from files or from resources of my assembly?
Posted on April 2, 2015
If you want a Bee Mobile control to display a PNG image, you usually have two choices. You can either add the PNG image to the control from your project’s resources or from a file (some controls provide even more possibilities, e.g., you can load the PNG image from a byte array or from a MemoryStream).
Say for example you want a TVistaButton to display your PNG image. There are two properties for this:
ImageFileIcon accepts the name of .PNG file it should load into TVistaButton. The file has to be in the same folder as your application or in a sub-folder. If it’s in a sub-folder, the sub-folder name has to precede the name of the PNG file in the assigned string, e.g.:
tVistaButton1.ImageFileIcon = @"myImages\icon1.png";
ImageResxIcon accepts the name of PNG image in project’s resources, e.g.:
tVistaButton1.ImageFileIcon = "icon1.png";
Interesting note: These properties can’t be used both at the same time, because setting one of them causes the other one to bear a value of String.Empty (or “”).
If you use Visual Studio Designer (“VSD”) to assign a value to these properties, then VSD will do this:
- In case of ImageFileIcon property, VSD will add the .png file to your project, set its Build Action to Content and set Copy To Output Directory to ‘Copy If Newer’. That will ensure the .png file is copied along with your application to the target directory and that the application will be able to find the file(s) and use it/them so you do not need to change any properties of the file.
- In case of ImageResxIcon property, VSD will automatically add the file to your project’s resources and set ImageResxIcon accordingly. It will also set Build Action ‘Embedded Resource’ so that Visual Studio compiler (or rather resource generator which is a tool run prior to actual compilation) “builds” the file into your application’s resources (I quoted the word “builds”, because it is not really a build in the same terms as source code is built, but it is just a resource generation process – for more information on this see the detailed view of your output window when building your project). Thus VSD makes sure that the .png file will become part of your app’s resources and TVistaButton will be able to find that file during the app’s run-time. VSD will also set Copy To Output Directory to ‘Do not copy’, because it is not necessary to copy the .png file to your device, since the .png file is already built into your app’s resources.
Both approaches have their pros and cons. The pros of using ImageFileIcon is the fact, that the images are deployed as separate files to your device, which gives you the freedom to operate them, eventually substitute them with other .png files during the run-time of your application. The con is that you provide direct access to your image files to the user of your app which may not be desirable at times.
When the images are part of the resources of your application, they actually become part of your app’s .exe (or .dll) and user does not have direct access to them. Also, the application may appear more compact in the terms that it consists of fewer files.
TForm is different
However, TForm behaves differently from what is described above. TForm accepts not only .png files, but also .bmp and .jpg files. If you assign a background image to TForm using its BackgroundImage property, the image will become part of TForm’s own resources automatically and it will not appear in Solution Explorer. Hence, there is no need to deploy the image file to your device or making sure it’s part of your project’s resources.