Board index » cppbuilder » Image->Canvas don't print.

Image->Canvas don't print.

I work with BCB 1.0.
In my form there is a TPrintDialog and a TImage.
When I try to print using Image1->Canvas my prog don't work, but when
I print with the form canvas it work. Why?

This is my simple prog:

//---------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop

#include <printers.hpp>
#include "Main.h"
static TCanvas *cnv;
//---------------------------------------------------------------
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner): TForm(Owner)
{
  cnv=Image1->Canvas;

  // Deleting next line don't print!
  //---------------------------------
  Image1->Hide(); cnv=Canvas;

Quote
}

//---------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender)
{
  cnv->Rectangle(10,30,290,150);
Quote
}

//---------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  if(PrintDialog1->Execute())
  {
    Printer()->BeginDoc();
    StretchBlt(
      Printer()->Handle,// handle of destination device context
      100, // x-coordinate of upper-left corner of dest. rect.
      100, // y-coordinate of upper-left corner of dest. rect.
      900, // width of destination rectangle
      900, // height of destination rectangle
      cnv->Handle, // handle of source device context
      0,  // x-coordinate of upper-left corner of source rectangle
      0,  // y-coordinate of upper-left corner of source rectangle
      Image1->Width, // width of source rectangle
      Image1->Height,// height of source rectangle
      SRCCOPY   // raster operation code
    );
    Printer()->EndDoc();
  }
Quote
}

file://----------------------------------------------------------------
 

Re:Image->Canvas don't print.


Hi Cristiano,

Quote
> In my form there is a TPrintDialog and a TImage.
> When I try to print using Image1->Canvas my prog don't work, but when
> I print with the form canvas it work. Why?

The StretchBlt() API function can't be reliably used to render an image
to the device context different device such as that of the printer.
Instead, convert your Image to a Device-Independant Bitmap (DIB), then
use the StretchDIBits() API function.  Have a look at the following page
for more info...

http://bcbcaq.freeservers.com/print_graphics.html

Good luck!

Damon

Other Threads