GetDiBits() How ?

How do I use GetDiBits() to load a bitmap into a
TImage.Picture.Bitmap so that I can still use the
Canvas, and Canvas tools etc.

I was advised to use GetDiBits() because I need
True Color, and Timage.Picture.Bitmap is usally
only in 256 colors.

John.
LVL 1
jturpinAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
mirek071497Connect With a Mentor Commented:
Hi jturpin !

I write simple example of loading 24bbp bitmaps and i found so borland made very big mistake. When bitmap is 24bbp then there are no Palette information. You can use 24bbp bitmaps if you add one line to Graphics.pas

function PaletteFromW3DIB(const BI: TBitmapInfo): HPALETTE;
.....
if Colors <= 2 then Exit;
if Bi.bmiHeader.biBitCount=24 then exit;
....

when you change this and recompile library then all work's fine
you can load bitmap wit 24bbp.

Of course you must have partial source which came with delphi. If You hav'nt this source you can do this in another way :

procedure TForm1.Button1Click(Sender: TObject);
var NewDC : HDC;
  Bmp : HBitmap;
begin
  MyBmp := TBitmap.Create;
  MyBmp.LoadFromFile ('D:\MYPIC.BMP');
  NewDC := CreateCompatibleDC( PaintBox1.Canvas.Handle );
  Bmp := CreateCompatibleBitmap( PaintBox1.Canvas.Handle, MyBmp.Width, MyBmp.Height );
 SelectObject( NewDC, Bmp );
{ here you must do trick !!!!! }
{  SetDIBits from loaded Bitmap }
  BitBlt( PaintBox1.Canvas.Handle, 0, 0, 200, 200, NewDC, 0, 0, SrcCopy );
  DeleteObject( Bmp.Handle );
  DeleteDC( NewDC );
  MyBmp.Free;
end;

I write code for SetDIBits approx after 2 hour.
0
 
mirek071497Commented:
When You use  GetDIBits(....BitInfo: BitmapInfo...) then at first you must parse BitmapInfo structure to check how You'r bitmap is retrived. (of course i think so all structures was allocated).
Full example is rather big because there is 5 possible cases but for 24bbp bitmap it is very easy and full described if Help files.

When You use SetDIBits then you must paint Bitmap (for example with BitBlt).

Tell me more what you need then i write simple example.
0
 
jturpinAuthor Commented:
I have an application which loads a bitmap into
TImage.TPicture.TBitmap. I then allow the user to do some
processing of this picture using some tools I provide,
PenTool, RectangleTool etc. These changes are recorded
on TImage.Canvas. The user can also select and zoom
parts of the picture, and then edit the zoomed section.
I am using TCanvas.StretchDraw to zoom.

I now have a bigger 24 bit true color picture that I want
to process. However when I load it the colors are off - the
sky is green and the picture is much darker.

This distortion is either the result of Delphi's TBitmap
not being to handle the larger picture, or it is the
result of my video card not being able to handle the picture.
I have heard arguments on both sides.

If it is TCanvas, then I have been told to use GetDiBits.
Therefore I need to know how to load the bitmap using
GetDiBits so I can still use my tools.

If it is the video card I need a new card.

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
mirek071497Commented:
OK but i have not time today. I write for you tommorow, but you probably have graphics.pas where are GetDIBits is used. Try watch at borland source (for example TBitmap.ReadStream) or wait to tomorrow.
0
 
mirek071497Commented:
Hi Jturpin

Sorry but if you can't load 24bpp bitmap in design time without mesage Out Of Memory you must made changes in Graphics.pas or increase points becaus in this problem you need read bitmap in you'r own procedures. All call to Bitmap.handle will generate Out Of Memory and you can't use this.

1. Made changes in Graphics.Pas and then try this :
procedure TForm1.Button2Click(Sender: TObject);
var NewDC : HDC;
begin
  MyBmp := TBitmap.Create;
  MyBmp.LoadFromFile ('D:\MYPIC.BMP');
  NewDC := CreateCompatibleDC( PaintBox1.Canvas.Handle );
  SelectObject( NewDC, MyBmp.Handle );
  BitBlt( PaintBox1.Canvas.Handle, 0, 0, MyBmp.Width, MyBmp.Height, NewDC, 0, 0, SrcCopy );
  DeleteObject( MyBmp.Handle );
  DeleteDC( NewDC );
  MyBmp.Free;
end;

2.Increase you'r points and i can write procedure to display 24bbp bitmap but point1 is better because you can use Bitmap.handle.
0
 
mirek071497Commented:
oh ! i just read you'r comment. You must set display for more than 256 colors !
WHen you can load bitmaps and work on them then you have probably better version of delphi (i have old delphi2). If You'r display was cofigured for 256 color's you can't see 24bbp in good colors (of course you can do own procedure to change palette). Try change settings of display to HiColor or True color.
If You can't do this you need new video card.
0
 
mirek071497Commented:
Hi jturpin.
I now buy Delphi3 and i sow so ther is some differences with Delphi2 and Delphi3 with subject :"Bitmaps and Palettes".
Tell me what version of Delphi you have and what you need more.
0
 
mirek071497Commented:
Hi Jturpin !
I have now Delphi1, 2 and 3 so tell me which version you use.
I write working example for you.
0
All Courses

From novice to tech pro — start learning today.