Board index » delphi » question on printing a form or how to convert DBB to DIB

question on printing a form or how to convert DBB to DIB

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;
   //image2save.Picture.SaveToFile(fileName);
   bity := TBitmap.Create();
   bity := image2save.Picture.Bitmap;
   Result := bity;
  finally
   image2save.Free;
  end;
 finally
  tempCanvas.Free;
 end;
end;

 

Re:question on printing a form or how to convert DBB to DIB


Try
yourbitmap.pixelformat:=pf24bit;

This should force the bitmap to be a dib .

Quote
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;

Re:question on printing a form or how to convert DBB to DIB


Quote
> 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

As you may have found, Form.Print fails on many printers (and does not capture
many components crrectly.

Doing a screenshot (see the TI I wrote when I was at Borland
called "A better way to print a form". If your form (and its contents)
completly fit on the screen unobscured, this is a good method, with
the exception that using a single large StretchDIBits call may also fail on
many printers. It requires about 2000 lines of low level support
code to get StretchDiBits to work reliably across the board.
If this is not a concern, then using StretchDiBits is the best
free method available. If it is a concern, I would like to recommend
our TExcellentImagePrinter product.

You might want to try our TExcellentFormPrinter product. It can print
the entire contents of a form, even if the form is hidden or obscured, and
can print all the contents (even those that are scrolled from view).
http://www.code4sale.com/joehecht

Joe
--
Jimmy Page is having a charity artwork sale: Action For Brazil's Children Trust: http://www.zeppinhood.net/
Whole Lotta Love computing to cure cancer! http://www.wholelottalove.org/

Re:question on printing a form or how to convert DBB to DIB


Quote
"Francesco Savastano" <francescos...@tiscalinet.it> wrote in message

news:3c09528d_2@dnews...

Quote
> Try
> yourbitmap.pixelformat:=pf24bit;

> This should force the bitmap to be a dib .

I would think so too, but it gives a runtime error.

Quote

> 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.

Re:question on printing a form or how to convert DBB to DIB


Hi,

Try to change the following changes

Quote
>   bity := TBitmap.Create();

     bity.width := Image2Save.Width;
     bity.Height := image2save.height;
     bity.Handletype := bmDIB;
     bity.PixelFormat := pf24bit;
     stretchblt(bity.handle,0,0,bity.width,bity.height,
                      Image2.Picture.Bitmap.handle,0,0,bity.width,
                  bity.handle,srcCopy);

Quote
>   Result := bity;
>  finally
>   image2save.Free;
>  end;
> finally
>  tempCanvas.Free;
> end;
>end;

Hope it helps.

regards
Johnnie.

Other Threads