none
WinForms Button.Image is simply ignored RRS feed

  • Question

  • Hello everybody,

    I want to add an image to a normal WinForms Button such as:

    btn.Image = {some non-null bitmap object};
    btn.ImageAlign = ContentAlignment.MiddleCenter;
    btn.TextAlign = ContentAlignment.MiddleCenter;
    btn.TextImageRelation = TextImageRelation.ImageBeforeText;
    btn.FlatStyle = FlatStyle.Flat;
    

    Regardless what I tried, the image simply does not get drawn in the button, only the text gets drawn. Why please?


    Best Regards, Stefan Falk

    Tuesday, August 14, 2018 5:38 PM

Answers

  • Hello Wendy,

    Found it! It was, of course, my own fault. The framework behind all this set the FlatStyle property of simply everything to System - leading to the UserPaint style be removed, the button drawn by the OS, and thus the image to never be drawn. This stemmed from the Windows XP timeframe, and I removed it. Images get rendered now.

    Thank you for your testing and hints!


    Best Regards, Stefan Falk


    • Edited by Stefan Falk Wednesday, August 15, 2018 6:29 PM Found my own bug
    • Marked as answer by Stefan Falk Thursday, August 16, 2018 8:37 AM
    Wednesday, August 15, 2018 5:47 PM

All replies

  • Hi Stefan Falk,

    Thank you for posting here.

    Based on my search, I could set the image of button with your code. Please check the bitmap object which you set in the btn.Image property..

     private void btnLoad_Click(object sender, EventArgs e)
            {
                btnLoad.Image = new Bitmap("1.jpg");
                btnLoad.Text = "Load";
                btnLoad.ImageAlign = ContentAlignment.MiddleCenter;
                btnLoad.TextAlign = ContentAlignment.MiddleCenter;
                btnLoad.TextImageRelation = TextImageRelation.ImageBeforeText;
                btnLoad.FlatStyle = FlatStyle.Flat;
            }


    Or you could simply set the background image property.

    btnLoad.BackgroundImage = Image.FromFile("1.jpg");

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 15, 2018 7:17 AM
    Moderator
  • Hello Wendy,

    Found it! It was, of course, my own fault. The framework behind all this set the FlatStyle property of simply everything to System - leading to the UserPaint style be removed, the button drawn by the OS, and thus the image to never be drawn. This stemmed from the Windows XP timeframe, and I removed it. Images get rendered now.

    Thank you for your testing and hints!


    Best Regards, Stefan Falk


    • Edited by Stefan Falk Wednesday, August 15, 2018 6:29 PM Found my own bug
    • Marked as answer by Stefan Falk Thursday, August 16, 2018 8:37 AM
    Wednesday, August 15, 2018 5:47 PM