Summary
Although Java provides several mechanisms for opening an image, saving the image is not its forte. This tip will tell you how to save an image in a 24-bit bitmap file. In addition, Jean-pierre provides all the code needed to write an image file to a bitmap file.
This tip complements the step-by-step guide to loading bitmap files in Java applications, which illustrates the process of loading bitmap files in a Java application. This month I'll provide a tutorial on how to save an image in a 24-bit bitmap file, which also contains snippets of code that write an image object to a bitmap file.
If you work in a Microsoft Windows environment, the ability to create bitmap files will provide you with a lot of convenience. For example, in my last project, I had to dock Java with Microsoft Access. Java programs allow users to draw on the screen. This image is then printed to the Microsoft Access report. Because Java does not support OLE, my only option is to create a bitmap file for the diagram and tell the Microsoft Access report where to find the bitmap file. If you have written an application that sends images to the Clipboard, this technique may be useful to you-especially if you pass this information to another application.
Format of bitmap files
The bitmap file format supports 4-bit RLE (stroke length encoding) and 8-bit and 24-bit encodings. Since we only deal with 24-bit formats, let's look at the structure of the file below.
A bitmap file is divided into three parts. I have listed them below.
Part 1th: The header of a bitmap file
The header contains the type size information and layout information for the bitmap file. The structure is as follows (excerpt from the C language structure definition):
typedef struct tagBITMAPFILEHEADER {
UINT bfType;
DWORD bfSize;
UINT bfReserved1;
UINT bfReserved2;
DWORD bfOffBits;
}BITMAPFILEHEADER;
The following is a description of the code elements in this list:
Bftype: Specifies the file type whose value is always BM.
Bfsize: Specifies the size, in bytes, of the entire file.
BFRESERVED1: Reserved--Must be 0.
BFRESERVED2: Reserved--Must be 0.
Bfoffbits: Specifies the byte offset from Bitmapfileheader to the header of the image.
Now that you understand the purpose of the bit icon header is to identify the bitmap file. Each program that reads a bitmap file uses a bit icon header for file verification.