Solved

24 bit BMP on 256 color display

Posted on 1997-09-18
6
277 Views
Last Modified: 2010-04-03
When I use the standard image component to stick a 24 bit BMP file on a form the picture looks awful (like its only 16 colors). Is there a trick to make it look nicer without going to the hassle of converting it to a 256 color BMP first?

ta chris
0
Comment
Question by:chrishart
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1345515
I once got around the problem like this:  (You convert the bitmap to 256 in Delphi, not the physical bitmap on disk.  The original bitmap on disk in my case was a 24 bit bitmap too.)

procedure TForm1.LoadBitmap;
var
  Bmp: TBitmap;
begin
  Bmp := TBitmap.Create;
  Bmp.LoadFromFile('C:\Temp\Test.bmp');
  Bmp.PixelFormat := pf8bit;
  Image1.Picture.Assign(Bmp);
  Bmp.Free;
end;

0
 

Author Comment

by:chrishart
ID: 1345516
Thanks for the answer, it looked like it should do the trick.
I tried your code out but Sadly C++Builder does not seem to support
PixelFormat so it didn't work :-(
I'm sorry I didn't mention that I was using Builder, things are usually
the same.
It seems C++Builder's TBitmap is perhaps based on an old version of Delphi?

Thanks Chris
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1345517
Delphi's code for SetPixelFormat is below.  You should be able to convert it easily because it mostly deals with palettes and Wnidows API calls.

TPixelFormat = (pfDevice, pf1bit, pf4bit, pf8bit, pf15bit, pf16bit, pf24bit, pf32bit, pfCustom);

procedure TBitmap.SetPixelFormat(Value: TPixelFormat);
const
  BitCounts: array [pf1Bit..pf32Bit] of Byte = (1,4,8,16,16,24,32);
var
  DIB: TDIBSection;
  Pal: HPalette;
  DC: HDC;
begin
  if Value = GetPixelFormat then Exit;
  case Value of
    pfDevice:
      begin
        HandleType := bmDDB;
        Exit;
      end;
    pfCustom: InvalidGraphic(SInvalidPixelFormat);
  else
    FillChar(DIB, sizeof(DIB), 0);
    DIB.dsbm := FImage.FDIB.dsbm;
    with DIB, dsbm, dsbmih do
    begin
      bmBits := nil;
      biSize := sizeof(DIB.dsbmih);
      biWidth := bmWidth;
      biHeight := bmHeight;
      biPlanes := 1;
      biBitCount := BitCounts[Value];
      Pal := FImage.FPalette;
      case Value of
        pf4Bit: Pal := SystemPalette16;
        pf8Bit:
          begin
            DC := GDICheck(GetDC(0));
            Pal := CreateHalftonePalette(DC);
            ReleaseDC(0, DC);
          end;
        pf16Bit:
          begin
            biCompression := BI_BITFIELDS;
            dsBitFields[0] := $F800;
            dsBitFields[1] := $07E0;
            dsBitFields[2] := $001F;
          end;
      end;
      CopyImage(Handle, Pal, DIB);
      PaletteModified := Pal <> 0;
      Changed(Self);
    end;
  end;
end;

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:chrishart
ID: 1345518
Thanks JimBob for your comment, that looks good to me, can you make it an answer so I can award you your points and close the topic!

Thanks very much Chris
0
 
LVL 5

Accepted Solution

by:
JimBob091197 earned 100 total points
ID: 1345519
I hope it works ok for you...
0
 

Author Comment

by:chrishart
ID: 1345520
Sorry about the delay and thanks again

Chris
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month7 days, 18 hours left to enroll

617 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