Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 582
  • Last Modified:

Need some help to print a bitmap with StretchBlt

Hello,
I would like to print a bitmap from my resources. Please take a look at my code...what did I do wrong?
I only get a black rectangle...not the bitmap.
Please help. 500 points with a solution.

Thank you.
Best regards,
Thomas



CDC dc;
       CDC memDC;
     CBitmap bitmap;
       int maxw = pDC->GetDeviceCaps(HORZRES);
       int maxh = pDC->GetDeviceCaps(VERTRES);

       pInfo->m_rectDraw.SetRect(0,0,maxw,maxh);

       if(!bitmap.Attach(::LoadImage(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_BITMAP_EAGLE_LOGO),IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION)));

       BITMAP bm;
       bitmap.GetBitmap(&bm);
       memDC.CreateCompatibleDC(&dc);
       CBitmap *pBmp = pDC->SelectObject(&bitmap);

       pDC->StretchBlt(3400,180,maxw+1500,maxh+1500,&memDC,0,0,bm.bmWidth,bm.bmHeight, SRCCOPY);
       memDC.SelectObject(pBmp);




       dc.DeleteDC();
       memDC.DeleteDC();
       bitmap.DeleteObject();

void CAuftragsHistory_Lieferschein_Dialog::On_MY_Print(CDC* pDC, CPrintInfo* pInfo)
{
     CDC dc;
	 CDC memDC;
     CBitmap bitmap;
	 int maxw = pDC->GetDeviceCaps(HORZRES);
	 int maxh = pDC->GetDeviceCaps(VERTRES);

	 pInfo->m_rectDraw.SetRect(0,0,maxw,maxh);

	 if(!bitmap.Attach(::LoadImage(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_BITMAP_EAGLE_LOGO),IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION)));

	 BITMAP bm;
	 bitmap.GetBitmap(&bm);
	 memDC.CreateCompatibleDC(&dc);
	 CBitmap *pBmp = pDC->SelectObject(&bitmap);

	 pDC->StretchBlt(3400,180,maxw+1500,maxh+1500,&memDC,0,0,bm.bmWidth,bm.bmHeight, SRCCOPY);
	 memDC.SelectObject(pBmp);




	 dc.DeleteDC();
	 memDC.DeleteDC();
	 bitmap.DeleteObject();

   





	//Druckbereich in 0,1mm
	pDC->SetMapMode(MM_LOMETRIC);
    //--------------------------------------
	pInfo->SetMaxPage(1);
   //--------------------------------------

 

	CFont* pOldFont = pDC->SelectObject(m_fontUeberschrift0);
	//--------------------- Briefkopf -------------------------------
	 pDC->TextOut(155,-180,"Name of the company");
.
.
.
.

Open in new window

0
tsp2002
Asked:
tsp2002
  • 4
  • 3
1 Solution
 
sarabandeCommented:
i think you need to create a compatible memory dc from pDC and not from (uninitialized) dc.

then the bitmap probably needs to get selected to the memDC cause that is the source to copy the bitmap from with StretchBlt.

Sara
0
 
tsp2002Author Commented:
Sara, do you mean this:?
memDC.CreateCompatibleDC(&pDC);



then the bitmap probably needs to get selected to the memDC cause that is the source to copy the bitmap from with StretchBlt.

How should I do this?
0
 
sarabandeCommented:
no, the pDC already is a pointer:

BOOL ok = memDC.CreateCompatibleDC(pDC);

then, select bitmap into memDC:

memDC.SelectObject(&bitmap);

you do that after StretchBlt and use the old bitmap selected by pDC what most likely is none, means the pBmp is NULL.

so the memDC should be compatible with the device context pDC is pointing to (what is a device context for the current screen) and you load the bitmap to the memory dc (what means that device context has the bitmap in the top-left corner and nothing else). then you copy it from the memory dc to the screen dc using StretchBlt.

Sara


0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
sarabandeCommented:
'you do that after StretchBlt' should read 'your current code does a SelectObject to the memory dc after StretchBlt what obviously must be wrong.

Sara
0
 
tsp2002Author Commented:
Sara, did you mean this? Same result no bitmap...just a tiny little black box

void CAuftragsHistory_Lieferschein_Dialog::On_MY_Print(CDC* pDC, CPrintInfo* pInfo)
{
     CDC dc;
       CDC memDC;
       CPrintInfo Info;
     CBitmap bitmap;

       int maxw = pDC->GetDeviceCaps(HORZRES);
       int maxh = pDC->GetDeviceCaps(VERTRES);

       pInfo->m_rectDraw.SetRect(0,0,maxw,maxh);

       if(!bitmap.Attach(::LoadImage(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_BITMAP_EAGLE_LOGO),IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION)));

       BITMAP bm;
       bitmap.GetBitmap(&bm);
       BOOL ok= memDC.CreateCompatibleDC(pDC);
       CBitmap *pBmp = pDC->SelectObject(&bitmap);
     memDC.SelectObject(pBmp);
        


     pDC->StretchBlt(3400,180,maxw,maxh,&memDC,0,0,bm.bmWidth,bm.bmHeight, SRCCOPY);
      




       dc.DeleteDC();
       memDC.DeleteDC();
       bitmap.DeleteObject();

   





      //Druckbereich in 0,1mm
      pDC->SetMapMode(MM_LOMETRIC);
    //--------------------------------------
      pInfo->SetMaxPage(1);
   //--------------------------------------

0
 
sarabandeCommented:
instead of

 CBitmap *pBmp = pDC->SelectObject(&bitmap);

do

  memDC.SelectObject(&bitmap);

drop the

     memDC.SelectObject(pBmp);

you don't need to save ld bitmap of memDC cause memDC is a temorary device context only.

and you don't need to save old bitmap of pDC (what is a pointer to the current device context) cause it has no old bitmap and it won't load (select) a bitmap neither.

instead the StretchBlt copies bitmap from memDC to pDC.

Sara
0
 
tsp2002Author Commented:
Hello Sara,
yes...it works...I can print the bitmap.
Thank you for your help.
Best regards,
Thomas

void CAuftragsHistory_Lieferschein_Dialog::On_MY_Print(CDC* pDC, CPrintInfo* pInfo)
{
 
	 CDC memDC;
	 CBitmap bitmap;

	 int maxw = pDC->GetDeviceCaps(HORZRES);
	 int maxh = pDC->GetDeviceCaps(VERTRES);

	 pInfo->m_rectDraw.SetRect(0,0,maxw,maxh);

	 if(!bitmap.Attach(::LoadImage(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_BITMAP_LOGO_WST_BRIEF2),IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION)));
	 
    
	 BITMAP bm;
	 bitmap.GetBitmap(&bm);
	 BOOL ok= memDC.CreateCompatibleDC(pDC);
	 memDC.SelectObject(&bitmap);

	 
	  

 

	   pDC->StretchBlt(3550,430,950,635,&memDC,0,0,bm.bmWidth,bm.bmHeight, SRCCOPY);
	   


 
	 memDC.DeleteDC();
	 bitmap.DeleteObject();

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now