Solved

Realizing Palettes

Posted on 1997-09-17
3
893 Views
Last Modified: 2010-08-05
I load a bitmap from my users hard drive. I find that my user only has 256 colours available. So I need to realize a palette. So why doesn't:

//*******
PalPrev := SelectPalette(Canvas.Handle, MyBitmap.Palette, False);
r := RealizePalette(Canvas.Handle);
//show my picture here (with flashy effect!)
ShowPic(MyBitmap);
//afterwards, clean up:
SelectPalette(Canvas.Handle, PalPrev, True);
//********

work? (note, Canvas refers to the main form's canvas)

I tried changing the painting to a TPaintBox, but I got the same answer. Please also note that (for the form) I am intercepting the WM_ERASEBACKGROUND message, in order for my special effects to be flicker free. Surely that can't be the problem? Also, I know that MyBitmap has a palette.

I'll give anyone who can solve my problem a B, with an A reserved for those who give me a good explanation of why my above code doesn't work.
0
Comment
Question by:wolfcrag
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
mirek071497 earned 60 total points
ID: 1345308
1.The easiest way to draw you'r bitmap is in exchange of you'r more than 4 lines use only one :

Canvas.Draw( 0, 0, MyBitmap );

but mayby you can't use this (i don't know why).

2.You'r code can't work because of you'r call to Canvas.Handle is not good (delphi caching algorithm). You must use this as in this example :

var
  PalPrev : HPalette;
  NewDC : HDC;
  FormDC : HDC;
begin
  FormDC := GetDC( Handle );
  PalPrev := SelectPalette( FormDC, MyBitmap.Palette, False);
  RealizePalette( FormDC );
{ painting on FormDC }
  NewDC := CreateCompatibleDC( Canvas.Handle );
  SelectObject( NewDC, MyBitmap.Handle );
  BitBlt( FormDC, 0, 0, 100, 100, NewDC, 0, 0, SrcCopy );
{ end of Painting }
  DeleteDC( NewDC );
  SelectPalette( FormDC, PalPrev, True);
  ReleaseDC( Handle, FormDC );
end;

0
 
LVL 3

Author Comment

by:wolfcrag
ID: 1345309
Can you elaborate further on this delphi caching algorithm, and/or why calls to Canvas.Handle are not good? Your code worked beautifully. Thanks!
0
 
LVL 3

Expert Comment

by:mirek071497
ID: 1345310
My english is rather poor so i think that have no sense but the problem is in Delphi3 (if i good remember) and with Delphi2 all works fine. In Delphi3 Borland made some changes to palette and i don't work about it hard and i don't know how say this good. There are added methods for manipulating palette but i think so with some mistakes because sometimes i can't get acces to the palette.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question