Click here to Skip to main content
15,926,596 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: How to print color BITMAP from ressource on Printer ? Pin
Hamid_RT7-Mar-07 0:22
Hamid_RT7-Mar-07 0:22 
AnswerRe: How to print color BITMAP from ressource on Printer ? Pin
Hamid_RT6-Mar-07 18:15
Hamid_RT6-Mar-07 18:15 
GeneralRe: How to print color BITMAP from ressource on Printer ? Pin
jerome_data6-Mar-07 20:39
jerome_data6-Mar-07 20:39 
AnswerRe: How to print color BITMAP from ressource on Printer ? Pin
prasad_som6-Mar-07 22:25
prasad_som6-Mar-07 22:25 
GeneralRe: How to print color BITMAP from ressource on Printer ? Pin
Mark Salsbery7-Mar-07 3:48
Mark Salsbery7-Mar-07 3:48 
GeneralRe: How to print color BITMAP from ressource on Printer ? Pin
prasad_som7-Mar-07 23:01
prasad_som7-Mar-07 23:01 
GeneralRe: How to print color BITMAP from ressource on Printer ? Pin
Hamid_RT7-Mar-07 0:20
Hamid_RT7-Mar-07 0:20 
AnswerRe: I found it's DIB and DDB problem Pin
jerome_data7-Mar-07 23:28
jerome_data7-Mar-07 23:28 
Used this code to print a bitmap on printer and display

void YourClass::PrintBitmap(CDC *pDC, CPrintInfo *pInfo,UINT Bitmap,int x,int y)
{

LPBITMAPINFO info; // Structure for storing the DIB information,
// it will be used by 'StretchDIBits()'
HBITMAP hbit; // Handle to the bitmap to print
BITMAP bm; // Structure used for obtaining information
// about the bitmap (size, color depth...)
int nColors = 0; // Used to store the number of colors the DIB has
int sizeinfo = 0; // Will contain the size of info
RGBQUAD rgb[256]; // Used to store the DIB color table

// The following line loads the bitmap from resource bitmap
hbit = (HBITMAP) LoadImage(AfxGetInstanceHandle(),
MAKEINTRESOURCE(Bitmap),
IMAGE_BITMAP,
0,
0,
LR_CREATEDIBSECTION);


// Obtain information about 'hbit' and store it in 'bm'
GetObject(hbit, sizeof(BITMAP), (LPVOID) &bm);

nColors = (1 << bm.bmBitsPixel);
if(nColors > 256)
nColors=0; // This is when DIB is 24 bit.
// In this case there is not any color table information

// Now we need to know how much size we have to give for storing "info" in memory.
// This involves the proper BITMAPINFO size and the color table size.
// 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;

// If the bitmap is a compressed bitmap (BI_RLE for example), the 'biSizeImage' can not
// be calculated that way. A call to 'GetDIBits()' will fill the 'biSizeImage' field with
// the correct size.

// Now for 256 or less color DIB we have to fill the "info" color table parameter
if(nColors <= 256)
{
HBITMAP hOldBitmap;
HDC hMemDC = CreateCompatibleDC(NULL); // Creating an auxiliary device context

hOldBitmap = (HBITMAP) SelectObject(hMemDC, hbit); // Select 'hbit' in 'it'
GetDIBColorTable(hMemDC, 0, nColors, rgb); // Obtaining the color table information

// Now we 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);
}

// Let's get 'StretchDIBiting'! 'pDC' is the printer DC
HDC hdc = pDC->GetSafeHdc(); // Getting a safe handle

// Stretching all the bitmap on a destination rectangle of size (size_x, size_y)
// and upper left corner at (initial_pos_x, initial_pos_y)
StretchDIBits(hdc,
x,
y,
bm.bmWidth,
bm.bmHeight,
0,
0,
bm.bmWidth,
bm.bmHeight,
bm.bmBits,
info,
DIB_RGB_COLORS,
SRCCOPY);

// This mode, DIB_RGB_COLORS indicate the color table are pure RGB values
// The mode DIB_PAL_COLORS indicate the color table items are index to the local palette items

DeleteObject(hbit);
free(info)
}
Questiontemplate method in c++ Pin
mehmetned6-Mar-07 11:40
mehmetned6-Mar-07 11:40 
AnswerRe: template method in c++ Pin
Stephen Hewitt6-Mar-07 11:46
Stephen Hewitt6-Mar-07 11:46 
GeneralRe: template method in c++ Pin
mehmetned6-Mar-07 11:58
mehmetned6-Mar-07 11:58 
GeneralRe: template method in c++ Pin
Stephen Hewitt6-Mar-07 12:03
Stephen Hewitt6-Mar-07 12:03 
QuestionCDialogBar in a CDialog Appl. for HeaderCtrl Column Chooser? Pin
-273C6-Mar-07 11:37
-273C6-Mar-07 11:37 
QuestionQuestion about memory writing and reading Pin
godspeed1236-Mar-07 11:36
godspeed1236-Mar-07 11:36 
AnswerRe: Question about memory writing and reading Pin
PJ Arends6-Mar-07 12:12
professionalPJ Arends6-Mar-07 12:12 
QuestionMigrating from VC++ 6.0 MFC to VC++ 2005 Pin
BuckBrown6-Mar-07 11:33
BuckBrown6-Mar-07 11:33 
AnswerRe: Migrating from VC++ 6.0 MFC to VC++ 2005 Pin
prasad_som6-Mar-07 17:19
prasad_som6-Mar-07 17:19 
Questionhow to learn multithreading Pin
l_d6-Mar-07 9:07
l_d6-Mar-07 9:07 
AnswerRe: how to learn multithreading Pin
David Crow6-Mar-07 9:53
David Crow6-Mar-07 9:53 
AnswerRe: how to learn multithreading Pin
Hamid_RT6-Mar-07 18:21
Hamid_RT6-Mar-07 18:21 
GeneralRe: how to learn multithreading Pin
l_d7-Mar-07 8:06
l_d7-Mar-07 8:06 
GeneralRe: how to learn multithreading Pin
Hamid_RT7-Mar-07 17:47
Hamid_RT7-Mar-07 17:47 
Questionstop scrolling of first column in CListView / CListCtrl Pin
ensger6-Mar-07 8:18
ensger6-Mar-07 8:18 
AnswerRe: stop scrolling of first column in CListView / CListCtrl Pin
Michael Dunn6-Mar-07 12:29
sitebuilderMichael Dunn6-Mar-07 12:29 
Questionpush_back ina vector - Not working for me Pin
LCI6-Mar-07 7:00
LCI6-Mar-07 7:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.