Picturebox background color of another picturebox

Tomy97

New member
Joined
Mar 31, 2020
Messages
2
Programming Experience
3-5
Hello,
1585692628107.jpeg

I need to have a color from another picturebox in the top picturebox and not a background form.
White line is picturebox and Car have set transparent background.
 
I don't really know what you're saying. If you display an Image with transparency in a PictureBox then the PictureBox will show through. If that's happening for you then you must be doing something funky. You need to provide a FULL and CLEAR explanation of the problem. Assume that too many words is better than not enough.
 
I'm sorry, I don't know how to describe this situation. That's why I added a picture that I thought would be clear. But I have a gray background form, white picturebox like a bar and another picturebox with car. The car is cropped so it has no background. I need to pass over another picturebox to show a second picturebox and no form background.
 
There's no genuine transparency in Windows Forms. Transparency is faked by having the control's parent drawn onto the control's background. That's why you see the form in the background of the PictureBox. What you would need to do is, rather than having the car image in a PictureBox, you would need to draw it using GDI+. That way, you can draw it on both the form and the other PictureBox. Here's some example code I wrote some years ago that draws a line over every control on a form and on the form itself:
C#:
private void Form1_Load(object sender, EventArgs e)
{
    Control ctl = this.GetNextControl(this, true);
 
    while (ctl != null)
    {
        ctl.Paint += new PaintEventHandler(Form1_Paint);
        ctl = this.GetNextControl(ctl, true);
    }
}
 
private void Form1_Paint(object sender, PaintEventArgs e)
{
    // Get the points relative to the form.
    Point start = new Point(0, 0);
    Point end = new Point(this.ClientSize);
 
    Control paintee = (Control)sender;
 
    // Translate the points relative to the control being painted.
    start = paintee.PointToClient(this.PointToScreen(start));
    end = paintee.PointToClient(this.PointToScreen(end));
 
    e.Graphics.DrawLine(Pens.Black, start, end);
}
You need to implement the same principle. That means using a single method to handle the Paint event of the form and all controls that you need to draw over. In that event handler, you work out where you need to draw in form coordinates, then map that to screen coordinates and then to client coordinates for the control you're drawing on. If that control is the form then you end up back where you started, but it's simplest to just use the same code in every case. Finally, you'll call DrawImage rather than DrawLine.

You ought to do a bit of experimentation with GDI+ first, if you're not already familiar. To move the car, instead of simply moving a PictureBox control, you'll need to invalidate the area of the form currently containing the image and the area you want to move the image to. That will cause a Paint event to be raised and your drawing code executed.
 
Back
Top Bottom