?
Solved

Need some help to print a bitmap with StretchBlt

Posted on 2011-09-13
7
Medium Priority
?
556 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 35

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 35

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
[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

 
LVL 35

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 35

Accepted Solution

by:
sarabande earned 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

764 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