Hi Everybody,
I'm just a newbie for this forum.
I've use TPrintPreview component on BCB6 to print the result of my application
by using canvas to capture the form as shown in this code:
void __fastcall TForm2::Print1Click(TObject *Sender)
{
// Printign mechanism
int PrintLeft, PrintTop, PrintWidth, PrintHeight;
TRect Srect, Smrect,*DynamicSRect, *DynamicSMRect;
Graphics::TBitmap **PrintingBitmapSet;
int BitmapSetNum = 2;
PrintingBitmapSet = new Graphics::TBitmap* [BitmapSetNum];
DynamicSRect = new TRect [BitmapSetNum];
DynamicSMRect = new TRect [BitmapSetNum];
for(int i =0; i <= BitmapSetNum-1;i++)
{
PrintingBitmapSet[i] = new Graphics::TBitmap;
PrintingBitmapSet[i]->PixelFormat = pf24bit;
}
TCanvas *C = new TCanvas;
C->Handle = GetDC(0);
PrintBMP = new Graphics::TBitmap;
PrintBMP->PixelFormat = pf24bit;
CanvasPager->ActivePage = ZoomCanvasPage;
PrintLeft = Form2->Left;
PrintTop = Form2->Top + 83;
PrintWidth = Form2->Width;
PrintHeight = Form2->Height - 93;
Srect = Rect(PrintLeft,PrintTop,PrintLeft+PrintWidth,PrintTop+PrintHeight);
Smrect = Rect(0, 0, PrintWidth, PrintHeight);
DynamicSRect[0] = Rect(PrintLeft,PrintTop,PrintLeft+PrintWidth,PrintTop+PrintHeight);
DynamicSMRect[0] = Rect(0, 0, PrintWidth, PrintHeight);
PrintBMP->PixelFormat = pf24bit;
PrintBMP->Width = PrintWidth;
PrintBMP->Height = PrintHeight;
PrintBMP->Canvas->CopyRect(Smrect, C, Srect);
PrintBMP->Canvas->TextOutA(PrintBMP->Width - 250, 4, "Written By: Dr.Pooh, NECTEC @2008-2012");
PrintingBitmapSet[0]->Assign(PrintBMP);
delete PrintBMP; PrintBMP = NULL;
delete C; C = NULL;
C = new TCanvas;
C->Handle = GetDC(0);
PrintBMP = new Graphics::TBitmap;
PrintBMP->PixelFormat = pf24bit;
CanvasPager->ActivePage = PhotoCanvasPage;
PrintLeft = Form2->Left;
PrintTop = Form2->Top + 83;
PrintWidth = Form2->Width;
PrintHeight = Form2->Height - 93;
Srect = Rect(PrintLeft,PrintTop,PrintLeft+PrintWidth,PrintTop+PrintHeight);
Smrect = Rect(0, 0, PrintWidth, PrintHeight);
DynamicSRect[1] = Rect(PrintLeft,PrintTop,PrintLeft+PrintWidth,PrintTop+PrintHeight);
DynamicSMRect[1] = Rect(0, 0, PrintWidth, PrintHeight);
PrintBMP->PixelFormat = pf24bit;
PrintBMP->Width = PrintWidth;
PrintBMP->Height = PrintHeight;
PrintBMP->Canvas->CopyRect(Smrect, C, Srect);
PrintBMP->Canvas->TextOutA(PrintBMP->Width - 250, 4, "Written By: Dr.Pooh, NECTEC @2008-2012");
PrintingBitmapSet[1]->Assign(PrintBMP);
delete PrintBMP; PrintBMP = NULL;
delete C; C = NULL;
int PrintingCode = 1;
Form11->ShowForm11(PrintingBitmapSet,BitmapSetNum,PrintingCode);
}
//---------------------------------------------------------------------------
So far, I learnt from your example on how to print Bitmap Data in single page and it works very well. However, I still have a very hard time on how to print Bitmap Data stored in Dynamic Array as multiple page since it gives me Access Violation Problem at PaintGraphicEx function of TPrintPreview component.
The source code for Print Preview can be shown as follows:
void __fastcall TForm11::ShowForm11(Graphics::TBitmap **Resultbitmap,int NumPage,int PrintingCodeX)
{
// Need to be change to allow Multiple page
NumPage1 = NumPage;
PrintingCode = PrintingCodeX;
DynamicBitmap = new Graphics::TBitmap* [NumPage1];
for(i= 0; i <= NumPage1-1; i++)
{
if(Resultbitmap != NULL)
{
DynamicBitmap[i] = new Graphics::TBitmap;
DynamicBitmap[i]->PixelFormat = pf24bit;
DynamicBitmap[i]->Assign(Resultbitmap[i]);
}
}
CreatePages(DynamicBitmap,R,PortraitOrientation,ProportionalScaling,PrintingCode,NumPage1);
InvalidateRect(PrintPreview1->Handle,&R,true);
Show();
}
//------------------------------------------------------------------------------------------------------------------------
void __fastcall TForm11::CreatePages(Graphics::TBitmap **BitmapX,TRect Rect1,
bool PortraitOrientation1, bool ProportionalScaling1,int PrintingCodeX, int NumPageX)
{
// This is hte mechanic thich has to be changed to allow Multiple page
// create single page by CreateImageOnlyPage function
if(NumPageX == 1 && BitmapX != NULL)
{
bitmap = new Graphics::TBitmap;
bitmap->PixelFormat = pf24bit;
bitmap->Assign(BitmapX[0]);
PrintPreview1->BeginDoc();
CreateImageOnlyPage(bitmap,Rect1,ProportionalScaling1);
// This section work very well
Application->ProcessMessages();
PrintPreview1->EndDoc();
} else if(NumPageX >=2 && BitmapX != NULL)
{
PrintPreview1->BeginDoc();
for(i = 0; i <=NumPageX-1;i++);
{
PrintPreview1->NewPage(); // Next Page
if(BitmapX[i] != NULL)
{
CreateImageOnlyPage(BitmapX[i],Rect1,ProportionalScaling1);
}
Application->ProcessMessages();
}
PrintPreview1->EndDoc();
}
// CreateImageOnlyPage(BitmapX,Rect1,ProportionalScaling1);
}
//---------------------------------------------------------------------------------------------------------------------------------
void __fastcall TForm11::CreateImageOnlyPage(Graphics::TBitmap *BitmapX1,TRect Rect2, bool ProportionalScaling2)
{
if(ProportionalScaling2 == true)
{
Proportional = true;
}
else if(ProportionalScaling2 == false)
{
Proportional = false;
}
if(BitmapX1 != NULL)
{
PrintPreview1->PaintGraphicEx(Rect2,BitmapX1,Proportional,false,true); // -> Access violation Problem arise here!
}
}
//---------------------------------------------------------------------------------
The program stop at the following line in Preview.pas
function TPrintPreview.PaintGraphicEx(const Rect: TRect; Graphic: TGraphic;
Proportinal, ShrinkOnly, Center: Boolean): TRect;
var
gW, gH: Integer;
rW, rH: Integer;
W, H: Integer;
begin
gW := ConvertUnits(Graphic.Width, Screen.PixelsPerInch, mmPixel, FUnits);
% Program stop here!
//--------------------------------------------------------------------
If you know how to print the bitmap data in multiple pages, please help me out of this trouble now!
It would be a lot better if you can show the source code (Delphi is okay but BCB is preferred)