Jos Boelens <jos.boel...@chello.nl> wrote in message 3c0932ba_1@dnews...
> Hello,
> I'd like to print the content of a form. Does anyone has example code for
> that? It seems that a way to convert DBB (Device Dependent Bitmap) to DIB
> (quess what..) would be enough for me.
> Currently I have found an example to save the form as an bmp file and an
> example to print a bmp file. That works, but I rather would would not save
> and load to a bmp file and print directly. The problem is that the TBitmap
> is a DBB and the code for printing the bitmap requires a DIB. Perhaps it
is
> simple to convert from DBB to DIB, but I couldn't find an example and my
> experiments didn't work.
> Below the part of the code that requires a DIB BitMap and after that the
> code for capturing the form to the bitmap.
> thanks for any help,
> Jos
> PROCEDURE PrintBitmap(Canvas: TCanvas; DestRect: TRect; Bitmap:
> TBitmap);
> VAR
> BitmapHeader: pBitmapInfo;
> BitmapImage : POINTER;
> HeaderSize : DWORD; // Use DWORD for D3-D5 compatibility
> ImageSize : DWORD;
> BEGIN
> GetDIBSizes(Bitmap.Handle, HeaderSize, ImageSize);
> GetMem(BitmapHeader, HeaderSize);
> GetMem(BitmapImage, ImageSize);
> TRY
> GetDIB(Bitmap.Handle, Bitmap.Palette, BitmapHeader^, BitmapImage^);
> StretchDIBits(Canvas.Handle,
> DestRect.Left, DestRect.Top, // Destination Origin
> DestRect.Right - DestRect.Left, // Destination Width
> DestRect.Bottom - DestRect.Top, // Destination Height
> 0, 0, // Source Origin
> Bitmap.Width, Bitmap.Height, // Source Width & Height
> BitmapImage,
> TBitmapInfo(BitmapHeader^),
> DIB_RGB_COLORS,
> SRCCOPY)
> FINALLY
> FreeMem(BitmapHeader);
> FreeMem(BitmapImage)
> END
> END {PrintBitmap};
> function TfrmMain.SaveAsBitmap(): TBitmap;
> // like proc SaveAsBMP except result TBitmap iso bmp file
> var
> Source: TComponent;
> SysPal : LogPal;
> tempCanvas: TCanvas;
> sourceRect, destRect: TRect;
> image2save: TImage;
> notUsed: HWND;
> bity : TBitmap;
> begin
> tempCanvas := TCanvas.Create;
> try
> tempCanvas.Handle := GetDeviceContext(notUsed);
> image2save:=TImage.create(self);
> try
> with image2save do
> begin
> Height := Self.Height;
> Width := Self.Width;
> destRect := Rect(0,0,Width,Height);
> sourceRect := destRect;
> Canvas.CopyRect(destRect,tempCanvas,sourceRect);
> SysPal.lPal.palVersion:=$300;
> SysPal.lPal.palNumEntries:=256;
> GetSystemPalet{*word*249}tries(tempCanvas.Handle,0,256,SysPal.lpal.palPalEntry);
> Picture.Bitmap.Palette:= CreatePalette(Syspal.lpal);
> end;
> file://image2save.Picture.SaveToFile(fileName);
> bity := TBitmap.Create();
> bity := image2save.Picture.Bitmap;
> Result := bity;
> finally
> image2save.Free;
> end;
> finally
> tempCanvas.Free;
> end;
> end;