Solved

Need some help to print a bitmap with StretchBlt

Posted on 2011-09-13
7
523 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

777 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