Overwrite DrawImage in picturebox (VB.NET) - vb.net

I've a program that draw an image in a picturebox with DrawImage. This image, a ball (.png transparent), every second is overwritten by another ball of different color. After 3-5 second the edge of the ball becomes bad, because of overwriting.
I tried to clean the background with a FillRectangle before any overwrite, but i need to preserve the windows form background. How can i do that?

To anybody who will encounter this in the future:
g.Clear(Color.FromArgb(0,0,0,0)); //Clear the graphics with transparent as background
g.DrawImage(...); //Draw your image

Related

Unity Prevent game objects from resizing when resolution changes

I have a rectangle sprite that animates when a score is reached. Starts offscreen and then move onscreen before moving off-screen again. Works fine. Problem is when I change the resolution for different devices the sprite resizes and doesn't display as intended. The text fields inside the rectangle move outside on some of the resolutions. How can i keep the sprite at the original size regardless of resolution. I have tried Preserve Aspect, Set Native Size, moving anchors but it still resizes. I also tried using a panel.Any Ideas?

Draw with mouse and save the drawn region as separate image

Please see following picture:
background picture
The image Abc in red color is the background picture, maybe preloaded on some control, for example a picturebox control.
The black rectangle region is drawn using mouse. What I want to achieve is to copy out the drawn region as separate image.
My goal is the winform will display each mouse drawing stroke until there is a closed region being formed or enclosed. That means a closing contact point need to be tracked.
Could you advise me on the key classes that I need to look into?

Basic picturebox error

I want to add a .gif image to a picturebox.
I want user to choose image from a variety of images of different pixels.
if I use
picturebox1.image = image.fromfile("E:\ship1")
then there is no option available of imagelayout unlike backgroundimage.
because of different images pixel only half of image is displayed in my picturebox size.
I don't want to change my picturebox size because in the game that i am working on, it can cause problems.
If i use
picturebox.backgroundimage = image.fromfile("E:\ship1")
but then .gif would not show its animation.
Any suggestion for solution apart from changing size of picturebox cause that would be the last thing to do

Picture Box and Form Transparency

Maybe I am missing something, but is it the case that when you set a pictureboxes background to transparent, all it really does is set it to the same color as the forms background?
What I am trying to do is draw an animation for the benefit of this, a bouncing ball - which I paint on the form, then overlay that with a picture frame. End result should be a bouncing ball in a picture frame, I should mention that the picture frame does not have a straight edge, so it is not possible to arrange 4 picture boxes in a frame. The ball needs to vanish behind the frame to change color and then magically bounce back out.
I have tried:
1.Setting the picture box background to pink and then key out the same pink, this basically cuts away everything, including that which is behind the picture box
2.Setting the picture box to transparent, this just displays the picture box background as the same color as the forms background.
3.I have tried painting the image in a rectangle, this had the same effect as drawing it in a picture box.
I am not sure what I am doing wrong, I am wondering if there is any other ways I could try or if someone has made a custom control or library that supports transparency?
Try using a Panel with the background image set. This is because; as you said: the transparent option only takes the backcolour of the parent.
After doing some more in depth research I solved this by drawing both the images to the form using PaintEventArgs and me.paint
Making each image transparent using:
Dim TestImage as Bitmap
TestImage.MakeTransparent(<Transparency Color>)
Thank you to both of you for your replies though.
Hmm... If You want to use Picturebox and get a transparent image,
Try to set picturebox's background as your 'Ball' (probably you need an image editor).
and set picturebox transparent. It worked for me (VS Express 2010) once.

GDI+ Tearing! VB.NET

I have problem with the Graphic object. I have a loop which goes through some array and it gets the images from them and It draws them on a picture box. Every thing is fine but When I try to resize or draw another thing which is a little bit more heavy, Every thing start flashing Like when they're painting. I know it's too heavy to draw all that damn things ! but is there any way to avoid tearing?
Thanks.
Edit:
my code:
graphic.Clear(frmmain.Workspace.BackColor)
For i = 0 To mObjectsList.Count - 1
graphic.DrawImage(mObjectsList(i).oGraphic, mObjectsList(i).oX, mObjectsList(i).oY, mObjectsList(i).oWidth, mObjectsList(i).oHeight)
Next
graphic is a variable which I created it from my picturebox Graphic object
A picturebox doesn't have a Graphic object. Do not use its CreateGraphics() method. Whatever you draw through that stays on the screen for only a fraction of a second, barely making a blip. Use e.Graphics in the Paint event handler instead. That draws into the double-buffered bitmap. PictureBox always has its DoubleBuffered property set to true. That bitmap gets drawn when the Paint event completes. Which is why your objects flicker, they get overdrawn again by that bitmap.