button control bitmap file C#

Posted on 2007-08-03
Last Modified: 2013-12-17
I need to create a bitmap from a button control ( System.Windows.Forms.Button ) and save it into a file.  Basically, I need to capture exactly how the button is being shown.  I am using C# 2005.  Please provide a sample code.  How can I do that?  
Question by:jumanac14
    LVL 14

    Expert Comment

    Sounds little confusing. Can you explore it a bit?

    Author Comment

    public Form1()

        System.Windows.Forms.Button myButton = new Button ();
        this.Controls.Add ( myButton );
        Bitmap myButtonBitmap = new Bitmap (myButton.Width, myButton.Height, myButton.CreateGraphics ( ));
        myButtonBitmap.Save ( @"E:\myButtonBitmap.bmp" );

    What I would expect from this fragment of code is a bmp file that contains a "screen capture" of the button itself.  Instead, I'm just getting a file with a black background and nothing else.  What am I doing wrong?
    LVL 14

    Expert Comment

    If you want entire screen shot this is a one good article to look at.
    LVL 22

    Accepted Solution

    how about
    Bitmap myButtonBitmap = new Bitmap (myButton.Width, myButton.Height);
    myButton.DrawToBitmap(myButtonBitmap, myButton.Bounds);
    myButtonBitmap.Save(@"E:\myButtonBitmap.bmp" );
    LVL 22

    Expert Comment

    nm i see what you mean in paint.-- if i just use the windows viewer it basically just shows the rectangle of the button.

    Author Comment

    This solution works great! Thanks!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
    Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now