Solved

24 bit BMP on 256 color display

Posted on 1997-09-18
6
243 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
  • 3
  • 3
6 Comments
 
LVL 5

Expert Comment

by:JimBob091197
Comment Utility
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
Comment Utility
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
Comment Utility
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
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

 

Author Comment

by:chrishart
Comment Utility
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
Comment Utility
I hope it works ok for you...
0
 

Author Comment

by:chrishart
Comment Utility
Sorry about the delay and thanks again

Chris
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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 my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
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.

762 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