I have discussed this briefly in an earlier thread, and got no solutions to
it. Now I'm "officially" posting this question and hoping to get an
"official" answer on it.
When I send a bitmap to the Printer object's canvas, the printer will print
nothing. In my earlier post, I mentioned that if I print something on the
Printer's canvas after drawing the bitmap, I will get the Printer to print
the bitmap. I was wrong. The printer will print the line of string (I was
using a string to force the printer to print the bitmap) but no bitmap
printed.
This is what I did:
Graphics::TBitmap *bmp = new Graphics::TBitmap;
TRect rect;
.
.//load the bitmap with some picture,
.//draw some text on it, etc...
.//Set the rect to the size of bmp.
.
TPrinter *printer = Printer();
printer->BeginDoc();
printer->Canvas->Draw(0, 0, bmp);
printer->Canvas->Rectangle(rect); //this time I use a rect to force it
printer->EndDoc();
.
.
.
In the end, the printer printed nothing! Not even the rectangle! So can
someone please tell me what must I do to make the printer print a bitmap??
Thanks in advance.
Regards.
Untested:
//-------------------------------------------------------------
void __fastcall TForm1::Print1Click(TObject *Sender)
{
if( PrintDialog1->Execute() )
{
Printer()->BeginDoc();
double fPrinterVert = (double) GetDeviceCaps( Printer()->Canvas->Handle, LOGPIXELSY );
double fPrinterHorz = (double) GetDeviceCaps( Printer()->Canvas->Handle, LOGPIXELSX );
double fScreenVert = (double) GetDeviceCaps( Canvas->Handle, LOGPIXELSY );
double fScreenHorz = (double) GetDeviceCaps( Canvas->Handle, LOGPIXELSX );
int iHeight = (int) ((double)pBitmap->Height * (fPrinterVert / fScreenVert));
int iWidth = (int) ((double)pBitmap->Width * (fPrinterHorz / fScreenHorz));
StretchBltBitmap( Printer()->Canvas, 0, 0, iWidth, iHeight, pBitmap );
Printer()->EndDoc();
}
}
//-------------------------------------------------------------
void __fastcall TForm1::StretchBltBitmap(TCanvas *pCanvas, int iX, int iY, int iWidth, int iHeight, Graphics::TBitmap *pBitmap)
{
char* Buffer = NULL;
unsigned int HeaderSize = 0, ImageSize = 0;
GetDIBSizes( pBitmap->Handle, HeaderSize, ImageSize );
try
{
Buffer = new char[ HeaderSize + ImageSize ];
}
catch( Exception& E )
{
MessageDlg( "Error allocating memory for DIB : " + E.Message,
mtError, TMsgDlgButtons() << mbOK, 0 );
return;
}
BITMAPINFO* pBmi = (BITMAPINFO*) Buffer;
unsigned char* pImage = (unsigned char*) Buffer + HeaderSize;
if( GetDIB( pBitmap->Handle, pBitmap->Palette, pBmi, pImage ) )
{
StretchDIBits( pCanvas->Handle, iX, iY, iWidth, iHeight, 0,
0, pBitmap->Width, pBitmap->Height, pImage, pBmi, DIB_RGB_COLORS, SRCCOPY );
}
else
{
MessageDlg( "Error copying the DIB.", mtError,
TMsgDlgButtons() << mbOK, 0 );
}
delete [] Buffer;
}
//-------------------------------------------------------------
~ JD
If you ned reliable printing of bitmaps from CBuilder or Delphi, you should really
take a look at our TExcellentImagePrinter product at:
http://www.code4sale.com/joehecht/index.htm
Joe
Delphi, graphics, and printing specialist available - $35/hr
http://www.code4sale.com/codeit/index.htm
Joe Hecht Associates
121 Louise Drive
Crestview, FL 32536
Regards.
"Joe C. Hecht" <joeh...@code4sale.com> wrote in message
news:417945a1$1...@newsgroups.borland.com...
Thanks JD, I've tested the code and it works! So it seems like I'll have to
work on DIB when printing on TPrinter's Canvas... I'm now working on
converting/fitting the code in my application, will keep you informed as I
discover anything new...that is, if you are interested.
Regards.
"JD" <nos...@nospam.com> wrote in message
news:4178d5b1$1...@newsgroups.borland.com...
>
> "Newsreader" <Newsr...@newsgroup.com> wrote:
>
> Untested:
>
> //-------------------------------------------------------------
> void __fastcall TForm1::Print1Click(TObject *Sender)
>.
>.
>.
Good Luck. There are some TI's and FAQ's on the subject that I wrote when I
worked at Borland, however, if you want the real story, go to my product's site an
read everyting avialable (especially the FAQ) and it may help you understand what
is going on.
Joe
--