Delphi GraphicsFile Formats & Conversions

TBitmap, BMP, DIB


TBitmap
Delphi Graphics VCL/RTL page

general info 1. In D1/D2, a bitmap was a DIB (device independent bitmap) after loading, but became a DDB (device dependent bitmap) on the first screen draw -- the DIB was tossed and the DDB format was kept. The pixel format could (and usually did) change across load/modify/save sequences.

In D3, Danny Thorpe (Borland) reimplemented TBitmap to use a DIBSection to solve the load/modify/save problem present in D1/D2. The use of DIBs provided direct access to pixel memory (using the Scanline property) and the bitmap handle at the same time.

2. In D3/D4 if you load an RLE compressed bitmap, it is loaded as a DDB, but when it gets written out, it is written an an uncompressed (screen pixel format) DIB.

3. Getting a strange compile-time error with a TBitmap? There's a TBitmap defined in both the Windows and Graphics unit and they may be in the wrong order in your USES statement. Normally you want the one from the Graphics unit (put in last in your USES), unless you're directly using API functions.]

4.  The standard windows glyphs are stored within comctl32.dll as bitmaps.

5.  TBitmap is defined in both windows.pas and graphics.pas units.  Since the compiler automatically recognizes the last one in the "Uses" statement, make sure "Graphics" follows "Windows" in the "Uses" statement.

Example of comparing two TBitmaps and counting number of pixels that match and differ.

Bitmaps and InterBase BLOB Fields (TI 797D)
Converting a BMP to a JPEG and Vice Versa (TI 4582D)
Creating a bitmap from a pixel array (FAQ 1205D)
Creating temporary canvas (FAQ 1277D)
Drawing transparent text onto a TBitmap (FAQ 2079D)
Extracting a Bitmap from A BLOB Field (TI 791D)
Extracting Icons (FAQ 1778D)

Handling bitmap display (FAQ 2418D) [size limitations]
See also Very Large Bitmap Lab Report

Delphi Pool TBitmap Tips
www.lmc-mediaagentur.de/dpool/graph04.htm 

How can I create a bitmap from an icon? (FAQ 2752D)
How can I place a bitmap in a metafile? (FAQ 1842D)
How to paint a form with a bitmap (TI 551D)
Loading Bitmaps and Cursors from RES Files (TI1081D) [explains palette problem]
Loading bitmaps into dBASE and Paradox BLOB Fields (TI 779D)
Non VCL Bitmap buffering (FAQ 2774D)
Stretchdraw on an icon (FAQ 1818D)
Using the Win API GetObject to get a bitmap (FAQ 2518D)

  How to create a Bitmap from numeric data? (D5)

Patrick Martin's example TBitmap descendant using memory mapped files

Background Bitmaps on Forms by Robert Vivrette
www.undu.com/DN961001/00000006.htm

Big Bitmap Viewer by Grahame Marsh
www.undu.com/DN970101/00000013.htm

Load Bitmap Resource by Ray Lischner. Look for bmpres10.zip www.tempest-sw.com/freeware or via ftp at ftp://ftp.tempest-sw.com/pub/delphi/bmpres10.zip

Storing bitmaps as resources, pp. 525-531, Delphi Programming Problem Solver, Neil Rubenking

"Displaying your bitmaps quickly", Delphi Developer's Journal, July 1998, pp. 12-15. (Double buffering using Pixels instead of Scanline. This technique is not very quick compared to using Scanline!) File 98Jul/DDJ9873.ZIP at ftp.zdjournals.com/ddj

"Eliminate flicker when painting on a form or a TPaintBox control, " www.bcbdev.com/faqs/faq34.htm

Mike Shkolnik's UseNet Post:   Bitmap to TGraphicField
Joe Hecht's UseNet Post with BltTBitmapAsDib procedure

Rund um TBitmap
http://delphi.pjh2.de/articles/graphic/bitmap.php

efg's UseNet Post about how to create TList of TBitmaps

Assign Borland example: ..\Delphi n\Demos\Doc\GraphEx.dpr
Transparent property to work for a CoolBar background bitmap (FAQ 1390D)
Extracting a Bitmap from A BLOB Field (TI 791D)
Converting a BMP to a JPEG and Vice Versa (TI 4582D)

efg's Note: There is a Very Subtle Bug/Feature in the Delphi 3/4 Assign when only using Scanline to access pixel data. Assign should always be a "deep copy" (all data copied) versus a "shallow copy" (only a pointer copied), but this isn't always true! The kludge fix is to copy a single pixel from one bitmap to the other using the Pixels property to force unique references. See Assign.Txt.  (Fixed in D5)
Create/Free Creating temporary canvas (FAQ 1277D)

Francesco Savastano's UseNet Post about initializing bitmap to specified color

Dormant Problem with complex implementation that allows DDB and DIB Section to exist simultaneously in Single TBitmap as described in UseNet Post by Takuo Nakamura.  This note gives an example of when Dormant is needed.

Nick Hodges' UseNet Post answering "How does TBitmap.Dormant work?"

Peter Haas' UseNet Post about using Bitmap.Dormant after setting PixelFormat to fix a problem in D1-D4 with the TBitmaInfoHeader

efg's example of converting TIcon to TBitmap, as well as extracting the icon's "AND" bitmap mask and the "OR" color bitmap.   [This is the first example that I needed "Dormant" but this solution only worked in Windows 2000.  An alternate approach was needed for Windows 98.  --efg]

See the Cursor Overlay Lab report for another Dormant example.

FreeImage Finn Tolderlund's UseNet Post about Scanline and FreeImage
HandleType
(D3/D4)
THandleType = (bmDDB, bmDIB)

In D3/D4 if you create a TBitmap and DO NOT assign either the PixelFormat or the HandleType, a DDB is created.  If you set HandleType := bmDIB, or if you specify any PixelFormat (other than pfDevice), you get a DIB. 

A BMP loaded from a file is a DIB (HandleType = bmDIB).

Problem with complex implementation that allows DDB and DIB Section to exist simultaneously in Single TBitmap as described in UseNet Post by Takuo Nakamura.  This note gives an example of when HandleType is needed.

LoadFromFile Transparent property to work for a CoolBar background bitmap (FAQ 1390D)
How can I place a bitmap in a metafile? (FAQ 1842D)

See \Program Files\Borland\Delphi n\HELP\Examples\Bitmap\BMPFormU.PAS

Using Bitmaps, p. 216, Delphi Component Design by Danny Thorpe:
"When you call TBitmap.LoadFromFile to load a BMP file into a bitmap object, the bitmap object keeps a copy of the original file data in a memory stream.  The bitmap does not create a bitmap handle unless you refer to the bitmap's Handle property in your own code."  [D2.  True in D3 and later?]]

Mask Shannon Broskie's UseNet Post with DrawTitleBarImage function
Monochrome Setting Monochrome to TRUE on a pf24bit bitmap changes the PixelFormat to pfDevice.

My observation is that all non-white colors become black when Monochome is set to TRUE on a pf24bit bitmap.

 

Palette efg's UseNet Post about how to create pf8bit TBitmap with 256 shades of gray.

Also see Delphi Graphics Color Page

PixelFormat (D3/D4) TPixelFormat = (pfDevice, pf1bit, pf4bit, pf8bit, pf15bit, pf16bit, pf24bit, pf32bit, pfCustom);

PixelFormat and Scanline are discussed at length in
Manipulating Pixels With Delphi's ScanLine Property 

Peter Haas' UseNet Post about using Bitmap.Dormant after setting PixelFormat to fix a problem in D1-D4 with the TBitmapInfoHeader

Ian Martin's UseNet Post about PhotoShop's problem with pf16bit bitmaps

See also Monochrome.

Resource Files See Resource Files on Delphi Graphics Algorithms page
SaveToClipboardFormat The example in the D3 online help compiled (with a warning), but the online help example of SaveToClipboardFormat has never been quite right and does not compile (see efg's UseNet Post about this).  Here's a version of the online example that will compile in D3..D7:
USES clipbrd;  // I wish Borland showed what unit is needed in the help
procedure TForm1.Button1Click(Sender: TObject);
var
  MyFormat : Word;
  Bitmap   : TBitMap;
  AData    : THandle;
  APalette : hPalette;  // Wrong in D3-D7 online example
begin
  Bitmap := TBitmap.Create;
  try
    Bitmap.LoadFromFile('c:\Program Files\Common Files\Borland Shared\Images\Splash\256color\factory.bmp');
    Bitmap.SaveToClipBoardFormat(MyFormat,AData,APalette);
    ClipBoard.SetAsHandle(MyFormat,AData);
  finally
    Bitmap.Free;
  end;
end;
SaveToFile Converting a BMP to a JPEG and Vice Versa (TI 4582D)
Scanline (D3) The PixelFormat defines the layout of a Scanline.

PixelFormat and Scanline are discussed at length in
Manipulating Pixels With Delphi's ScanLine Property

Scanline Enigma working with pf24bit Bitmaps (fixed in D4.02 and D5)

Finn Tolderlund's UseNet Post about Scanline and FreeImage

See also Monochrome.

Transparent
TransparentColor
TransparentMode
The Transparent property of a TBitmap works only if you use the Draw method to draw it on a canvas. If Transparent := TRUE, and TransparentMode := tmAuto, then the transparent color is the color of the bottom-leftmost pixel of the bitmap. If TransparentMode := tmFixed, then the transparent color is defined by the TransparentColor property.

Dynamically drawing a transparent image (FAQ 1794D)

Also see "Transparency" in Section B of Delphi Graphics Algorithms.

 


BMP and DIB File Formats

File Format Description/Comments
Archives

Bitmap Archives

Raize Button Glyphs
www.raize.com/DevTools/Tools/RzBmps.htm 

Glyphs und Icons im Internet
http://community.borland.com/article/0,1410,25911,00.html 

Images in BMP format -- 16800 images (8 MB)
www.eldos.org/files/bitmaps.zip 

9452 glyphs from Richey's Delphi-Box
http://inner-smile.com/dl_div.htm#glyph

More than 400 glyphs from MS Office Applications
ftp://ftp.scalabium.com/pub/officebtns.zip 

Raize Button Glyphs
www.raize.com/RzBmps.htm 

1000 glyphs for free
www.rwakelin.freeserve.co.uk/sharedfiles/bmpiconz.zip

Links to various bitmap/icon collections
www.torry.net/docs_graphics.htm 

BMP Bitmap and Metafile Functions, Tomes Graphical, pp. 273-356

Windows Bitmap used with Microsoft Paint
Delphi 3 or 4: ..\Demos\Imagview

To create a RES file with Bitmaps: 
Edit a file name.RC and add a line like this for each bitmap:
NAME  BITMAP  "NAME.BMP"
all in upper case.  Replace "NAME" above with the appropriate bitmap name.   Once you have the name.RC file, use the resource compiler:  BRC32 name.RC
The file name.RES will be created.  Add {$R name.RES} to the unit needing the resource.

Loading Bitmaps and Cursors from RES files (TI 1081D)
Tried TI 1081D and you now see the message "Bitmap Image is not valid"?  Be sure to add the comment {$R name.RES} to the unit where you're using LoadResource.

Example of comparing two TBitmaps and counting number of pixels that match and differ.

efg's Resource Demo application shows how to use the following file types in resource files:  BMP, ICO, JPG, GIF (requires Anders Melander's TGIFImage), EMF, TXT, AVI, WAV and cursors

Peter Haas' UseNet Post about determining a bitmap's size without loading the complete TBitmap, and Joe Hecht's Reply Post with certain precautions 

Bitmap Orientation and biHeight
www.webartz.com/fourcc/fccbihgt.htm

biHeight and Video Formats in DirectShow (problem with inverted video images)
http://www.microsoft.com/hwdev/desinit/biheight.htm 

Scanline Tech Note explains accessing pixel data quickly for the various PixelFormats

24 bits/pixel to 8 bits/pixel bitmaps using the ReduceColors function.  See Anders Melander's UseNet Post about using TGIFImage to reduce colors.

RGB Formats
www.webartz.com/fourcc/fccrgb.htm

Encryption / Decryption
CryptImage.jpg (948 bytes)   efg's ImageCrypt Lab Report:  BMP Section
efg's Aspect Ratio Lab Report

Resource-Grabber will scan the directories and drives on your computer and extract all Bitmaps, Glyphs (button images), Icons, Cursors, JPG/JPEG images, WAVE and RMI sound files as well as AVI video clips it finds inside the programs and DLL files in any directory of your choice.  http://inner-smile.com/dl_res.htm

Glyph Viewer facilitates the search in large Glyph directories.  In a list all bit-maps of a directory as well as their file names and creation dates are displayed. The list can be sorted according to file names and creation dates. In a tree representation you can select the desired directory. 
http://www.pics-software.de/swglyph.htm 

BMP Technical Tutorial: 
www.geocities.com/SiliconValley/Haven/7041/tp/showbmp.txt

Microsoft's "Error 481: Invalid Picture" Err Viewing BMP File

Danny Thorpe's UseNet Post about RLE BMPs
Rafe Aldridge's UseNet Post about Bug in D5 loading RLE BMPs
Bob Villiers' UseNet Post with VarArrayLoadFile, which loads a bitmap into a variant

efg's PelsPerMeter example with GetPelsPerMeter procedure to return biXPelsPerMeter and biYPelsPerMeter values, and an example based on a UseNet Post by Peter Klein that returns these same values.

Biff Kadidlehopper's UseNet Post to get/set the dpi in a bitmap file or bitmap image (the biXPelsPerMeter and biYPelsPerMeter values)

The biXPelsPerMeter is a 4-byte integer at an offset of 38 bytes from the start of the file; biYPelsPerMeter is a 4-byte integer at an offset of 42 bytes from the start of the file.

Example from Robert Poyntz with SpecialBMP routine to set the biXPelsPerMeter and biYPelsPerMeter values.  SpecialGIF procedure by Robert Poyntz with biXPelsPerMeter and biYPelsPerMeter example.  

Supported by ImageLib www.imagelib.com

 
DIB Windows Device-Independent Bitmap

Delphi Programming & Assembly Language (DIBULTRA)
http://perso.magic.fr/sleon/prog/DIBUltra/DIBUltraE.htm 

Joe Hecht's UseNet Post with BltTBitmapAsDib procedure

Chris Hill's UseNet Post explaining two ways the term DIB is used

efg's examples of working with DIBs while converting Lead Tool's BitmapHandle to TBitmap

Delphi Graphics Programming by Peter Dove and Don Peer in Delphi Informant:
Part IV, Apr 97, "Getting DIBs on Speed" (pp. 45-56)

DIBs. A class that provides easy access to Device Independent Bitmaps. Delphi Super Page: http://delphi.icm.edu.pl/ftp/d10free/dibs.zip

Wie ist eine DIB aufgebaut (How a DIB is structured)
http://delphi.pjh2.de/articles/graphic/bitmap.php#DIB

Supported by ImageLib www.imagelib.com

Non-Delphi:
DIBs and Their Use:
http://msdn.microsoft.com/library/techart/msdn_dibs2.htm 

Using Device-Independent Bitmaps and Palettes http://support.microsoft.com/support/kb/articles/q72/0/41.asp

"More Fun with MFC: DIBs, Palettes, Subclassing, and a Gamut of Reusable Goodies", Part I (Jan 97), www.microsoft.com/MSJ/0197/mfcp1/mfcp1.htm 
www.microsoft.com/msj/code1993to1997/MSJJAN97.EXE

More Fun With MFC: DIBs, Palettes, Subclassing and a Gamut of Goodies, Part II (Mar 97)
www.microsoft.com/msj/0397/mfcp2/mfcp2.htm
www.microsoft.com/msj/code1993to1997/MSJMAR97.EXE

More Fun with MFC: DIBs, Palettes, Subclassing, and a Gamut of Goodies, Part III (Jun 97)
www.microsoft.com/msj/0697/mfc3/mfc3.htm 


Conversions

Conversion(s) Description/Comments
411 to BMP efg's 411 Lab Report to read .411 file and convert to a TBitmap
AVI to BMPs

Eddie Shipman's UseNet Post with Example of how to convert the following "common" AVIs into BMPs:   FindFolder, iFindFile, FindComputer, CopyFiles,iCopyFile, RecycleFile, EmptyRecycle,   DeleteFile

Toni Martir's notes and complete VFW example (see ShevinE's improvement below).   (Note:  to get Toni's complete example to compile in D3-D5, make sure you can compile with USES OLE2:

Tools | Environment Options | Library | Library Path
D3: C:\Program Files\Borland\Delphi 3\Lib\Delphi2
D4-5: $(DELPHI)\Lib\Delphi2)

Use Lizard.AVI or another AVI file with Toni's example.

ShevinE's improvements to Toni Martir's example (D3-D5 source).   Saves AVI to  BMP files as FrameXX.BMP.  Includes VFW.PAS file used in Toni's example.  (Requires USES OLE2 -- see above notes for details.)  [Use Lizard.AVI or another AVI file.]

efg's AVItoBMP example project (Delphi 3-5 source code). 
(Assumes "Jesus Lizard" AVI is present.)

The "Jesus Lizard" AVI file shows a lizard walking on water.   
Download from here  

VB Info:  AVI to BMPs
www.shrinkwrapvb.com/avihelp/avihlp_3.htm 

Bitmap to DIB Handle Takuo Nakamura's UseNet Post with BitmapToDIBHandle routine
BitmapHandle to/from TBitmap efg's example of converting from Lead's BitmapHandle with a BMP to several formats including CMP, EPS, JPG, PCT, PCX, PNG, RAS, TGA, TIF, WMF, or from TBitmap to BitmapHandle
BitmapToWMF Procedure in ImageLib Suite DLL.

Harm's UseNet Post about converting bitmap to EMF/WMF

BMPs to AVI Tilo Arnold's UseNet Post

See Rob Anderson's AVIBUILD.
Current version does not handle pf24bit BMPs correctly.
Includes vfw.pas (Video for Windows) interface unit for avifil32.dll.

VB Info:  BMPs to AVI
www.shrinkwrapvb.com/avihelp/avihlp_5.htm 

BMP to ICO Tomes Graphical, pp. 359-361
Ignacio Alvarez's UseNet Post with BitmapToIcon function

How do I create an icon from a bitmap?  (FAQ 2748D)
BMP to GIF TGIFImage from www.melander.dk/delphi/gifimage
BMP to JPG
Quick Conversion between Bitmaps and JPEG
www.undu.com/Articles/010119c.html 
efg's TBitmap in TImage to JPG file
    
PROCEDURE ConvertBMPtoJPG (CONST BMPName: STRING; JPGName: STRING);
  VAR
    Bitmap: TBitmap;
    JPEG : TJPEGImage;
BEGIN
  Bitmap := TBitmap.Create;
  JPEG := TJPEGImage.Create;
  TRY
    Bitmap.LoadFromFile(BMPName);
    JPEG.CompressionQuality := 80;
    JPEG.Assign(Bitmap);
    JPEG.SaveToFile(JPGName);
  FINALLY
    Bitmap.Free;
    JPEG.Free
  END
END {ConvertBMPtoJPG};

Converting a BMP to a JPEG and Vice Versa (TI4528D)

efg's UseNet Post to convert TJPEGImage to TBitmap, change size of TBitmap, and convert back to TJPEGImage

efg's BMPJPG Lab Report. Interactively convert BMPs to JPGs or vice versa. Nearly all the JPEG compession options cn be controlled via the user interface, including image quality and progressive encoding and display.

efg's Command-line utility for batch conversion of BMPs to JPGs: BMPtoJPG.PAS

BMP to TIFF Save BMP as TIFF 6.0  (D1-D5, updated Sept 2000)
http://delphi.icm.edu.pl/ftp/d10free/bmp2tiff.zip 
http://community.borland.com/homepages/dsp/ftp/d10free/bmp2tiff.zip 
BMP to TXT Example of how to convert BMP file to a TXT file of RGB values, and how to convert such a TXT file to RGB values in a TBitmap for display.
BMP to WMF Non-Delphi:  AlgoLab Photo Vector turns raster images (.bmp, .jpg, and .png files) into vector images (.wmf files) with a minimum of fuss. www.zdnet.com/downloads/stories/info/0,,001EP8,.html 
Cursor to Bitmap Thomas' UseNet Post about how to extract the cursor image from a cursor file for drawing on a bitmap
DIBtoBitmap Procedure in ImageLib Suite DLL.  Also see hDIB to TBitmap.
hDIB to TBitmap efg's Lead Tools-to-Delphi conversions show two ways to start with a hDIB and end up with a TBitmap.  
- Method 1. hDIB to TBitmap resulting in bmDDB using StretchDIBits
- Method 2. hDIB to TBitmap resulting in bmDIB using MemoryStream 
ICO to TBitmap Howard Moon's UseNet Post with ConvertIconToBitmap procedure
JPG to BMP Converting a BMP to JPEG and Vice Versa (TI 4582D)
efg's BMPJPG Lab Report (see description above under BMP to JPG)
efg's
command-line utility for batch conversion of JPGs to BMPs: JPGtoBMP.PAS

efg's UseNet Post to convert TJPEGImage to TBitmap, change size of TBitmap, and convert back to TJPEGImage

PGM to BMP or JPG See efg's Interactive PGMP5 "viewer" to display PGM files (P5 format) or the command-line utility ConvertFaces to convert PGM files of the AT&T Laboratories Cambridge Database of Faces (400 images) at www.uk.research.att.com/facedatabase.html to BMP or JPG: PortableGrayMap.ZIP
RichEdit to TBitmap Peter Haas' UseNet Post
TControl to TBitmap efg's example of TControl to TBitmap including a TLabel, TPanel and TBitBtn.  
TIFF to BMP TIFF to BMP
http://delphi.icm.edu.pl/ftp/d20free/tiff2bmp.zip
http://community.borland.com/homepages/dsp/ftp/d20free/tiff2bmp.zip 
TXT to BMP Example of how to convert BMP file to a TXT file of RGB values, and how to convert such a TXT file to RGB values in a TBitmap for display.

     How to create a Bitmap from numeric data? (D5)  Shows gray scale vs. spectrum.

WMF to BMP Harm's UseNet Post with example

Links Verified 7 May 2001
Updated 26 Feb 2005


since 7 May 2001