Board index » cppbuilder » GDI+ for printing

GDI+ for printing


2005-10-25 07:58:55 AM
cppbuilder17
Hi All,
After some experiemnts with GDI+, I am quite impressed with the new
capability of GDI+. However, when I try to send a plot to a printer using
GDI+ DrawImage function, it fails to produce an image on the paper. Any one
have any success with GDI+ DrawImage for printing. A sample code will be
appreciated.
Thanks,
Binzhong
 
 

Re:GDI+ for printing

Binzhong Zhou wrote:
Quote
After some experiemnts with GDI+, I am quite impressed with the new
capability of GDI+. However, when I try to send a plot to a printer using
GDI+ DrawImage function, it fails to produce an image on the paper. Any one
have any success with GDI+ DrawImage for printing. A sample code will be
appreciated.
Here is a working sample. Just replace the printer name with yours;
HDC PrinterDC = CreateDC(NULL, "HP Deskjet 6500 Series", NULL, NULL);
DOCINFO docInfo = {0};
docInfo.cbSize = sizeof(DOCINFO);
docInfo.lpszDocName = "PrintTest";
StartDoc(PrinterDC, &docInfo);
StartPage(PrinterDC);
Graphics* graphics = new Graphics(PrinterDC);
Gdiplus::Image image(L"your_image.jpg");
graphics->DrawImage(&image, 0, 0, image.GetWidth(), image.GetHeight());
delete graphics;
EndPage(PrinterDC);
EndDoc(PrinterDC);
DeleteDC(PrinterDC);
HTH
Michel
--
----------------------------------------
Michel Leunen
mailto: see my homepage.
C++Builder, BCC5.5.1 Web site:
www.leunen.com/
----------------------------------------
 

Re:GDI+ for printing

Thanks, Michel, for the sample. The results I got were mixture: depending on
the way you use the GDI+. If I directly draw on the printer DC via GDI+
function, the image will be automatically scaled to the same size as the
screen display but it doesn't do the high quality interpolation although I
asked for with the statement
graphics.SetInterpolationMode(Gdiplus::InterpolationModeHighQualityBicubic);
The following statement
graphics.DrawImage(image, Gdiplus::Rect(0, 0, W, H),
0, 0, image->GetWidth(), image->GetHeight(), Gdiplus::UnitPixel);
does not affect result even I specified the W, H to the paper size. It only
prints to the size of the screen display. How can I turn this default
feature off?
If I darw the image through off-screen DC (memry DC) using GDI+, I got a
very tiny porttion of the image. Again, the W, H parameters do not help.
My ShowImage function is shown below. To test, you can add this function
into your printing section:
.....
StartPage(PrinterDC);
ShowImage(PrinterDC);
EndPage(PrinterDC);
.....
Cheers,
Binzhong
//---------------------------------------------------------------------------
void TTestWnd::ShowImage(HDC dc)
{
int W = ClientWidth;
int H = ClientHeight;
if (Printer()->Printing)
{
W = Printer()->PageWidth/10;
H = Printer()->PageHeight/10;
}
Gdiplus::Bitmap *image = new Gdiplus::Bitmap(L"Test.bmp");
// Rendering the image in memory DC
// Create a memory dc & set its dimension
HDC memdc = ::CreateCompatibleDC(dc);
GDIObject MyBitmap(memdc, ::CreateCompatibleBitmap(dc, W, H));
// set the graphic to the memdc
Gdiplus::Graphics graphics(memdc);
//Gdiplus::Graphics graphics(dc);
// fill the background using the brush
Gdiplus::SolidBrush brush(Gdiplus::Color(255, 255, 255));
graphics.FillRectangle(&brush, 0, 0, W, H);
graphics.SetInterpolationMode(Gdiplus::InterpolationModeHighQualityBicubic);
//graphics.SetInterpolationMode(Gdiplus::InterpolationModeHighQualityBilinear);
// Draw the image
graphics.DrawImage(image, Gdiplus::Rect(0, 0, W, H),
0, 0, image->GetWidth(), image->GetHeight(), Gdiplus::UnitPixel);
Gdiplus::Pen* pen = new Gdiplus::Pen(Gdiplus::Color(255, 0, 0));
graphics.DrawRectangle(pen, W/4, H/4, W-W/2, H-H/2);
graphics.DrawEllipse(pen, W/4, H/4, W-W/2, H-H/2);
delete(pen);
// Copy the memory DC drawing onto canvas dc
::BitBlt(dc, 0, 0, W, H, memdc, 0, 0, SRCCOPY);
// Release the memory
::DeleteDC(memdc);
delete image;
}
 

{smallsort}

Re:GDI+ for printing

Binzhong Zhou wrote:
Quote
Thanks, Michel, for the sample. The results I got were mixture: depending on
the way you use the GDI+. If I directly draw on the printer DC via GDI+
function, the image will be automatically scaled to the same size as the
screen display but it doesn't do the high quality interpolation although I
asked for with the statement

graphics.SetInterpolationMode(Gdiplus::InterpolationModeHighQualityBicubic);
True and it seems to be the noemal behaviour.
Quote

The following statement

graphics.DrawImage(image, Gdiplus::Rect(0, 0, W, H),
0, 0, image->GetWidth(), image->GetHeight(), Gdiplus::UnitPixel);

does not affect result even I specified the W, H to the paper size. It only
prints to the size of the screen display. How can I turn this default
feature off?
I don't know. I think it's the same problem you got when printing with
the 'old' GDI functions.
Damon Chandler recommend to create a DIB from the Bitmap with
GetDIBits() and then printing the DIB onto the PrinterDC using
StretchDIBits(). Of course, you can use GDI+ to create your bitmap.
I've not tested this with GDI+ yet.
Michel
--
----------------------------------------
Michel Leunen
mailto: see my homepage.
C++Builder, BCC5.5.1 Web site:
www.leunen.com/
----------------------------------------
 

Re:GDI+ for printing

Quote
I don't know. I think it's the same problem you got when printing with the
'old' GDI functions.
Damon Chandler recommend to create a DIB from the Bitmap with GetDIBits()
and then printing the DIB onto the PrinterDC using StretchDIBits(). Of
course, you can use GDI+ to create your bitmap.
I've not tested this with GDI+ yet.
I know how to make it work under old GDI by creating my own DIB and sending
it to the PrinterDC even with very large images. I hope the GDI+ solve the
printing issue better than the old GDI. But It sounds like the problem is
still remained. It's possible it's just I have not properly understood the
GDI+ as I just start to learn it.
 

Re:GDI+ for printing

"Binzhong Zhou" < XXXX@XXXXX.COM >wrote in message news: XXXX@XXXXX.COM ...
Quote
I know how to make it work under old GDI by creating my own DIB and sending it to the PrinterDC even with very large images. I
hope the GDI+ solve the printing issue better than the old GDI. But It sounds like the problem is still remained. It's possible
it's just I have not properly understood the GDI+ as I just start to learn it.
It is a printing issue. DIBs are the way to go. Our TExcellentImagePrinter can help:
www.code4sale.com/joehecht/index.htm
Joe
--
Delphi, graphics, and printing specialist available - $35/hr
www.code4sale.com/codeit/index.htm
Joe Hecht Associates
121 Louise Drive
Crestview, FL 32536