System.Drawing Color.Transparent example not working

Nicholas Piazza 541 Reputation points
2024-02-20T22:33:25.0566667+00:00

In the Microsoft documentation for the System.Drawing.Color.Transparent property (https://learn.microsoft.com/en-us/dotnet/api/system.drawing.color.transparent?view=net-8.0), the given example does not seem to work. Here is my coded implementation of that example:


namespace ColorTransparent;
public partial class FormCT : Form
{
    public FormCT ()
    {
        InitializeComponent ();
        UseTransparentProperty ();
    }
    private void UseTransparentProperty ()
    {
        // Set up the PictureBox to display the entire image, and
        // to cover the entire client area.
        pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
        pictureBox1.Dock = DockStyle.Fill;
        try
        {
            // Set the Image property of the PictureBox to an image retrieved
            // from the file system.
            pictureBox1.Image =
                Image.FromFile (@"C:\Users\Admin\OneDrive\Pictures\Family\Aunt Lee Alone\Aunt Lee.png");
            // Set the Parent property of Button1 and Button2 to the 
            // PictureBox.
            button1.Parent = pictureBox1;
            button2.Parent = pictureBox1;
            // Set the Color property of both buttons to transparent. 
            // With this setting the buttons assume the color of their
            // parent.
            button1.BackColor = Color.Transparent;
            button2.BackColor = Color.Transparent;
        }
        catch (FileNotFoundException)
        {
            MessageBox.Show ("There was an error." +
                "Make sure the image file path is valid.");
        }
    } // method UseTransparentProperty

Both buttons remain with a white background instead of the PictureBox image that covers the Form. Shown below is a snapshot of the displayed form. Any ideas for fixing this problem? Thanks.

Developer technologies | .NET | Other
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2024-02-21T04:25:04.3033333+00:00

    Hi @Nicholas Piazza , Welcome to Microsoft Q&A,

    Please do not upload images with people in them, as such images contain files containing private information.

    There is indeed a problem with the tutorial you mentioned. In WinForms, the button control does not support transparent background colors.

    Best Regards, Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".  Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.