Click here to Skip to main content
15,746,652 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hey guys

I have done this:

Dim ShouldPaintImage2 as boolean=true
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
       e.Graphics.DrawImage(My.Resources.image1, 20, 20)
if ShouldPaintImage2=true then
[remove old graphic]
 e.Graphics.DrawImage(My.Resources.image2, 20, 20)
End Sub

Is it possible to remove only the graphic of image1 so i can change the image to image2?

Updated 15-Mar-13 11:32am
Sergey Alexandrovich Kryukov 15-Mar-13 18:33pm    
Do you mean that you want to trigger from showing one image to another, during runtime? I would assume that, otherwise your would simply draw just one image...
"In reverse" is irrelevant, because after each invalidation, all the rendering is redone from the very beginning, in whatever order. I explained it in my answer.
Sergey Alexandrovich Kryukov 18-Mar-13 14:04pm    
You should not post non-solution as solution. I would say, don't answer your own question, to avoid abuse reports. It would be even find if not heavy abuse by some members.
And what if you still needed the gradient? It would not solve the problem. And the problem is very simple.

Yes of course. You need to understand that OnPaint, which you are correctly using, is called on the Windows WM_PAINT messages, which your UI receives on each invalidation:[^].

Please see my past answers:
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
How to speed up my application?[^].

That said, all you need is this: Draw not a fixed image, but a variable image/bitmap reference. Make a reference to Image an instance field of, say, your form class; let's say, its name is MyImage. Then your line with DrawImage should be changes to e.Graphics.DrawImage(MyImage ...).

Now, the procedure of "switching the image" will look like

MyImage = ' different reference, ether image1 or image2...
self.Invalidate(); ' this will eventually cause re-painting
' here, "self" is your control, of the class where you override OnPaint;
' it could be your form or whatever control it is...

Are you getting the idea?

Share this answer
I think you're going about this the wrong way, wouldn't it make more sense to just not draw it in the first place if that's the case? i.e.:
Dim ShouldPaintImage2 as boolean=true
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
    If ShouldPaintImage2=true Then
        e.Graphics.DrawImage(My.Resources.image2, 20, 20)
        e.Graphics.DrawImage(My.Resources.image1, 20, 20)
    End If
End Sub

(Sorry if this isn't perfectly correct syntax, my VB.Net is very rusty)

Otherwise, to my knowledge you can't erase a specific object, if your real code is more complicated, clear the drawing, then redraw the whole thing, using image2 instead of image1.
Share this answer
Sergey Alexandrovich Kryukov 15-Mar-13 18:29pm    
Sorry, it won't work, as you are missing the main point. The whole idea is to cause the switch from one image to another. The mechanism is Control.Invalidate.
Did you ever do Forms animation? It is based on invalidation...
I explain it all in my answer.
lewax00 15-Mar-13 18:31pm    
Where do you see in his question that his issue is getting the function to trigger? That's not at all what he asked.
Sergey Alexandrovich Kryukov 15-Mar-13 18:32pm    
Please understand: this "if" would make no sense at all if you never trigger from image1 to image2. Of course it is required. Do you see my point?
lewax00 15-Mar-13 18:39pm    
If I asked you how to add an event handler to a button would the answer require instructions on how to click a button to make the event fire? Of course it's required, but it's not what's being asked. Nothing in his question suggests he's having difficulty getting to that point, he's asking what do do once he's already there. If he also has difficulty getting it to repaint, it's his responsibility to ask for further clarification.
Sergey Alexandrovich Kryukov 15-Mar-13 18:53pm    
You have your point, but that's rather formal argument. Anyway, I'll take your point and will up-vote the answer to 4, as I think you should have at least explained a bit more, because I think OP really needed some motion/switch and would be confused without it...

Thank you,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900