Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 331
  • Last Modified:

Printing a DIB

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
Rayzoro
Asked:
Rayzoro
1 Solution
 
mnewton022700Commented:
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
 
elston_saCommented:
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
 
YozzerCommented:
How do i define the Raw Image Buffer or what is it?


0
 
RayzoroAuthor Commented:
What is the raw image data buffer or how do i define it
0
 
elston_saCommented:
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

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now