-
January 12th, 2016, 03:42 AM
#1
LoadImage bitmap runtime
I want to display Logo on my document like other data which I retrieve from database are store into variable and then those are passes to print on document. I am storing path of related bitmap in database and it is also retrieve into variable I know following:
hbit = (HBITMAP) LoadImage(AfxGetInstanceHandle(),
// MAKEINTRESOURCE(IDB_BITMAP4),
MAKEINTRESOURCE(IDB_BITMAPNAME),
IMAGE_BITMAP,
0,
0,
LR_CREATEDIBSECTION); SRCCOPY);
IDB_BITMAPNAME is a BITMAP which File Name Property set to the logo's path.
This is a static way to do means I already set FileName Property of BITMAP. I want to set it at runtime. please help
I tried as follows but not work,
hbit = (HBITMAP) LoadImage(NULL,
// MAKEINTRESOURCE(IDB_BITMAP4),
sLogopath,
IMAGE_BITMAP,
0,
0,
LR_LOADFROMFILE);
sLogoPath is CString type variable which contain C:\Users\Administrator\Desktop\cg_logoa.bmp path of bitmap
I also tried for dilectly mention path instead of variable "C:\\Users\\Administrator\\Desktop\\cg_logoa.bmp"
Also tried for this
L"C:\\Users\\Administrator\\Desktop\\cg_logoa.bmp" but it gives following error
error C2664: 'LoadImageA' : cannot convert parameter 2 from 'unsigned short [44]' to 'const char *'
-
January 12th, 2016, 06:58 AM
#2
Re: LoadImage bitmap runtime
Well, you never say what exactly does not work in your code. Did you test hbit for being not NULL after the call? In case it is not, the LoadImage() call works fine, and your problem is somewhere else.
Best regards,
Igor
-
January 12th, 2016, 08:29 AM
#3
Re: LoadImage bitmap runtime
Originally Posted by Igor Vartanov
Well, you never say what exactly does not work in your code. Did you test hbit for being not NULL after the call? In case it is not, the LoadImage() call works fine, and your problem is somewhere else.
1] I want to display bitmap image on document after click on print preview, sLogo is variable which contain path of that logo which is retrieved in listbox control (which is on anther dialogbox) from access database.
2] I retrieved other text value also in same way and all displayed properly on print preview click. but bitmap image is not displayed. (actually database sLogo contain proper path of that bitmap Image located on desktop)
3] My logo name is cg_logoa and I saved it in database as cg_logoa.bmp with path, is there need to rename my logo on desktop as cg_logoa.bmp.?
please help and answer of all questions.
-
January 12th, 2016, 10:48 AM
#4
Re: LoadImage bitmap runtime
Look, your explanation explains really not a thing. It doesn't matter for issue resolution what you want in general, or where you get the path from. The question was what exactly does not work. And please stop giving out please-helps and focus on the questions you are asked with.
What is the Win API call you get an error with? What is the error code? Do you check at all error states in your code? Did you debug the app?
You either answer the questions or upload here a simplistic project that replicates the faulty behavior.
Best regards,
Igor
-
January 12th, 2016, 01:53 PM
#5
Re: LoadImage bitmap runtime
Originally Posted by Igor Vartanov
Look, your explanation explains really not a thing. It doesn't matter for issue resolution what you want in general, or where you get the path from. The question was what exactly does not work. And please stop giving out please-helps and focus on the questions you are asked with.
What is the Win API call you get an error with? What is the error code? Do you check at all error states in your code? Did you debug the app?
You either answer the questions or upload here a simplistic project that replicates the faulty behavior.
I am trying to write bitmap on document. and it is not happing this is my problem. blank document display.
My code is(which I already mention is the question) :
hbit = (HBITMAP) LoadImage(NULL,
// MAKEINTRESOURCE(IDB_BITMAP4),
sLogopath,
IMAGE_BITMAP,
0,
0,
LR_LOADFROMFILE);
-
January 12th, 2016, 02:17 PM
#6
Re: LoadImage bitmap runtime
When calling any WIN32 API function, you need to test whether the call has been successful or not. LoadImage() is documented here https://msdn.microsoft.com/en-us/lib...=vs.85%29.aspx and if the function fails, the return value is NULL. So you need something like
Code:
hbit = (HBITMAP) LoadImage(NULL, sLogopath, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
if (hbit == NULL) {
//error occured - use GetLastError() to obtain error code
char err[255] = {0};
_snprintf(err, 254, "LoadImage error: %i\n", GetLastError());
OutputDebugString(err);
}
and similar for all other WIn32 API's used. Then you know which function is failing and the error code - which can be used to find the error reason.
Last edited by 2kaud; January 12th, 2016 at 02:19 PM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 12th, 2016, 02:19 PM
#7
Re: LoadImage bitmap runtime
1. Did you read the MSDN article LoadImage function?
2. What is the return value of LoadImage?
3. If it is NULL then what does the GetLastError return?
Victor Nijegorodov
-
January 12th, 2016, 09:27 PM
#8
Re: LoadImage bitmap runtime
Originally Posted by kiwagh105@gmail.com
I am trying to write bitmap on document. and it is not happing this is my problem. blank document display.
My code is(which I already mention is the question) :
hbit = (HBITMAP) LoadImage(NULL,
// MAKEINTRESOURCE(IDB_BITMAP4),
sLogopath,
IMAGE_BITMAP,
0,
0,
LR_LOADFROMFILE);
Okay, you've answered Igor's first question, what about the rest of the questions?
1) What is the Win API call you get an error with?
2) What is the error code?
3) Do you check at all error states in your code?
4) Did you debug the app?
-
January 12th, 2016, 11:57 PM
#9
Re: LoadImage bitmap runtime
Originally Posted by Arjay
Okay, you've answered Igor's first question, what about the rest of the questions?
1) What is the Win API call you get an error with?
2) What is the error code?
3) Do you check at all error states in your code?
4) Did you debug the app?
1] I got Error on new sub dialogload open using DoModal Method.
which is Debig Assertion Failed.
file afxwin1.inl (bur when I ckick in ignore from three buttons Abort Retry Ignore) It works.
2]If I write L before path of bitmap (as search on google) It gives error
'LoadImageA' : cannot convert parameter 2 from 'unsigned short [44]' to 'const char *'
3] did but not getting
4]did but not understand
-
January 13th, 2016, 02:34 AM
#10
Re: LoadImage bitmap runtime
Originally Posted by 2kaud
When calling any WIN32 API function, you need to test whether the call has been successful or not. LoadImage() is documented here https://msdn.microsoft.com/en-us/lib...=vs.85%29.aspx and if the function fails, the return value is NULL. So you need something like
Code:
hbit = (HBITMAP) LoadImage(NULL, sLogopath, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
if (hbit == NULL) {
//error occured - use GetLastError() to obtain error code
char err[255] = {0};
_snprintf(err, 254, "LoadImage error: %i\n", GetLastError());
OutputDebugString(err);
}
and similar for all other WIn32 API's used. Then you know which function is failing and the error code - which can be used to find the error reason.
Thanks for reply. hbit is not NULL.
-
January 13th, 2016, 02:59 AM
#11
Re: LoadImage bitmap runtime
Originally Posted by kiwagh105@gmail.com
Thanks for reply. hbit is not NULL.
Then LoadImage is not your problem. As I said before.
Last edited by Igor Vartanov; January 13th, 2016 at 03:02 AM.
Best regards,
Igor
-
January 13th, 2016, 04:10 AM
#12
Re: LoadImage bitmap runtime
Thanks for reply. hbit is not NULL.
Are you doing similar checks for every WIN32 API call? If no, then these types of checks need to be added. If yes then which API call fails?
It would also be useful for us to be able to offer further guidance if you posted the code involved and not just one line.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 13th, 2016, 05:43 AM
#13
Re: LoadImage bitmap runtime
Originally Posted by 2kaud
Are you doing similar checks for every WIN32 API call? If no, then these types of checks need to be added. If yes then which API call fails?
It would also be useful for us to be able to offer further guidance if you posted the code involved and not just one line.
Sorry I did not get the question. which are the WIN32 API I don't know about it ? how to check it? I did just debugging.
My code:
Code:
pDC->SetMapMode(MM_TEXT);
///////////////
LPBITMAPINFO info;
HBITMAP hbit; // Handle to the bitmap to print
BITMAP bm; // Structure used for obtaining information
// about the bitmap (size, color depth...)
int nColors = 0;
int sizeinfo = 0;
RGBQUAD rgb[256];
// The following line loads the bitmap from resource bitmap
hbit = (HBITMAP) LoadImage(NULL,
zakas, // It is a variable which contain path of bitmap file
IMAGE_BITMAP,
0,
0,
LR_LOADFROMFILE);
// Obtain information about 'hbit' and store it in 'bm'
GetObject(hbit, sizeof(BITMAP), (LPVOID) &bm);
nColors = (1 << bm.bmBitsPixel);
if(nColors > 256)
nColors=0; /
sizeinfo = sizeof(BITMAPINFO) + (nColors * sizeof(RGBQUAD));
info = (LPBITMAPINFO) malloc(sizeinfo);
// 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(nColors <= 256)
{
HBITMAP hOldBitmap;
HDC hMemDC = CreateCompatibleDC(NULL);
hOldBitmap = (HBITMAP) SelectObject(hMemDC, hbit);
GetDIBColorTable(hMemDC, 0, nColors, rgb);
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);
}
HDC hdc = pDC->GetSafeHdc();
StretchDIBits(hdc,
3480,//400
250,//280
384*3,//350
160*3,//350
0,
0,
bm.bmWidth,
bm.bmHeight,
bm.bmBits,
info,
DIB_RGB_COLORS,
SRCCOPY);
DeleteObject(hbit);
free(info);
///////////////
pDC->SetMapMode(MM_ANISOTROPIC);
pDC->SetWindowExt(827,-1169*2);
int xlogpix=pDC->GetDeviceCaps(LOGPIXELSX);
int ylogpix=pDC->GetDeviceCaps(LOGPIXELSY);
int xextent=827*xlogpix/100;
int yextent=1169*2*ylogpix/100;
pDC->SetViewportExt(xextent,yextent);
HeadeMultiple(pDC,pInfo,pDoc->flag_serial);
}
Last edited by kiwagh105@gmail.com; January 13th, 2016 at 06:49 AM.
-
January 13th, 2016, 06:01 AM
#14
Re: LoadImage bitmap runtime
Originally Posted by kiwagh105@gmail.com
Sorry I did not get the question. which are the WIN32 API I don't know about it ? how to check it? I did just debugging.
My code:
Please, edit your post adding CODE tags around the code snippets. Otherwise your code is unreadable.
Read Announcements: Before you post ...
About Wi32 API you could read at least in wiki: https://en.wikipedia.org/wiki/Windows_API
Victor Nijegorodov
-
January 13th, 2016, 06:15 AM
#15
Re: LoadImage bitmap runtime
I add code tag please see #13.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|