How to avoid palette problems with a TImage / TBitmap (Views: 100)


I have written an D 4.0 application that opens a jpeg, paints it to a TImage canvas, alters the TImage, and then saves the TImage back to a new jpeg file. The application works great on the development machine, however when installing it on another machine (using IS 2) it generates incorrect pictures. The jpeg are displayed correctly. When I draw to the canvas the picture is also correct. However when the image is saved to a new file the original portion of the image looks like garbage, however the portion that was added is correct. I can't find any dependencies listed that need to get distributed. Did I miss something?


First of all, the canvas of a TImage is not meant to be written on by anyone else but the image the TImage contains. Also, this sounds like a palette problem. If so, your development machine probably doesn't use palettes (16,24 or 32 bit color depth) and your test machine uses palettes (8 bit color depth). Try something like this instead (not tested):

procedure DrawBitmapOnJPEG(JPEG: TJPEGImage; BMP: TBitmap);
  Bitmap: TBitmap;
  Bitmap := TBitmap.Create;
    { Convert JPEG to bitmap (DIB hopefully) }
    { Avoid palette problems }
    Bitmap.PixelFormat := pf24bit;
    { Draw BMP on JPEG }
    Bitmap.Canvas.Draw(0, 0, BMP);
    { Convert bitmap back to JPEG }

<< Back to main page