As mentioned, LoadImage loads an image ready for output on a screen. You have the alternative already given, or to use GDI+ to load the image. The advantage of using GDI+ is that not only is a DIB created, but you can also load formats including png, jpg and gif.
Provided you have already intialized GDI+, the following function will load an image ready for screen or print.
HBITMAP mLoadImageFile(wchar_t *filename)
{
HBITMAP result = NULL;
Bitmap bitmap(filename, false);
bitmap.GetHBITMAP(0, &result);
return result;
}
Failing the use of GDI+, you can use the dib section flag with LoadImage.
The following code creates a 1 page pdf, loading a copy of the CP logo that's been converted from gif to bmp.
Just:
(a) change the name of the loaded image in the
drawPage
function
(b) change the name of the printer in the 1st line of
main
Note: Link against the gdi32 and winspool libraries.
#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
void drawPage(HDC hdc)
{
HBITMAP logo = (HBITMAP)LoadImage(NULL, "logo.bmp", IMAGE_BITMAP, 0,0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);
int pageWidth, pageHeight;
pageWidth = GetDeviceCaps( hdc, HORZRES ),
pageHeight = GetDeviceCaps( hdc, VERTRES );
int dpiX, dpiY;
dpiX = GetDeviceCaps(hdc, LOGPIXELSX);
dpiY = GetDeviceCaps(hdc, LOGPIXELSY);
double dpiMmX, dpiMmY;
dpiMmX = dpiX / 25.4;
dpiMmY = dpiY / 25.4;
HBRUSH redBrush = CreateSolidBrush( RGB(255,0,0) );
RECT pageRect = {0,0,pageWidth,pageHeight};
int borderWidth = dpiMmX * 10;
int borderHeight = dpiMmY * 10;
RECT borderRect = {borderWidth, borderHeight, pageWidth-1-borderWidth, pageHeight-1-borderHeight};
FrameRect(hdc, &pageRect, redBrush);
int i;
for (i=0; i<5; i++)
{
FrameRect(hdc, &borderRect, redBrush);
InflateRect(&borderRect, -1, -1);
}
HDC memDC = CreateCompatibleDC(hdc);
HBITMAP old = (HBITMAP)SelectObject(memDC, logo);
StretchBlt(hdc, 100,100, 1000,540, memDC, 0,0, 250,125, SRCCOPY);
SelectObject(memDC, old);
DeleteObject(logo);
DeleteDC(memDC);
DeleteObject(redBrush);
}
void testPrint(char *szPrinterName)
{
DOCINFO diDocInfo = {0};
diDocInfo.cbSize = sizeof( DOCINFO );
diDocInfo.lpszDocName = "printTest";
HANDLE printerHandle;
if (OpenPrinter(szPrinterName, &printerHandle, NULL) != 0)
{
HDC pDC2 = CreateDC("", szPrinterName, NULL, NULL);
if( StartDoc( pDC2, &diDocInfo ) > 0 )
{
if( StartPage( pDC2 ) > 0 )
{
drawPage(pDC2);
EndPage(pDC2);
}
EndDoc( pDC2 );
}
ClosePrinter(printerHandle);
}
}
int main()
{
testPrint("CutePDF Writer");
return 0;
}
EDIT: MFC code added
#include "stdafx.h"
#include "CdcTest.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
CWinApp theApp;
using namespace std;
void drawPage(HDC hdc)
{
HBITMAP logo = (HBITMAP)LoadImage(NULL, L"c:/logo.bmp", IMAGE_BITMAP, 0,0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);
int pageWidth, pageHeight;
pageWidth = GetDeviceCaps( hdc, HORZRES ),
pageHeight = GetDeviceCaps( hdc, VERTRES );
int dpiX, dpiY;
dpiX = GetDeviceCaps(hdc, LOGPIXELSX);
dpiY = GetDeviceCaps(hdc, LOGPIXELSY);
double dpiMmX, dpiMmY;
dpiMmX = dpiX / 25.4;
dpiMmY = dpiY / 25.4;
HBRUSH redBrush = CreateSolidBrush( RGB(255,0,0) );
RECT pageRect = {0,0,pageWidth,pageHeight};
int borderWidth = dpiMmX * 10;
int borderHeight = dpiMmY * 10;
RECT borderRect = {borderWidth, borderHeight, pageWidth-1-borderWidth, pageHeight-1-borderHeight};
FrameRect(hdc, &pageRect, redBrush);
int i;
for (i=0; i<5; i++)
{
FrameRect(hdc, &borderRect, redBrush);
InflateRect(&borderRect, -1, -1);
}
HDC memDC = CreateCompatibleDC(hdc);
HBITMAP old = (HBITMAP)SelectObject(memDC, logo);
StretchBlt(hdc, 100,100, 1000,540, memDC, 0,0, 250,125, SRCCOPY);
SelectObject(memDC, old);
DeleteObject(logo);
DeleteDC(memDC);
DeleteObject(redBrush);
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
HMODULE hModule = ::GetModuleHandle(NULL);
if (hModule != NULL)
{
if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
{
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
CDC memDC;
memDC.CreateDCW(NULL, L"CutePDF Writer", NULL, NULL);
DOCINFO diDocInfo = {0};
diDocInfo.cbSize = sizeof( DOCINFO );
diDocInfo.lpszDocName = L"printTest";
if( StartDoc( memDC.GetSafeHdc(), &diDocInfo ) > 0 )
{
if( StartPage( memDC.GetSafeHdc() ) > 0 )
{
drawPage(memDC.GetSafeHdc());
EndPage(memDC.GetSafeHdc());
}
EndDoc( memDC.GetSafeHdc() );
}
}
}
else
{
_tprintf(_T("Fatal Error: GetModuleHandle failed\n"));
nRetCode = 1;
}
return nRetCode;
}