Loading images from files
To load an image from a file, the loadFromFileHandle:atOffset: message is sent to an instance of the appropriate subclass of CgImageFileFormat. The first argument is a CfsFileDescriptor, opened for reading on the input file. The atOffset: argument is the number of bytes from the start of the file at which to start reading. The result is a CgDeviceIndependentImage if the file was successfully loaded, or nil if an error occurred.
The following example illustrates how to load a PCX image from a file named my-image.pcx:
| format file image |
format := CgPCXFileFormat new.
file := CfsFileDescriptor open: 'my-image.pcx' oflag: ORDONLY.
image := format loadFromFileHandle: file atOffset: 0.
file close.
^image
The loadFromFile: method takes a file name directly, saving the application from having to explicitly open and close the file. The following code has the same result as the previous example:
| format image |
format := CgPCXFileFormat new.
image := format loadFromFile: 'my-image.pcx'.
^image
Last modified date: 01/29/2015