stop Refreshing of images drawn by setpixel() and saving it how to do?

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

stop Refreshing of images drawn by setpixel() and saving it how to do?

This code draw a rectange of green colour. This image gets refreshed everytime whenever it is resized.
As far as i could knoew is WM_PAINT is ahandler which call ONDRAW() through ONPAINT().. Correct me if I am wrong.

Now How do i avoid this refreshing of images. also How will I save this bitmap image generated using setpixel(). Please any one could suggest me and help me out.

FYI I am a learner in visual C++ and I am learning it by solving some of my own problems and examples framed by myself.

Re: stop Refreshing of images drawn by setpixel() and saving it how to do?

Originally Posted by Skizmo

Why are you setting it pixel by pixel ? With FillSolidRect you can do the same, and it's MUCH faster.

I know that but my purpose is difeerent. Say i am plotting based on a data a 2-d contour plot I have to go pixel wise since i have data at each point ie pixel. At that moment i cannot avoid it. Can you or any please suggest any solution or idea

Re: stop Refreshing of images drawn by setpixel() and saving it how to do?

You should read about flicker-free drawing using double buffering.
In short, you plot your data on the off-screen DC and use BitBlt() in your OnDraw() to paint it on screen.
You would also need to handle WM_ERASEBACKGROUND message to preventing that erase background.

Re: stop Refreshing of images drawn by setpixel() and saving it how to do?

Originally Posted by tarkes

... Now How do i avoid this refreshing of images. ... As far as i could knoew is WM_PAINT is ahandler

The whole purpose of the WM_PAINT handler is to refresh the image. So why do you want to avoid it?

Originally Posted by tarkes

... also How will I save this bitmap image ...

This is an entirely different question. Solve your first question first, since it doesn't make sense to save something that isn't what you want. After you solve your first question, then perhaps Google for the CImage class, which might help you.

Re: stop Refreshing of images drawn by setpixel() and saving it how to do?

This is an entirely different question. Solve your first question first

Actually... if he has the answer to this question, he can solve the problem of the first question. My guess about why he wants to avoid the refresh is that the SetPixel function is horribly slow. So if he knows how to write it to an image and use that as a cached image, the problem of the slow SetPixel will be gone

Re: stop Refreshing of images drawn by setpixel() and saving it how to do?

The idea is this: Let a CImage object be a member of your class. Draw your 2-d contour into the CImage using CImage::SetPixel. This needs to be done only once. Then, in your OnDraw handler for WM_PAINT, call CImage::BitBlt to blt the contents of the CImage bitmap into the device context provided.

Re: stop Refreshing of images drawn by setpixel() and saving it how to do?

Assuming skizmo isn't right

Windows will trigger a WM_PAINT message whenever something has happened that has invalidated any part of the window.
If you size the window (smaller or larger), then the default behaviour of Windows isi that the entire window will be marked invalid.
If some other window is overlapping your window and that window gets moved, again, that part of the window will be invalidated.
If your window has scrollbars, and the scrollbars are pressed that invalidates the window.
And finally, your own program can explicitely invalidate parts of a window via the InvalidateRect() function.

When a part of a window is invalid, a WM_PAINT will be "in the queue" (it isn't actually there).

You seem to be using MFC, so this WM_PAINT is usually handled in OnPaint, for some controls, MFC will do some preliminary work for you, and you have a somewhat simpler OnDraw() function to do your drawing stuff.

Now... All that stuff above about part of a window getting invalidated... you can override this default behaviour and prevent this invalidation. THe problem of course is that this will make your application look somewhat funny/weird, unless you also do "something" in the areas that would have been invalidated. Applications can use this behaviour to prevent flicker and get more performance by not having to redraw bits of the screen that don't need to be repainted.
It is a somewhat more advanced topic, and not something for a learning programmer, you need a substantial amount of code in place (several hundred lines of code at least) to handle all the special cases.

That said.
SetPixel is really really really slow. The solution is indeed to make a bitmap, and twiddle the bits in this bitmap, and paint the bitmap with BitBlt as it is needed. CImage is one way to to it. If you can't afford to use GDI+, the other way would be to use CreateDIDSection().

Re: stop Refreshing of images drawn by setpixel() and saving it how to do?

Originally Posted by MikeAThon

Aha, I see. I think you're right. I interpreted his mention of "save" as in a "save to disk", not a "save in memory for a quick re-draw".

Mike

There has been lot of discussion goin on these. Soory I was bit out for 2 days. I was not in a position to check the post.
My aim is:
1. Display the image without refreshment i.e. what save in cache memory and display.

2. Save into disk if it is possible so.

3. Presence of scroll bar in window if the image is large than SDI window.

I have not read the posts thourughly. Let me go through it and get some hints if possible.

Re: stop Refreshing of images drawn by setpixel() and saving it how to do?

1) This requires a "off screen bitmap" which you then blit to screen.
It avoids the whole pixel by pixel thing entirely.

2) THen I'd go with the CImage GDI+ solution posted. You can do this with the CreateDIBSection() solution also, but saving an image that's usuable/displayable in other programs is a bit more complex.

3) I would recommend you look at the CScrollView class as base class for your SDI window. This'll take care of most of the scrolling stuff for you. All you need to do is set your desired size (600x600), and you provide the OnDraw function, MFC will take care of the rest adequately enough for a starter program. Though you can improve on what it does.