Solved

Need some help to print a bitmap with StretchBlt

Posted on 2011-09-13
7
527 Views
Last Modified: 2012-05-12
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
Comment
Question by:tsp2002
  • 4
  • 3
7 Comments
 
LVL 33

Expert Comment

by:sarabande
ID: 36529578
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
 

Author Comment

by:tsp2002
ID: 36530640
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
 
LVL 33

Expert Comment

by:sarabande
ID: 36530751
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 
LVL 33

Expert Comment

by:sarabande
ID: 36530774
'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
 

Author Comment

by:tsp2002
ID: 36534374
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
 
LVL 33

Accepted Solution

by:
sarabande earned 500 total points
ID: 36534478
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
 

Author Comment

by:tsp2002
ID: 36535018
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question