mfc >> Displaying an edited bitmap

by In Excelsius Dago » Wed, 02 Feb 2005 00:18:56 GMT

I am currently working on a dialog application in which I want to
display a bitmap which I manipulate based on data in a file.

I can display the bitmap by putting the following code, copied from
CodeGuru, in the OnPaint function:

/////////////////////////////
HBITMAP hBmp = (HBITMAP)::LoadImage(NULL, "filename.bmp",
IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE/LR_CREATEDIBSECTION);

bmp.Attach(hBmp); // CBitmap bmp was declared as a member function in
the class view

CClientDC dc(this);
CDC bmDC;

bmDC.CreateCompatibleDC(&dc);
CBitmap *pOldbmp = bmDC.SelectObject(&bmp);
BITMAP bi;
bmp.GetBitmap(&bi);

dc.BitBlt(0,0,bi.bmWidth, bi.bmHeight, &bmDC, 0,0, SRCCOPY);

bmDC.SelectObject(pOldbmp);
/////////////////////////////////////////////////////

I would like to edit the values of the bitmap. Within the OnPaint
function, I can do so using bmp.SetBitmapBits.

But I would like to do this editing in another member function. But I'm
missing something because when I invoke the SetBitmapBits in another
function, nothing changes.

Thanks in advance,



mfc >> Displaying an edited bitmap

by Scott McPhillips [MVP] » Wed, 02 Feb 2005 07:09:14 GMT





I assume that you would like to modify the bitmap in memory, then simply
paint the stored bitmap in OnPaint. You can store the bitmap's actual
bits, and color mapping if desired, within your own memory using a DIB
section. This article provides a class that can do just about anything
with a DIB section:
http://www.codeguru.com/Cpp/G-M/bitmap/article.php/c1737/

--
Scott McPhillips [VC++ MVP]




mfc >> Displaying an edited bitmap

by In Excelsius Dago » Wed, 02 Feb 2005 23:43:42 GMT

Thanks Scott. I downloaded the class and am able to use many of the
member functions. But I am unable to use the draw function....

Draw(CDC *pDC, CPoint ptDest, BOOL bForceBackground = FALSE)

probably through inexcusable ignorance on my part. Here's a public
display of such ignorance: It seems that this function fits really well
into an MFC Document application, which supplies an OnDraw(CDC *pDC)
function. So I would just use the dc pointer supplied by MFC. I'm
making a dialog app, which uses OnPaint() instead. So I need a way to
get to the device context pointer that would make the draw function
work. And yes, I have alot to learn about device contexts.



Displaying an edited bitmap

by In Excelsius Dago » Thu, 03 Feb 2005 00:07:36 GMT

Hmm, never mind, perhaps.

I was able to make the OnPaint() member function of a dialog class to
display a bit map using the Draw(DCD *pdc,....) member function of the
class referred by to by Great Scott.

After loading the bitmap in OnInitDialog(), I went into OnPaint() and
inserted the following lines of code

CClientDC dc(this); //gets the device context that the dialog class
interfaces with

CDC *bmDC; //The draw function wants a CDC pointer, so I comply
like a slave

bmDC = &dc; //connecting to ankle bone to the shin bone, I suppose
.......

bmp.Draw(bmDC, ....) //and it works. Cool.

Off I go to manipulate the rgb values of the bitmap using the pointer
returned by GetDIBits in another member function, or at least try
to.....



Displaying an edited bitmap

by Scott McPhillips [MVP] » Thu, 03 Feb 2005 07:35:12 GMT




Great ;)

But there a some Windows painting basics that you need to know: Within
the OnPaint function it is vital that you use CPaintDC, not any other
kind of DC. CPaintDC is specialized just for OnPaint - using it tells
Windows that the window has now been painted. Without it you get an
infinite loop.

You may pass the address of the CPaintDC to the Draw function, as you
are doing above with the address of the CClientDC.

--
Scott McPhillips [VC++ MVP]



Displaying an edited bitmap

by In Excelsius Dago » Thu, 03 Feb 2005 23:56:17 GMT

Thanks for the additional tip, Great Scott.

And perhaps my conceptual error on device contexts is the cause of the
following comment. I was able to use the Load, Draw, and GetDIBits from
the DIBSectionLite......on my computer at work. When I tried to run the
same functions at home on my PC, I got an Unhandled Exception
MSVCRT.DLL 0xC0000005 Access Violation. With the help of F5, F10 and
F11, I found that the Load member function calls the SetBitMap member
function. In SetBitMap, the program has a hissy fit on a memcpy:

memcpy(m_ppvBits, lpBits, dwImageSize);

Any thoughts on this? Later today, I'll try to do it all again with the
PaintDC at home. At work, the use of the PaintDC worked fine. Cheers,
IED



Similar Threads

1. Display a bitmap from Win32, unmanaged code? - CSharp/C#

2. Display bitmap as compressed ASCII string of set bits

Dear Group.

For an application I am developing I need to display a 64 bit bitmap
as a compressed ascii string of bits.   This is to allow for the
easier understanding of the bits for the users.

For example in the bitmap if bits, 1,2,3,4,5,6, 17,18,19 and 63 are
set they would like to display a string 1-6,17-19, 63.

Is there a easy way to do this in Csharp or do I need to roll my own?
Any suggestions on the best way to do this if I need to roll my own?

Thanks

David

3. Trouble displaying bitmap - CSharp/C#

4. Displaying pixel in a bitmap as a rectangular pixel

Hi

I i am zooming part of an image ( 15x15 px for example ). What i see
are  pixels ( but kinda renderred or something like that ) i nnot
rectangular form. I need to see them as rectangular pixels - to
clearly see differences between colours.
Can anyone of you help me with that?
PK

5. displaying bitmap

6. creating a bitmap from memory and displaying it

I have an MFC app which captures individual frames from streaming video
and is supposed to display the just captured frame. The streaming video
and captured frames should be displayed in two separate static text
areas in the app's window.

I capture a frame into memory by pressing a button, and populate a
CBitmap class with the frame values by pointer iteration. I am having
problems displaying the bitmap.

How do I relate the target CDC and the static text area in the window?
The following is the code which is a combination of codes I fished out
for the net. The code resides in the onButtonClicked function. No
bitmap is displayed when I click the button.

Bitmap.SetBitmapBits(bm.bmHeight*bm.bmWidthBytes,pData);
//pData is the array containing frame values
CClientDC dc(this);
CDC dcMem;
dcMem.CreateCompatibleDC(&dc);
CBitmap *pOldBitmap = dcMem.SelectObject(&Bitmap);
BOOL bitbilt = dc.BitBlt(20, 10, bm.bmHeight, bm.bmWidth, &dcMem, 0, 0,
SRCCOPY);

//BitBlt returns 1.

7. Displaying a bitmap

8. location problem: display bitmap

Hi,
I create a CForm window (MFC) with a button (some other controls) on it. I
need to display a bitmap (from a bitmap file) at the right side of the
button.
My problem is I cannot get the correct location.

Below is my code. I believe I set the different view, which makes the pixel
calculation not work.

"GetSystemMetrics()" and "GetWindowRect()" may not work in here, but which
function should work?

thanks a lot
larry



----------------------------------------------------------------------------
	BITMAP bm, RegBm; // Windows BITMAP data structure; see Win32 help

	if (m_pdcRegMem->GetSafeHdc() == NULL) {
		CClientDC dc(this);
		OnPrepareDC(&dc); // necessary

			HBITMAP hBitmap = NULL;
			hBitmap = (HBITMAP)LoadImage(NULL, "View.bmp", IMAGE_BITMAP, 0, 0,
				LR_LOADFROMFILE | LR_CREATEDIBSECTION | LR_DEFAULTSIZE);
			m_pRegViewBmp->Attach(hBitmap);

		m_pdcRegMem->CreateCompatibleDC(&dc);
		m_pdcRegMem->SelectObject(m_pRegViewBmp);
		m_pRegViewBmp->GetObject(sizeof(RegBm), &RegBm);
		m_sizeRegView.cx = RegBm.bmWidth;
		m_sizeRegView.cy = RegBm.bmHeight;
	}
}

void ConView::OnDraw(CDC* pDC)
{
	// TODO: Add your specialized code here and/or call the base class
	// draw LOGO bitmap
	pDC->SetStretchBltMode(COLORONCOLOR);

	CRect btnRect;

	((CButton*)GetDlgItem(IDC_BTN_PROCESS))->GetWindowRect(btnRect);

	int cx = GetSystemMetrics(SM_CXSCREEN);
	int cy = GetSystemMetrics(SM_CYSCREEN);

	pDC->StretchBlt(btnRect.right, -10, cx-btnRect.right, -cy,  m_pdcRegMem,
		0, 0,  m_sizeRegView.cx, m_sizeRegView.cy, SRCCOPY);
}