Hi there, Im trying to make a program that takes in a filename of a bmp and then prints out the BMP on a printer selected by the user. Below is the code I have written so far, and when I run it, it asks for the printer name then the filename name , and after i enter these the printer just feeds through 1 sheet of paper and doesnt print anything, I have no idea why, any help would be much appreciated, Thanx in advance.
Here comes the code :
Code:
#include <windows.h>
#include <windef.h>
#include <wingdi.h>
#include <stdio.h>
#include <winspool.h>
#include <fstream.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;
class printer_select;
class imageprint{
DOCINFO di;
LPBITMAPINFO info; // Struct for storing the DIB information,
// it will be used by 'StretchDIBits()'
HBITMAP hbit; // Handle to the bitmap to print
BITMAP bm; // Struct used for obtaining information
// about the bitmap (size, color depth...)
int nColors; // store the number of colors the DIB has
int sizeinfo; // Will contain the size of info
RGBQUAD rgb[256]; // Used to store the DIB color table
int YOUR_BITMAP_ID;
public:
imageprint(LPCTSTR file_name);
imageprint(HANDLE fileinstance);
// int print(printer_select &printer_ptr); do this later
~imageprint();
};
class printer_select {
HDC pDC;
TCHAR Device[50];
public:
printer_select();
friend int imageprint::print(printer_select &printer_ptr);
~printer_select();
};
imageprint::imageprint(LPCTSTR file_name){
nColors = 0; //store the number of colors the DIB has
sizeinfo = 0; // Will contain the size of info
// The following line loads the bitmap from a file
hbit = (HBITMAP) LoadImage(0,
file_name,
IMAGE_BITMAP,
0,
0,
LR_CREATEDIBSECTION | LR_LOADFROMFILE);
// Obtain information about 'hbit' and store it in 'bm'
GetObject(hbit, sizeof(BITMAP), (LPVOID) &bm);
int nColors = (1 << bm.bmBitsPixel);
if(nColors > 256)
nColors=0; // This is when DIB is 24 bit.
// Color table is only needed when the DIB has 256 colors or less.
sizeinfo = sizeof(BITMAPINFO) + (nColors * sizeof(RGBQUAD));
// This is the size required
info = (LPBITMAPINFO) malloc(sizeinfo); // Storing info in memory
// Before 'StretchDIBits()' we have to fill some "info" fields.
// This information was stored in 'bm'.
info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
info->bmiHeader.biWidth = bm.bmWidth;
info->bmiHeader.biHeight = bm.bmHeight;
info->bmiHeader.biPlanes = 1;
info->bmiHeader.biBitCount = bm.bmBitsPixel * bm.bmPlanes;
info->bmiHeader.biCompression = BI_RGB;
info->bmiHeader.biSizeImage = bm.bmWidthBytes*bm.bmHeight;
info->bmiHeader.biXPelsPerMeter = 0;
info->bmiHeader.biYPelsPerMeter = 0;
info->bmiHeader.biClrUsed = 0;
info->bmiHeader.biClrImportant = 0;
di.cbSize = sizeof(DOCINFO);
di.lpszDocName = file_name;
di.lpszOutput = (LPTSTR) NULL;
if(nColors <= 256)
{
HBITMAP hOldBitmap;
HDC hMemDC = CreateCompatibleDC(NULL);
hOldBitmap = (HBITMAP) SelectObject(hMemDC, hbit);
GetDIBColorTable(hMemDC, 0, nColors, rgb);
// pass this color information to "info"
for(int iCnt = 0; iCnt < nColors; ++iCnt)
{
info->bmiColors[iCnt].rgbRed = rgb[iCnt].rgbRed;
info->bmiColors[iCnt].rgbGreen = rgb[iCnt].rgbGreen;
info->bmiColors[iCnt].rgbBlue = rgb[iCnt].rgbBlue;
}
SelectObject(hMemDC, hOldBitmap);
DeleteDC(hMemDC);
}
}
imageprint::~imageprint(){
DeleteObject(hbit);
}
int imageprint::print(printer_select &printer_ptr){
//'StretchDIBiting'! 'pDC' is the printer DC
HDC hdc = CreateCompatibleDC(printer_ptr.pDC);
StartDoc(printer_ptr.pDC, &di);
StartPage(printer_ptr.pDC);
StretchDIBits(hdc,
0, //initial_pos_x
0, //initial_pos_y
info->bmiHeader.biWidth, //size_x
info->bmiHeader.biHeight, //size_y
0,
0,
bm.bmWidth,
bm.bmHeight,
bm.bmBits,
info,
DIB_RGB_COLORS,
SRCCOPY);
EndPage(printer_ptr.pDC);
EndDoc(printer_ptr.pDC);
return 0;
}
printer_select::printer_select(){
cout<<"enter printername: ";
cin >> Device;
pDC = CreateDC("WINSPOOL",Device,NULL,NULL);
}
printer_select::~printer_select(){
DeleteDC(pDC);
}
int main(void)
{
printer_select *printer_ptr;
printer_ptr = new printer_select;
TCHAR filename[50];
cout << "enter filename to print: ";
cin >> filename;
imageprint first_image(filename);
first_image.print(*printer_ptr);
}