Erstellung eines Bitmaps und Darstellung des Bitmaps in Dialog

rainer82

Grünschnabel
ich lade eine Binaere Datei folgendermassen:

Code:
bool CImageHandler::BuildImageMatrix(std::vector<CString> fList,CString srcpath)
{
 std::vector<CString>::iterator fIterator;
 CString fName;
 CStringA anssrcpath(srcpath);
 FILE* fPointer=NULL;
 size_t result;
 int count=0,i=372;
 unsigned __int8 buffer[BUFFERLENGTH];
 
 for(fIterator=fList.begin(); fIterator!=fList.end(); fIterator++)
  {
    fName=*fIterator;
    if(fName.Right(3)=="ptd")
  {
 CStringA ansfName(fName);
 errno_t err;
 //load file in binary mode
 err=fopen_s(&fPointer,anssrcpath+ansfName,"rb");
 if(fPointer==NULL)
  {
    MessageBox(/*NULL,*/_T("File: ")+srcpath+fName+_T(" is invalid!"),_T("Error"),MB_ICONERROR|MB_OK);
    return false;
  }				
 // copy the file into the buffer:
 fgetc(fPointer);
 result=fread(buffer,sizeof(unsigned __int8),BUFFERLENGTH,fPointer);
 // terminate
 fclose(fPointer);
 if(!CreateBitmapFromArray(buffer))
  {
    free(buffer);
    return false;
  }
}
		}
 return true;
}

Dann mach ich Folgendes:
Code:
bool CImageHandler::CreateBitmapFromArray(unsigned __int8 buf[])
{
 CPaintDC dc(AfxGetMainWnd()); // device context for painting
	
 DWORD dwBits= (DWORD)buf;

 BITMAPINFO bmi;
 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
 bmi.bmiHeader.biWidth = 4;
 bmi.bmiHeader.biHeight = 4;
 bmi.bmiHeader.biPlanes = 1;
 bmi.bmiHeader.biBitCount = 32;
 bmi.bmiHeader.biCompression = BI_RGB;
 bmi.bmiHeader.biSizeImage = 0;
 bmi.bmiHeader.biXPelsPerMeter = 0;
 bmi.bmiHeader.biYPelsPerMeter = 0;
 bmi.bmiHeader.biClrUsed = 0;
 bmi.bmiHeader.biClrImportant = 0;

 CBitmap bitmap;
 bitmap.CreateCompatibleBitmap(&dc,4,4);
 ::SetDIBits(dc.m_hDC,bitmap,0,4,&dwBits,&bmi,DIB_RGB_COLORS);
 CDC dcMemory;
 dcMemory.CreateCompatibleDC(&dc);
 CBitmap * pOldBitmap = dcMemory.SelectObject(&bitmap);
 dc.BitBlt(0, 0, 4, 4, &dcMemory, 0, 0, SRCCOPY);
 dcMemory.SelectObject(pOldBitmap);

return true;
}

Die Klasse Imagehandler ist abgeleitet von CDialog.

Wie stelle ich nun die Bitmapdatei auf meinem Dialog dar?
Gibt es da einfachere, fuer Anfaenger wie mich, verstaendlichere Wege?
Fuer allgemeine, konstruktive Kritik was den Code angeht waehre ich auch dankbar .

Gruss Christian
 
Zuletzt bearbeitet:
Hi ich hab grad nicht viel zeit und hab mir deinen Code nicht angeschaut, aber will dennoch helfen ;)
Deine Frage ist in einem MFC Dialog ein Bitmap zu zeichnen?

Dann nimm die folgende Klasse und erzeuge dir eine PictureBox und nutze diese Klasse:

PictureBox.h
Code:
#pragma once

class PictureBox : public CStatic
{	
	DECLARE_DYNAMIC(PictureBox)

public:
	PictureBox(void);
	virtual ~PictureBox(void);
	void SetBitmap(CString strBitmap);      
protected:    
	DECLARE_MESSAGE_MAP()    
	void ShowBitmap(CPaintDC *pDC);       
	CString m_sBitmap;    
	CBitmap m_bmpBitmap;    
	BITMAP bm;   

public:    
	afx_msg void OnPaint();
};

PictureBox.cpp
Code:
#include "StdAfx.h"
#include "PictureBox.h"

IMPLEMENT_DYNAMIC(PictureBox, CStatic)   

PictureBox::PictureBox()   
{   
	m_sBitmap = "";
}      

PictureBox::~PictureBox()   
{   
}         

BEGIN_MESSAGE_MAP(PictureBox, CStatic)    
	ON_WM_PAINT()   
END_MESSAGE_MAP()            

// PictureBox message handlers      
void PictureBox::ShowBitmap(CPaintDC *pdc)    
{    
	//Create a device context to load the bitmap into    
	CDC dcMem;    
	dcMem.CreateCompatibleDC(pdc);       
	//Get the Display area available    
	CRect lRect;    
	GetClientRect(lRect);    
	lRect.NormalizeRect();       
	//select the bitmap into compatible device context    
	CBitmap* pOldBitmap = (CBitmap*)dcMem.SelectObject(&m_bmpBitmap);    
	//m_bmpBitmap.SetBitmapDimension(lRect.Width(),lRect.Height());       
	
	//copy & resize the window to the dialog window    
	pdc->StretchBlt(0,0,lRect.Width(),lRect.Height(),&dcMem,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);   
} 

void PictureBox::OnPaint()   
{    
	CPaintDC dc(this); 
	// device context for painting       
	RECT rect;    
	GetClientRect(&rect);    
	dc.FillSolidRect(&rect, RGB(255,255,255));       
	if(m_sBitmap!="")     
		ShowBitmap(&dc);   
}   

void PictureBox::SetBitmap(CString strBitmap)   
{    
	m_sBitmap = strBitmap;       
	HBITMAP hBitmap = (HBITMAP) ::LoadImage(AfxGetInstanceHandle(),    
		m_sBitmap, IMAGE_BITMAP, 0, 0,     
		LR_LOADFROMFILE | LR_CREATEDIBSECTION);       
	// Do we have a valid handle for the loaded image?    
	if (hBitmap)    
	{     
		// Delete the current bitmap     
		if (m_bmpBitmap.DeleteObject())        
			m_bmpBitmap.Detach(); 
		// If there was a bitmap, detach it     
		// Attach the currently loaded bitmap to the bitmap object     
		m_bmpBitmap.Attach(hBitmap);    
	}    m_bmpBitmap.GetBitmap(&bm);  
	//Get Bitmap Structure    
	Invalidate();
}

Viel Spass damit ;)
Wenn du fragen hast, dann frag einfach.

Liebe grüße
RuFF
 
Zurück