Solved

Realizing Palettes

Posted on 1997-09-17
3
889 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now