Recent

Author Topic: How to print a TBitmap  (Read 6610 times)

TheSlimvReal

  • Newbie
  • Posts: 1
How to print a TBitmap
« on: June 29, 2015, 07:16:11 pm »
Hey guys, I have a program, where I want to print the content of a Panel.
I already found a way to save the content of the Panel to a bitmap:
Code: [Select]
var R: TRect;
    Bitmap: TBitmap;
begin
  Bitmap := TBitmap.Create;
  try
    R := Rect(0, 0, Panel1.Width, Panel1.Height);
    Bitmap.SetSize(Panel1.Width, Panel1.Height);
    Bitmap.Canvas.CopyRect(R, Panel1.Canvas, R);
  finally
  bitmap.free;
  end;
end;
I was searching in the internet for about an hour, but I can't find something that helps me...
I just want to print, what I saved in my variable "Bitmap".

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: How to print a TBitmap
« Reply #1 on: June 29, 2015, 08:10:35 pm »
Assign the bitmap's canvas to printer's canvas.

eric

  • Sr. Member
  • ****
  • Posts: 267
Re: How to print a TBitmap
« Reply #2 on: June 29, 2015, 11:57:08 pm »
... or more simply, just use CopyRect directly from the panel to the printer's canvas.

Dr.Theopolis

  • Jr. Member
  • **
  • Posts: 69
Re: How to print a TBitmap
« Reply #3 on: June 30, 2015, 11:33:04 pm »
An option I use, though more elaborate, is to use html component such as Turbopower TIpHtmlPanel and load your bitmap into a runtime-generated html page and use that component's print capability. You can keep the component hidden if desired. I use this method as a quick-n-easy report generator too.

mica

  • Full Member
  • ***
  • Posts: 196
Re: How to print a TBitmap
« Reply #4 on: August 17, 2015, 11:59:43 pm »
Turbopower TIpHtmlPanel print preview seems gone.

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: How to print a TBitmap
« Reply #5 on: August 18, 2015, 03:04:31 am »
hello,
see also  HTMLViewer  :

Quote
Some features:
- Cascading Stylesheets
- Large HTML files
- HTML Frames
- HTML Forms
- HTML Tables
- Bitmap, GIF, JPEG, and PNG Images
- Transparent images
- Image caching
- Left and right floating images
- Image sizing attributes
- Client side image maps
- Background colors and images
- Font sizes, styles, and colors with HTML tags or default settings
- Formatted printing of the HTML document
- Can print multiple pages horizontally
- Print preview
- Text search
- Copy to clipboard
- Subscripts and superscripts
- Unicode, UTF-8 and many single and multi byte character codes supported
« Last Edit: August 18, 2015, 03:10:12 am by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

Michl

  • Full Member
  • ***
  • Posts: 226
Re: How to print a TBitmap
« Reply #6 on: August 18, 2015, 09:35:44 am »
Turbopower TIpHtmlPanel print preview seems gone.
You can activate it. For example insert in the first lines of unit IpHtml:
Code: [Select]
{$DEFINE Html_Print}   
Code: [Select]
type
  TLiveSelection = (lsMoney, lsChilds, lsTime);
  TLive = Array[0..1] of TLiveSelection;

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: How to print a TBitmap
« Reply #7 on: August 19, 2015, 02:05:54 am »
hello,
success story to print the content of a form using THtmlViewer :

1 - one invisible htmlviewer component on the form
2 - one button to capture the content of a form and write canvas on a MemoryStream.
3 - one button to show preview form.

code to capture the content of a form and load it on the htmlviewer :
Code: [Select]
procedure TFMain.CaptureClick(Sender: TObject);
var R: TRect;
var   myBitmap: TBitmap;
begin
  HtmlViewer1.LoadFromString('<body></body>');
  myBitmap := TBitmap.Create;
  try
   R := Rect(0, 0, FMain.Width, FMain.Height);
   myBitmap.SetSize(FMain.Width, FMain.Height);
   myBitmap.Canvas.CopyRect(R, FMain.Canvas, R);
   ImageStream := TMemoryStream.Create;
   myBitmap.SaveToStream(ImageStream);
   ImageStream.Position:= 0;
  Htmlviewer1.InsertImage('test',ImageStream);
  HtmlViewer1.LoadFromString('<body><img src=url(test) /></body>');
  finally
  myBitmap.free;
  end;
end;

the code of the preview button :
Code: [Select]
procedure TFMain.Button2Click(Sender: TObject);
var
  pf: TBegaHtmlPrintPreviewForm;
  Viewer: ThtmlViewer;
  Abort: boolean;
begin
  if HtmlViewer1 <> nil then
  begin
    pf := TBegaHtmlPrintPreviewForm.Create(Self);
    pf.HtmlViewer  := HtmlViewer1;
    Abort := False;
    try
      if not Abort then
        pf.ShowModal;
    finally
      pf.Free;
    end;
  end;
end;

code of imageonrequest event :
Code: [Select]
procedure TFMain.HtmlViewer1ImageRequest(Sender: TObject; const SRC: ThtString;
  var Stream: TStream);
begin
   Stream:=ImageStream;
end;

Not sure that the code is optimal. How to properly use the onimagerequest event and insertimage method ?

The result  in the attachment.

Friendly, J.P
« Last Edit: August 19, 2015, 02:22:00 am by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: How to print a TBitmap
« Reply #8 on: August 20, 2015, 12:27:42 am »
hello,
better code , no memory leeks  8-) :
 print the content of a form using  HTMLViewer :

1 - one invisible htmlviewer component on the form
2 - one button to capture the content of a form and write canvas on a MemoryStream.
3 - one button to show preview form.

uses to add :
Code: [Select]
HtmlView, HTMLUn2, HtmlGlobals,BegaHtmlPrintPreviewForm;

code to capture the content of a form  :
Code: [Select]
procedure TFMain.CaptureClick(Sender: TObject);
HtmlViewer1.Clear;
HtmlViewer1.LoadFromString('<body><img src=test /></body>');
End;

the code above generate an onImageRequest event.
Code for the  onImageRequest event :
Code: [Select]
procedure TFMain.HtmlViewer1ImageRequest(Sender: TObject; const SRC: ThtString;
var Stream: TStream);
var myBitmap: TBitmap;
var R: TRect;
begin
//  ShowMessage(SRC);
  if SRC = 'test' then
  begin
    myBitmap := TBitmap.Create;
    try
      R := Rect(0, 0, FMain.Width, FMain.Height);
      myBitmap.SetSize(FMain.Width, FMain.Height);
      myBitmap.Canvas.CopyRect(R, FMain.Canvas, R);
      FreeAndNil(ImageStream);
      ImageStream := TMemoryStream.Create;
      myBitmap.SaveToStream(ImageStream);
      ImageStream.Position := 0;
      Stream := ImageStream;
    finally
      myBitmap.Free;
    end;
  end;
end;

ImageStream is a global variable of type TMemoryStream defined in the interface of the unit.

code for the preview button :
Code: [Select]
procedure TFMain.PreviewClick(Sender: TObject);
var
  pf: TBegaHtmlPrintPreviewForm;
  Viewer: ThtmlViewer;
  Abort: boolean;
begin
  if HtmlViewer1 <> nil then
  begin
    pf := TBegaHtmlPrintPreviewForm.Create(Self);
    pf.HtmlViewer := HtmlViewer1;
    Abort := False;
    try
      if not Abort then
        pf.ShowModal;
    finally
      pf.Free;
    end;
  end;
end;

Friendly, J.P
« Last Edit: August 20, 2015, 12:43:28 am by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

 

TinyPortal © 2005-2018