Click here to Skip to main content
15,884,298 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionPrint Bitmap from Printing Class Library Pin
Member 1403961931-Oct-18 7:29
Member 1403961931-Oct-18 7:29 
AnswerRe: Print Bitmap from Printing Class Library Pin
Victor Nijegorodov31-Oct-18 8:15
Victor Nijegorodov31-Oct-18 8:15 
GeneralRe: Print Bitmap from Printing Class Library Pin
Member 1403961931-Oct-18 8:31
Member 1403961931-Oct-18 8:31 
GeneralRe: Print Bitmap from Printing Class Library Pin
Victor Nijegorodov31-Oct-18 8:37
Victor Nijegorodov31-Oct-18 8:37 
GeneralRe: Print Bitmap from Printing Class Library Pin
Richard Deeming31-Oct-18 9:14
mveRichard Deeming31-Oct-18 9:14 
GeneralRe: Print Bitmap from Printing Class Library Pin
Victor Nijegorodov31-Oct-18 11:06
Victor Nijegorodov31-Oct-18 11:06 
GeneralRe: Print Bitmap from Printing Class Library Pin
Victor Nijegorodov31-Oct-18 8:43
Victor Nijegorodov31-Oct-18 8:43 
AnswerRe: Print Bitmap from Printing Class Library Pin
leon de boer31-Oct-18 17:27
leon de boer31-Oct-18 17:27 
The PrintBitMap code is likely just calling the standard windows API call LoadImage.

LoadImageA function | Microsoft Docs[^]
If you read the documentation the fix will be obvious, you need to not set the LR_LOADFROMFILE flag when passing in a resource ID in the string.

Down the bottom in the remarks you will find reference to a neat macro IS_INTRESOURCE(lpszName).
IS_INTRESOURCE macro | Microsoft Docs[^]
So Microsoft gives you a macro to work out if a string is actually an INTRESOURCE.

So basically a one line fix in the code find the LR_LOADFROMFILE and set it only if IS_INTRESOURCE returns false.
It is the always setting of that flag that is fouling the LoadImage working properly because you want the extended behaviour that is spelled out in this statement
Quote:
The image to be loaded. If the hinst parameter is non-NULL and the fuLoad parameter omits LR_LOADFROMFILE, lpszName specifies the image resource in the hinst module. If the image resource is to be loaded by name from the module, the lpszName parameter is a pointer to a null-terminated string that contains the name of the image resource. If the image resource is to be loaded by ordinal from the module, use the MAKEINTRESOURCE macro to convert the image ordinal into a form that can be passed to the LoadImage function.

As an extended answer you may want to consider adding a few lines of code to test the string extension for known types JPG etc not just resource ID and use IPicture to add that support. As an example this will take a filename string and convert a jpg to a bitmap handle. You would use this function in place of LoadImage in the situation you had a jpeg filename and you could then print jpegs.

The disadvantages of using classes and frameworks is you never learn the Windows API and how it is designed to work. Anyhow the small code block follows, I gave you the option of returning the wth, ht of the jpeg loaded but you can use NULL if you don't want them returned.
#include <olectl.h>	
HBITMAP HBMPFromJPGFile (char* filename, int* returnWth, int* returnHt){
	const int HIMETRIC_PER_INCH = 2540;
	int Wth, Ht;
	HRESULT hr;
	WCHAR OlePathName[512];
	HBITMAP bmp;
	HDC MemDC;
	SIZE sizeInHiMetric;
	IPicture *Ipic = NULL;

	MultiByteToWideChar(CP_ACP, 0, filename, 
		(int)strlen(filename)+1, &OlePathName[0], 512);				// Convert filename to unicode
	hr = OleLoadPicturePath(OlePathName, NULL, 0, 0, 
		IID_IPicture, (LPVOID*) &Ipic);								// Load the picture
	if ((hr == S_OK) & (Ipic != 0)){								// Picture loaded okay																
		HDC Dc = GetDC(NULL);										// Get screen DC
		int nPixelsPerInchX = GetDeviceCaps(Dc, LOGPIXELSX);		// Screen X pixels per inch 
		int nPixelsPerInchY = GetDeviceCaps(Dc, LOGPIXELSY);		// Screen Y pixels per inch
		ReleaseDC(NULL, Dc);										// Release screen DC
		Ipic->get_Width(&sizeInHiMetric.cx);						// Get picture witdh in HiMetric
		Ipic->get_Height(&sizeInHiMetric.cy);						// Get picture height in HiMetric
		Wth = (nPixelsPerInchX * sizeInHiMetric.cx +
              HIMETRIC_PER_INCH / 2) / HIMETRIC_PER_INCH;			// Calculate picture width in pixels
		Ht = (nPixelsPerInchY * sizeInHiMetric.cy +
              HIMETRIC_PER_INCH / 2) / HIMETRIC_PER_INCH;			// Calculate picture height in pixels
		 MemDC = CreateCompatibleDC(0);								// Create a memory device context
		int P2Width = Wth >> 2;										// Divid width by 4
		if ((P2Width << 2) < Wth) P2Width++;						// Inc by 1 if width x 4 is less than original
		P2Width = P2Width << 2;										// Power of two width
		/* Create A Temporary Bitmap */
		BITMAPINFO	bi = {0};										// The Type Of Bitmap We Request
		DWORD		*pBits = 0;										// Pointer To The Bitmap Bits

		bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);				// Set Structure Size
		bi.bmiHeader.biBitCount	= 32;								// 32 Bit
		bi.bmiHeader.biWidth = P2Width;								// Power Of Two Width
		bi.bmiHeader.biHeight = Ht;									// Make Image Top Up (Positive Y-Axis)
		bi.bmiHeader.biCompression	= BI_RGB;						// RGB Encoding
		bi.bmiHeader.biPlanes = 1;									// 1 Bitplane
		bmp = CreateDIBSection(MemDC, &bi, DIB_RGB_COLORS, 
			(void**)&pBits, 0, 0);									// Create a DIB section
	
		Ipic->Release();											// Finished with IPicture release memory
		
		/* If call set pointers to return width and height of jpeg return them */
		if (returnWth) (*returnWth) = Wth;
		if (returnHt) (*returnHt) = Ht;

		return bmp;													// Return bitmap handle
	}
   return 0;														// Return fail result of zero
}

In vino veritas


modified 1-Nov-18 0:17am.

QuestionReferencing a class in other in a other class Pin
ForNow28-Oct-18 3:43
ForNow28-Oct-18 3:43 
AnswerRe: Referencing a class in other in a other class Pin
Victor Nijegorodov28-Oct-18 7:35
Victor Nijegorodov28-Oct-18 7:35 
GeneralRe: Referencing a class in other in a other class Pin
ForNow28-Oct-18 7:37
ForNow28-Oct-18 7:37 
GeneralRe: Referencing a class in other in a other class Pin
Richard MacCutchan28-Oct-18 11:34
mveRichard MacCutchan28-Oct-18 11:34 
GeneralRe: Referencing a class in other in a other class Pin
Stefan_Lang31-Oct-18 3:59
Stefan_Lang31-Oct-18 3:59 
AnswerRe: Referencing a class in other in a other class Pin
Joe Woodbury28-Oct-18 9:02
professionalJoe Woodbury28-Oct-18 9:02 
AnswerRe: Referencing a class in other in a other class Pin
Bram van Kampen2-Nov-18 14:31
Bram van Kampen2-Nov-18 14:31 
QuestionPrivate test case failed Pin
Member 1403429426-Oct-18 9:35
Member 1403429426-Oct-18 9:35 
SuggestionRe: Private test case failed Pin
Richard MacCutchan26-Oct-18 22:41
mveRichard MacCutchan26-Oct-18 22:41 
QuestionRe: Private test case failed Pin
David Crow29-Oct-18 7:11
David Crow29-Oct-18 7:11 
QuestionCatch javascript event in CHtmlView Pin
_Flaviu23-Oct-18 23:37
_Flaviu23-Oct-18 23:37 
AnswerMessage Closed Pin
24-Oct-18 0:48
_Flaviu24-Oct-18 0:48 
GeneralRe: Catch javascript event in CHtmlView Pin
_Flaviu26-Oct-18 1:14
_Flaviu26-Oct-18 1:14 
AnswerRe: Catch javascript event in CHtmlView Pin
Afzaal Ahmad Zeeshan24-Oct-18 0:51
professionalAfzaal Ahmad Zeeshan24-Oct-18 0:51 
Questiondouble signed comparison in C++ Pin
kamal197723-Oct-18 16:42
kamal197723-Oct-18 16:42 
AnswerRe: double signed comparison in C++ Pin
CPallini23-Oct-18 20:47
mveCPallini23-Oct-18 20:47 
AnswerRe: double signed comparison in C++ Pin
Victor Nijegorodov23-Oct-18 21:04
Victor Nijegorodov23-Oct-18 21:04 

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.