5 Replies - 2645 Views - Last Post: 09 October 2012 - 06:34 PM Rate Topic: -----

#1 Shtock  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 14-November 11

Trouble with image fixing using ValidateRect()

Posted 09 October 2012 - 05:11 AM

Hello everyone

I'm trying to resolve the problem with image blinking and I almost get it ,but I got some problems.

I want to redraw all the rest of the window except only the image. I'm trying to throw the Image's rectangle(i.e.coordinates) into ValidateRect and perform rendering but somehow it doesn't work

If I just overriding OnEraseBkgnd(CDC* pDC) {return false;}
It gives me a workable code,without any flickering(especially it looks great with high-definition images like 1680x1050) . But with small pictures where all the rest window is free when I begin resize it, it lefts remains(thats why I need to redraw it)
I've tried a lot of different ways and can't make it work
All the code is in OnDraw(CDC* pDC/*pDC*/) function
1)
if(oldpathName!=pathName){
	memDC.DeleteDC();
	memDC.CreateCompatibleDC(pDC);
	cImg.Destroy();
	if(cImg.Load((LPCTSTR)pathName)!=S_OK)
		AfxMessageBox(L"Error");
	memDC.SelectObject(cImg);
	pDC->BitBlt(0, 0, cImg.GetWidth(), cImg.GetHeight(), &memDC, 0, 0, SRCCOPY);
	oldpathName=pathName;erase=true;}
	else{
		CRect s(0,0,tmpImg->width,tmpImg->height);
		////InvalidateRect(s,FALSE);
		//RedrawWindow(s,NULL,RDW_INVALIDATE | RDW_UPDATENOW | RDW_ERASE);
		ValidateRect(&s);
		pDC->BitBlt(0, 0, cImg.GetWidth(), cImg.GetHeight(), &memDC, 0, 0, SRCCOPY);
		//RedrawWindow(s,NULL,RDW_VALIDATE | RDW_UPDATENOW | RDW_ERASENOW);


If I run the program,the area where the image hangs (I validated it) begins blinking when I resize the borders.With small images I almost see nothing , but when I do it with the large ones some blinks occurs. It means that the function didn't fix the image

Maybe I should carry my code from OnDraw() into OnPaint(),than (maybe) OnPaint()'d call ->WM_PAINT->and then OnEraseBkgnd() would redraw the window or smth like that? It may look stupidly but I dont see any way now.

Please gimme a clue what I should change

Attached image(s)

  • Attached Image

This post has been edited by Shtock: 09 October 2012 - 05:12 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Trouble with image fixing using ValidateRect()

#2 Shtock  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 14-November 11

Re: Trouble with image fixing using ValidateRect()

Posted 09 October 2012 - 09:08 AM

After many efforts I at last got What I need
To resolve my problem I dragged all drawing operations from OnDraw() to OnPaint()
But somehow I noticed on GPU widget that my video accelerator(i.e. videocard) sustains some load when I begin load\reload some amount of images. If I operate with resizing on MsPaint all look fine. I assume maybe I somewhere have some unnecessary overheads.
Now my code looks that way:
void CMicrofilmOfficeView::OnPaint()
{
if(tmpImg){
	CClientDC aDC(this);
    CDC* pDC =&aDC;
	if(oldpathName!=pathName){
	memDC.DeleteDC();
	memDC.CreateCompatibleDC(pDC);
	cImg.Destroy();
	if(cImg.Load((LPCTSTR)pathName)!=S_OK)
		AfxMessageBox(L"Error");
	memDC.SelectObject(cImg);
	pDC->BitBlt(0, 0, cImg.GetWidth(), cImg.GetHeight(), &memDC, 0, 0, SRCCOPY);
	oldpathName=pathName;}
	else{
		pDC->BitBlt(0, 0, cImg.GetWidth(), cImg.GetHeight(), &memDC, 0, 0, SRCCOPY);
		ValidateRect(&CRect(0,0,tmpImg->width,tmpImg->height));
	}
}

Please gimme please an advice how to maximum optimize my code /
Was This Post Helpful? 0
  • +
  • -

#3 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 773
  • View blog
  • Posts: 2,287
  • Joined: 20-March 10

Re: Trouble with image fixing using ValidateRect()

Posted 09 October 2012 - 09:15 AM

Well,

If you do have overhead in your code it's not in
what you are showing us..

and we are not with the best of intentions going
to be able to help you.

You would have to post more of your classes and program
especially image classes and resizing routines.

All the Best

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3572
  • View blog
  • Posts: 11,107
  • Joined: 05-May 12

Re: Trouble with image fixing using ValidateRect()

Posted 09 October 2012 - 09:45 AM

How is the size of tmpImg related to cImg? If tmpImg covers more area that cImg, your call to ValidateRect() passing in the dimension of tmpImg tells Windows that everything within that rectangle is good to go and needs no repainting. If that area happens to include the left over artifacts from the scrollbars, then those will remain on untouched, specially since you overrode the WM_ERASEBACKGROUND message to not repaint the background.

The best approach to dealing with flickering is double buffering. You can double buffer either the entire client area if you don't know what areas are going to be changing, or you can just double buffer the small area that needs updating.
Was This Post Helpful? 0
  • +
  • -

#5 Shtock  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 14-November 11

Re: Trouble with image fixing using ValidateRect()

Posted 09 October 2012 - 10:16 AM

View PostSkydiver, on 09 October 2012 - 09:45 AM, said:

How is the size of tmpImg related to cImg? If tmpImg covers more area that cImg, your call to ValidateRect() passing in the dimension of tmpImg tells Windows that everything within that rectangle is good to go and needs no repainting. If that area happens to include the left over artifacts from the scrollbars, then those will remain on untouched, specially since you overrode the WM_ERASEBACKGROUND message to not repaint the background.

The best approach to dealing with flickering is double buffering. You can double buffer either the entire client area if you don't know what areas are going to be changing, or you can just double buffer the small area that needs updating.


1)I dont override OnEraseBkgnd(CDC* pDC), 'cause I don't see why I'd do it if I set ValidateRect().When the window need to be redrawn OnPaint() automatically calls CWnd::OnEraseBkgnd(CDC* pDC) and redraw all the rest of the window except only the picture. (Right??)

2)tmpImg is my temp solution, 'cause I need to operate with image and I use OpenCV.My prog works as follows:
a)Load image from file->than created IplImage from the image->than it's drawn on the Form->then(stupidly way and I'll rewrite it to work with bitmap) I load image with Cimage->than all operations makes with Cimage.

3)Why you suggest use double buffering if I already use see:
memDC.SelectObject(cImg);//memDC is a global var (It seems me it's better then create it every time,am I right?)
	pDC->BitBlt(0, 0, cImg.GetWidth(), cImg.GetHeight(), &memDC, 0, 0, SRCCOPY);

Or it is smth else and I'm wrong ??

4)my question: Do I need use ValidateRect() every time I need to redraw the window? Maybe there is another way once fix it and than sometime unfix it ? (in may way unfix picture when I need to load a new image)

thanks guy...and hope you'll scribble me something

This post has been edited by Shtock: 09 October 2012 - 10:17 AM

Was This Post Helpful? 0
  • +
  • -

#6 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 773
  • View blog
  • Posts: 2,287
  • Joined: 20-March 10

Re: Trouble with image fixing using ValidateRect()

Posted 09 October 2012 - 06:34 PM

Quote

4)my question: Do I need use ValidateRect() every time I need to redraw the window? Maybe there is another way once fix it and than sometime unfix it ? (in may way unfix picture when I need to load a new image)


No,

You only need to use it once.
After you load the image and invalidate the rect when you are loading a new image.

I doubt however this is slowing you down.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1