Solved

Printing a DIB

Posted on 2000-04-06
5
320 Views
Last Modified: 2013-11-20
I've been trying to figure this out for days i can open a bitmap with the following code in my CProjectView but how do i print it.

heres the CprojectView
Please help

// ProjectView.cpp : implementation of the CProjectView class
//
#include "stdafx.h"
#include "Project.h"
#include "ProjectDoc.h"
#include "ProjectView.h"
#include "Architect.h"
#include "Client.h"
#include "Inputhousedetails.h"
#include "Changepassword.h"
#include "Logon.h"
#include "Splash1.h"
#include "DibBmp.h"
#include "imagectrl.h"
#include "FileName.h"
#include <mmsystem.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CProjectView

IMPLEMENT_DYNCREATE(CProjectView, CFormView)

BEGIN_MESSAGE_MAP(CProjectView, CFormView)
      //{{AFX_MSG_MAP(CProjectView)
      ON_COMMAND(ID_VIEW_ARCHITECTDETAILS, OnViewArchitectdetails)
      ON_COMMAND(ID_VIEW_CLIENTDETAILS, OnViewClientdetails)
      ON_COMMAND(ID_DAILYTASKS_ADD_ARCHITECT, OnDailytasksAddArchitect)
      ON_COMMAND(ID_DAILYTASKS_ADD_CLIENT, OnDailytasksAddClient)
      ON_COMMAND(ID_DAILYTASKS_CHANGEPASSWORD, OnDailytasksChangepassword)
      ON_COMMAND(ID_DAILYTASKS_EDIT_ARCHITECTDETAILS, OnDailytasksEditArchitectdetails)
      ON_COMMAND(ID_DAILYTASKS_EDIT_CLIENTDETAILS, OnDailytasksEditClientdetails)
      ON_COMMAND(ID_TOOLS_GENERATEVRMLFILE, OnToolsGeneratevrmlfile)
      ON_COMMAND(ID_TOOLS_VIEWWALKTHROUGH, OnToolsViewwalkthrough)
      ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
      ON_WM_PAINT()
      ON_COMMAND(ID_FILE_PRINTDRAWING, OnFilePrintdrawing)
      //}}AFX_MSG_MAP
      // Standard printing commands
      ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
      ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
      ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CProjectView construction/destruction

CProjectView::CProjectView() : CFormView(CProjectView::IDD)
{
      // TODO: add construction code here
      pDisplayBitmap = NULL;
      
      RECT tRect;
      tRect.top = 200;
      tRect.left = 200;
      tRect.right = 200;
      tRect.bottom = 200;

      pImageCtrl = ::new CImageCtrl(NULL,100,50);      //create image control object

}

CProjectView::~CProjectView()
{
}

BOOL CProjectView::PreCreateWindow(CREATESTRUCT& cs)
{
      // TODO: Modify the Window class or styles here by modifying
      //  the CREATESTRUCT cs
      
      return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CProjectView drawing

void CProjectView::OnDraw(CDC* pDC)
{
      CProjectDoc* pDoc = GetDocument();
      ASSERT_VALID(pDoc);
}

/////////////////////////////////////////////////////////////////////////////
// CProjectView printing

BOOL CProjectView::OnPreparePrinting(CPrintInfo* pInfo)
{
      // default preparation
      return DoPreparePrinting(pInfo);
}

void CProjectView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
      // TODO: add extra initialization before printing
}

void CProjectView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
      // TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CProjectView diagnostics

#ifdef _DEBUG
void CProjectView::AssertValid() const
{
      CView::AssertValid();
}

void CProjectView::Dump(CDumpContext& dc) const
{
      CView::Dump(dc);
}

CProjectDoc* CProjectView::GetDocument() // non-debug version is inline
{
      ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CProjectDoc)));
      return (CProjectDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CProjectView message handlers

void CProjectView::OnViewArchitectdetails()
{
      CArchitect Arch;
      Arch.DoModal();
      
}

void CProjectView::OnViewClientdetails()
{
      CClient Client;
      Client.DoModal();
}

void CProjectView::OnDailytasksAddArchitect()
{
      CArchitect Arch;
      Arch.DoModal();
}

void CProjectView::OnDailytasksAddClient()
{
      CClient Client;
      Client.DoModal();
}

void CProjectView::OnDailytasksChangepassword()
{
      CChangepassword pass;
      pass.DoModal();
}

void CProjectView::OnDailytasksEditArchitectdetails()
{
      CArchitect Arch;
      Arch.DoModal();
}


void CProjectView::OnDailytasksEditClientdetails()
{
      CClient Client;
      Client.DoModal();
}



void CProjectView::OnToolsGeneratevrmlfile()
{
      
      CFileName filen;
      filen.DoModal();
            
}


void CProjectView::OnToolsViewwalkthrough()
{
      CString filename;
      
      CFileDialog dial(TRUE,NULL,NULL,NULL,"World Files (*.wrl)|*.wrl|All Files (*.*)|*.*||",NULL);      //construct a file dialog
      
      dial.m_ofn.lpstrInitialDir = "C:\\Project\\Output\\";
      dial.m_ofn.lpstrTitle = "Open a World to view...";
      
      int nResponse = dial.DoModal();
      
      if( nResponse == IDOK )      //begin modal loop
      {
      
            filename = dial.GetPathName();
            //Home
            PlaySound("launch.wav",NULL,SND_FILENAME);
            CSplashWnd::ShowSplashScreen(this);
            
            HINSTANCE h = ShellExecute(NULL, "open", filename, "NULL", NULL, SW_MAXIMIZE);

      //College
      //HINSTANCE h = ShellExecute(NULL, "open", "f:\\college\\Ground Floor.wrl", NULL, NULL, SW_SHOWNORMAL);
      }
      
}


//This is needed for placing the form in the SDI view (IDD_DRAWING

void CProjectView::DoDataExchange(CDataExchange* pDX)
{
      CFormView::DoDataExchange(pDX);
      //{{AFX_DATA_MAP(CProjectView)
            // NOTE: the ClassWizard will add DDX and DDV calls here
      //}}AFX_DATA_MAP
}


void CProjectView::OnFileOpen() //Open the File Dialog so the user can display the bitmap
{
      CString filen;
      CFileDialog dlg(TRUE,NULL,NULL,NULL,"Bitmap Files (*.bmp)|*.bmp|All Files (*.*)|*.*||",NULL);      //construct a file dialog
      
      dlg.m_ofn.lpstrInitialDir = "c:\\project\\plans\\";
      dlg.m_ofn.lpstrTitle = "Open a Drawing...";

      int nResponse = dlg.DoModal();
      
      if( nResponse == IDOK )      //begin modal loop
      {
            filen = dlg.GetFileName();
            CFile* pFile;                  //Create File Pointer
            pFile = new CFile;            //Create File Object

            pFile->Open( dlg.GetFileName(), CFile::modeRead);

            if (pDisplayBitmap)            //pDisplay Pointer Holds A Valid Pointer
            {
                  ::delete pDisplayBitmap;
            }

            pDisplayBitmap = ::new CDibBmp;      //Create CDibBmp Object Using Global New Operator      
            
            if(pDisplayBitmap->Load(pFile))
            {
                  pImageCtrl->SetImage(pDisplayBitmap,30,30);
                  Invalidate();
                  CProjectDoc* pDoc = GetDocument();
                  ASSERT_VALID(pDoc);
                  AfxGetApp()->m_pMainWnd->SetWindowText("VR Site Development - " + filen);


            }
            else
            {
                  ::delete pDisplayBitmap;
                  pDisplayBitmap = NULL;
            }
            
            pFile->Close();
      }

}


void CProjectView::OnPaint()
{
      CPaintDC dc(this); // device context for painting
      
      CProjectDoc* pDoc = GetDocument();
      ASSERT_VALID(pDoc);
      
      if (pDisplayBitmap != NULL)
      {
            pImageCtrl->Draw(&dc);            
      }

      
      UpdateData(false);
}


void CProjectView::OnFilePrintdrawing()
{
      
      
}


void CProjectView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{
      // TODO: Add your specialized code here and/or call the base class
      CFormView::OnPrint(pDC, pInfo);
}





0
Comment
Question by:Rayzoro
[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
5 Comments
 
LVL 3

Expert Comment

by:mnewton022700
ID: 2692052
Have you tried calling on draw on the image from within your OnDraw method?
ie. Try adding the following code to CProjectView::OnDraw

if (pDisplayBitmap != NULL)
{
    pImageCtrl->Draw(pDC);
}

0
 
LVL 1

Expert Comment

by:elston_sa
ID: 2692745
OnPrint(CDC* dc, CPrintInfo* pInfo)
{
int xoffset=dc->GetDeviceCaps(PHYSICALOFFSETX);
int yoffset=dc->GetDeviceCaps(PHYSICALOFFSETY);

int tpagesize=dc->GetDeviceCaps(VERTRES);
int tpagewidth=dc->GetDeviceCaps(HORZRES);
float scaley=0;      
float scalex=0;
scalex=((float)(tpagewidth-(2*xoffset)))/((float)ImageWidth);
scaley=((float)(tpagesize-(2*yoffset)))/((float)ImageHeight);
scalex<scaley?scaley=scalex:scalex=scaley; //maintain aspect ratio

CRect srcrect(0,0,(int)(ImageWidth*scalex),(int)(ImageHeight*scaley));
srcrect.OffsetRect((int)(xoffset),(int)yoffset);
StretchDIBits(dc->m_hDC,srcrect.left,srcrect.top,srcrect.Width(),srcrect.Height(),
0,0,ImageWidth,ImageHeight,RawImageBuffer,Bmi,DIB_RGB_COLORS,SRCCOPY) ;
// Bmi is BITMAPINFO HEADER
// RawImageBuffer IS raw image data buffer
}
0
 

Expert Comment

by:Yozzer
ID: 2695802
How do i define the Raw Image Buffer or what is it?


0
 

Author Comment

by:Rayzoro
ID: 2696370
What is the raw image data buffer or how do i define it
0
 
LVL 1

Accepted Solution

by:
elston_sa earned 100 total points
ID: 2699105
Raw Image data Buffer contains the Image information, like for a DIB file it is The structure of the file is BITMAPINFO + Image Bits.

byte *lpbi=data read from the DIB ( BITMAPINFO + Image Bits) ;

lpbmi = (LPBITMAPINFO)lpbi;

int nColors = lpbmi->bmiHeader.biClrUsed;


BYTE *RawImageBuffer = ((BYTE*)lpbi) + sizeof(BITMAPINFOHEADER) + nColors * sizeof(RGBQUAD);


Alternately u can use the CImage Library. It is available under the MFC section at www.visualc.com 
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

734 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