Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: graphics VB.NET
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?
 
Thanks
Posted 15-Mar-13 12:27pm
Edited 15-Mar-13 12:32pm
v4
Comments
Sergey Alexandrovich Kryukov at 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.
—SA
Sergey Alexandrovich Kryukov at 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.
—SA
Rate this: bad
good
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:
http://msdn.microsoft.com/en-us/library/598t492a.aspx[^].
 
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 vb.net 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?
 
—SA
  Permalink  
v2
Rate this: bad
good
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)
    Else
        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.
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 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.
—SA
lewax00 at 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 at 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?
—SA
lewax00 at 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 at 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,
—SA
Rixterz123 at 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 at 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 at 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
0 OriginalGriff 365
1 Sergey Alexandrovich Kryukov 319
2 CPallini 275
3 DamithSL 214
4 Maciej Los 185
0 OriginalGriff 5,455
1 DamithSL 4,457
2 Maciej Los 3,885
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,115


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 18 Mar 2013
Copyright © CodeProject, 1999-2014
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