Newsletter sign-up
View all newsletters

Sign up for our Enterprise Java Newsletter

Enterprise Java

Java Tip 60: Saving bitmap files in Java

A tutorial -- including all the code you need to write a bitmap file from an image object

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
This tip complements Java Tip 43, which demonstrated the process of loading bitmap files in Java applications. This month, I follow up with a tutorial on how to save images in 24-bit bitmap files and a code snip you can use to write a bitmap file from an image object.

The ability to create a bitmap file opens many doors if you're working in a Microsoft Windows environment. On my last project, for example, I had to interface Java with Microsoft Access. The Java program allowed the user to draw a map on the screen. The map was then printed in a Microsoft Access report. Because Java doesn't support OLE, my only solution was to create a bitmap file of the map and tell the Microsoft Access report where to pick it up. If you've ever had to write an application to send an image to the clipboard, this tip may be of use to you -- especially if this information is being passed to another Windows application.

The format of a bitmap file

The bitmap file format supports 4-bit RLE (run length encoding), as well as 8-bit and 24-bit encoding. Because we're only dealing with the 24-bit format, let's take a look at the structure of the file.

The bitmap file is divided into three sections. I've laid them out for you below.

Section 1: Bitmap file header

This header contains information about the type size and layout of the bitmap file. The structure is as follows (taken from a C language structure definition):

typedef struct tagBITMAPFILEHEADER {
   UINT bfType;
   DWORD bfSize;
   UINT bfReserved1;
   UINT bfReserved2;
   DWORD bfOffBits;
}BITMAPFILEHEADER;


Here's a description of the code elements from the above listing:

  • bfType: Indicates the type of the file and is always set to BM.
  • bfSize: Specifies the size of the whole file in bytes.
  • bfReserved1: Reserved -- must be set to 0.
  • bfReserved2: Reserved -- must be set to 0.
  • bfOffBits: Specifies the byte offset from the BitmapFileHeader to the start of the image.


Here you've seen that the purpose of the bitmap header is to identify the bitmap file. Every program that reads bitmap files uses the bitmap header for file validation.

Section 2: Bitmap information header

The next header, called the information header, contains all the properties of the image itself.

Here's how you specify information about the dimension and the color format of a Windows 3.0 (or higher) device independent bitmap (DIB):

typedef struct tagBITMAPINFOHEADER {
    DWORD biSize;
    LONG  biWidth;
    LONG  biHeight;
    WORD  biPlanes;
    WORD  biBitCount;
    DWORD biCompression;
    DWORD biSizeImage;
    LONG  biXPelsPerMeter;
    LONG  biYPelsPerMeter;
    DWORD biClrUsed;
    DWORD biClrImportant;
} BITMAPINFOHEADER;


Each element of the above code listing is described below:

  • biSize: Specifies the number of bytes required by the BITMAPINFOHEADER structure.
  • biWidth: Specifies the width of the bitmap in pixels.
  • biHeight: Specifies the height of the bitmap in pixels.
  • biPlanes: Specifies the number of planes for the target device. This member must be set to 1.
  • biBitCount: Specifies the number of bits per pixel. This value must be 1, 4, 8, or 24.
  • biCompression: Specifies the type of compression for a compressed bitmap. In a 24-bit format, the variable is set to 0.
  • biSizeImage: Specifies the size in bytes of the image. It is valid to set this member to 0 if the bitmap is in the BI_RGB format.
  • biXPelsPerMeter: Specifies the horizontal resolution, in pixels per meter, of the target device for the bitmap. An application can use this value to select a bitmap from a resource group that best matches the characteristics of the current device.
  • biYPelsPerMeter: Specifies the vertical resolution, in pixels per meter, of the target device for the bitmap.
  • biClrUsed: Specifies the number of color indexes in the color table actually used by the bitmap. If biBitCount is set to 24, biClrUsed specifies the size of the reference color table used to optimize performance of Windows color palettes.
  • biClrImportant: Specifies the number of color indexes considered important for displaying the bitmap. If this value is 0, all colors are important.


Now all the information needed to create the image has been defined.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (2)
Login
Forgot your account info?

ye, you absolutly right! There is some bug in code!By Anonymous on February 6, 2010, 2:37 pmye, you absolutly right! There is some bug in code!

Reply | Read entire comment

Does not work with windows image fax viewerBy Anonymous on October 23, 2008, 8:42 amHi, But unfortunately, the BMP drawn using this code does not open up with windows image and fax viewer in Windows XP. Not very sure of the reasons...I am...

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources