Print Image on Desktop Printer Using GDI+

Universal Document Converter works like a virtual printer that saves any document you print as a raster PDF file or as an image file. You can use a post-print feature of Universal Document Converter to apply additional processing to every output file. Below is an example of one of several post-printing solutions.

// You can automatically send copy of each image file produced by
// Universal Document Converter from your application using GDI+.
//
// You must initialize GDI+ in your InitInstance() function using
// "GdiplusStartup(...)" and release it in your ExitInstance()
// function using "GdiplusShutdown(...)".

#include "Winspool.h"
#include <atlconv.h>
#include <gdiplus.h>

using namespace Gdiplus;
#pragma comment (lib, "Gdiplus.lib")

BOOL PrintImageUsingGDIPlus( char* szPath, char* szPrn )
{
  DEVMODE  *pDM;
  LONG     dmLen;
  HANDLE   prnHandle;
  HDC      hDC;
  UINT     dimCount, frameCount;
  GUID     *pDimIDs;
  GUID     pageGuid;
  float    width, height;
  DOCINFO  di = { sizeof(DOCINFO), "Printing My Image" };
////
  if( !OpenPrinter( szPrn, &prnHandle, 0 ) )
    return 0;

  dmLen = DocumentProperties( 0, prnHandle, szPrn, 0, 0, 0 );
  pDM = (DEVMODE*)new char[dmLen];
  DocumentProperties( 0, prnHandle, szPrn, pDM, 0, DM_OUT_BUFFER );
////
  hDC = CreateDC( szPrn, szPrn, 0, pDM );
  delete pDM;

  if( !hDC )
  {
    ClosePrinter( prnHandle );
    return 0;
  }

  if( StartDoc( hDC, &di ) == SP_ERROR )
  {
    ClosePrinter( prnHandle );
    return 0;
  }
////
  Graphics graphics( hDC );

// Loading image from file
  USES_CONVERSION;
  Image image( A2W( szPath ) );

  dimCount = image.GetFrameDimensionsCount();
  pDimIDs = new GUID[dimCount];

// Get the list of frame dimensions from the Image object
  image.GetFrameDimensionsList( pDimIDs, dimCount );

// Get the number of frames (pages) in the first dimension
  frameCount = image.GetFrameCount( &pDimIDs[0] );
  delete pDimIDs;

  pageGuid = FrameDimensionPage;

  for( UINT i = 0; i < frameCount; i++ )
  {
    StartPage( hDC );

    image.SelectActiveFrame( &pageGuid, i );
    graphics.SetPageUnit( UnitInch );
		
    width = image.GetWidth() / image.GetHorizontalResolution();
    height = image.GetHeight() / image.GetVerticalResolution();
    graphics.DrawImage( &image, 0.f, 0.f, width, height );
		
    EndPage( hDC );
  }
////
  EndDoc( hDC );
  ClosePrinter( prnHandle );

  return ( frameCount > 0 ) ? TRUE : FALSE;
}


  • Matthew Vance

    USA

    «Problem solved as quick as it took to convert this PDF. I definitely want to keep a copy of Universal Document Converter around for the times when nothing else works. In fact, try it first and save yourself the migraine!»