Mega Code Archive

 
Categories / Delphi / Graphic
 

Convert BMP and JPEG images to a bitmap

Title: Convert BMP and JPEG images to a bitmap Question: With Delphi it's easy to create a window and to load images. But if you want use images for e.g. graphical effects you need a bitmap to work with. But there is a problem. JPGs do not have a Bitmap property. Answer: { Insert images (BMPs or JPGs) to your window (tForm): - click on image icon of the component Additional - and insert it into your window - double click it to load a BMP or JPG image file - set the property 'visible' to false to hide it - switch to the source code implementation - call CopyImageToBitmap to create the needed bitmap } uses SysUtils, Windows, Graphics, ExtCtrls, JPEG; : : //********************************************************* // copy BMP or JPG image to given bitmap //--------------------------------------------------------- procedure CopyImageToBitmap (im: tImage; bm: tBitmap); begin if bm = nil then begin bm := tBitmap.Create; bm.PixelFormat := pfDevice; end; bm.Width := im.Picture.Width; bm.Height := im.Picture.Height; if im.Picture.Graphic is tJPEGImage then bm.Canvas.Draw (0,0, im.Picture.Graphic) // it's a JPG else bm.Canvas.Draw (0,0, im.Picture.Bitmap); // it's a BMP end; // example: var texImage: tImage; tileBm: tBitmap; : : // image1 is the name of the inserted image texImage := image1; tileBm := tBitmap.Create; tileBm.PixelFormat := pfDevice; CopyImageToBitmap (texImage, tileBm); : :