Click here to Skip to main content
13,549,129 members
Rate this:
Please Sign up or sign in to vote.
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?

Posted 15-Mar-13 11:27am
Updated 15-Mar-13 11:32am
SAKryukov 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.
SAKryukov 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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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?

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.
SAKryukov 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.
SAKryukov 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.
SAKryukov 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,
Rixterz123 17-Mar-13 12:29pm
the problem is that i want my form to remain the same, except one image changes.
The reason i am not using pictureboxes is because the rectangle behind the image has a gradient, and the background of the changing image looks weird against it, so basically i am trying to make an icon on a form.
lewax00 18-Mar-13 16:18pm
Right, but unfortunately, as far as I'm aware, the form does not track what steps you took to get the current visual state, so you cannot simple undo a step, you just clear the custom drawn part and redraw it with your new image. But you did mention something that gave me an idea...have you considered a PictureBox with a transparent background?
Rixterz123 22-Mar-13 13:39pm
no, a transparent background will change it's BG to that of the form.
I have a gradiented rectangle.
So i threw it away and kept a dumb old plain one. Mission complete.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.180515.1 | Last Updated 18 Mar 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100