Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 *'


I am trying to write bitmap on document. and it is not happing this is my problem. blank document display.

My code is:
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);


}
Posted
Updated 13-Jan-16 1:38am
v2
Comments
Jochen Arndt 13-Jan-16 7:33am    
The LoadImage() call with path and LR_LOADFROMFILE should work. If it fails (return value is NULL), you should call GetLastError() to get an error code and optionally look up the error message.

error C2664 is sourced by passing a Unicode string while your application is not a Unicode build.

Enhancing my comment post as answer with a reasonable error source.

The LoadImage() call with path and LR_LOADFROMFILE should work. If it fails (return value is NULL), you should call GetLastError() to get an error code and optionally look up the error message.

If the error code is 5 ("Access denied") it is probably because you are running your program as normal user that has no access to the desktop of the administrator where the file is located.

error C2664 is sourced by passing a Unicode string while your application is not a Unicode build.
 
Share this answer
 
Comments
Kishor-KW 13-Jan-16 9:02am    
if (GetObject(hbit, sizeof(BITMAP), (LPVOID)&bm) == 0)
OutputDebugString("GetObject failed\n");

after executing GetObject failed message is shown what should I do? Let me clear that this class file is not associated with any dialogbox hence there is no picture control, is that reason for this message?
Jochen Arndt 13-Jan-16 9:33am    
So loading the image is successful but GetObject() fails?
The code looks OK. So I don't know what happened.

For testing you can try calling
GetObject(hbit, sizeof(BITMAP), NULL);
This should return the required size = sizeof(BITMAP).

If this fails, your file is probably not a valid bitmap file.

Otherwise, check your project settings for alignment.
From the GetObject() MSDN page:
"The address of lpvObject must be on a 4-byte boundary; otherwise, GetObject fails."
Kishor-KW 13-Jan-16 22:53pm    
what is mean by lpvObject ?
Jochen Arndt 14-Jan-16 3:00am    
The third parameter of GetObject() as described on the MSDN page.
Finally Got solution:
modified code:


HBITMAP hBmp = (HBITMAP)::LoadImage(NULL,kdid.sLogo,
IMAGE_BITMAP,0,0,
LR_LOADFROMFILE|LR_CREATEDIBSECTION);

CBitmap bmp;
bmp.Attach(hBmp);


GetObject(hBmp, sizeof(BITMAP), (LPVOID) &bm);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900