Solved

Need some help to print a bitmap with StretchBlt

Posted on 2011-09-13
7
507 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 32

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 32

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 32

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 32

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Setting font in GroupBox 10 129
__FILE__ 6 174
Pointers - Drive letter 1 59
Visual Studio Community 2015 updates failed 5 73
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 seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now