c++
------------------------------------
void CVC4SampleDlg::SendNVBitImage( HANDLE hPrinter, UINT lBitmapName, WORD wKeyCode)
{
    CDC dcPrinter, dcMemory; // printer device context,  memory device context
BITMAP SrcBitmap; // bitmap
CBitmap bitmap1, bitmap2;
CBitmap* oldBitmap;
COLORREF color; // color of the selected pixel
DWORD dwWritten = 0; // number of bytes written to printer

// create printer device context
dcPrinter.CreateDC(NULL, m_cmbPrinters, NULL, NULL);
// load the bitmap from the resource file
bitmap1.LoadBitmap( lBitmapName ); //image width should be multiple of 8
// the the bitmap properties and store in SrcBitmap
GetObject(bitmap1.operator HBITMAP( ), sizeof(BITMAP), (LPSTR)&SrcBitmap);
// create a device context in memory
dcMemory.CreateCompatibleDC( &dcPrinter );
// select the bitmap into memory
oldBitmap = dcMemory.SelectObject( &bitmap1 ); // get the size of the array based on the size of the bitmap
int sendWidth = 0;
int sendHeight = 0; if ((SrcBitmap.bmWidth % 8) != 0) {
sendWidth = 1;
}
sendWidth = sendWidth + (int)(SrcBitmap.bmWidth / 8);
if ((SrcBitmap.bmHeight % 8) != 0) {
sendHeight = 1;
}
sendHeight = sendHeight + (int)(SrcBitmap.bmHeight / 8); int size = sendHeight * (sendWidth * 8) + 4;

    unsigned char* pImage1Data = new unsigned char[ size ]; // set xL and xH to the width of the bitmap
*(WORD*)&pImage1Data[0] = (WORD)(sendWidth % 256);
*(WORD*)&pImage1Data[1] = (WORD)(int)(sendWidth / 256);
// set yL and yH to the height of the bitmap
*(WORD*)&pImage1Data[2] = (WORD)(sendHeight % 256);
*(WORD*)&pImage1Data[3] = (WORD)(int)(sendHeight / 256); unsigned char data = 0x00;
unsigned char data_mask = 0x80; int counter = 4; // position in the array
for (int i = 0; i < SrcBitmap.bmWidth; ++i){
for (int j = 0; j < SrcBitmap.bmHeight; ++j){
// get the color of the pixel
color = GetPixel( dcMemory, i, j );
if( color != 0xFFFFFF ){
// if the color is not white
// 1 means a pixel will be printed
data = data | data_mask;

data_mask >>= 1; if( j % 8 == 7 && j != 0 ){
//if data contains 8 bits store in the char array
pImage1Data[counter] = data;
counter++;
data = 0x00;
data_mask = 0x80;
}
}
if (data_mask != 0x80) {
pImage1Data[counter] = data;
counter++;
data = 0x00;
data_mask = 0x80;
}
} for (;counter < size; counter++ ) {
pImage1Data[counter] = 0x00;
} // send the bitmap image to the printer
WritePrinter(hPrinter, pImage1Data, size, &dwWritten);
delete [] pImage1Data;
dcMemory.SelectObject( oldBitmap );
DeleteDC( dcMemory);
return;
}
-----------------------------------
c#
-----------------------------------
     public static void PrintBitmap(System.Drawing.Bitmap bmp)
        {
       
            
            int sendwidth = 0;
            int sendheight = 0;
            if ((bmp.Width % 8) != 0)
            {
                sendwidth = 1;
            }
            sendwidth = sendwidth + (int)(bmp.Width / 8);
            if ((bmp.Height % 8) != 0)
            {
                sendheight = 1;
            }
            sendheight = sendheight + (int)(bmp.Height / 8);
            int size = sendheight * (sendwidth * 8)+4 ;
            byte[] BitString = new byte[size];
            BitString[0] = (byte)(sendwidth % 256);
            BitString[1] = (byte)(int)(sendwidth / 256);
            BitString[2] = (byte)(sendheight % 256);
            BitString[3] = (byte)(int)(sendheight / 256);
            byte data = 0x00;
            byte data_mask = 0x80;
            int counter =4;
            for (int i = 0; i < bmp.Width; i += 1) 
            {
                for (int j = 0; j < bmp.Height; j += 1)
                {
                    System.Drawing.Color color = bmp.GetPixel( i, j);
                    if (color != System.Drawing.Color.White)
                    {
                        data = (byte)(data | data_mask);
                    }
                    data_mask = (byte)(data_mask>> 1);
                    if (j % 8 == 7 && j != 0)
                    {
                        BitString[counter] = data;
                        counter += 1;
                        data = 0x00;
                        data_mask = 0x80;
                    }
                }
                if ((byte)data_mask != 0x80)
                {
                    BitString[counter] = data;
                    counter += 1;
    
                    data = 0x00;
                    data_mask = 0x80;
                }
            }
            for (int i = counter; i < size; i += 1)
            {
                BitString[i] = 0;
               
            }
            IntPtr ptr1 = Marshal.AllocCoTaskMem((int)size);
            Marshal.Copy(BitString, 0, ptr1, (int)size);
            WritePrinter(lhPrinter, ptr1, size, ref pcWritten);        }