Solved

24 bit BMP on 256 color display

Posted on 1997-09-18
6
249 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.

911 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

20 Experts available now in Live!

Get 1:1 Help Now