How to run example requiring Button.bmp?

Nicholas Piazza 546 Reputation points
2023-07-23T21:48:36.9833333+00:00

I'm trying to run the Windows Desktop example for constructor Bitmap(Type, String) as shown in Microsoft documentation https://learn.microsoft.com/en-us/dotnet/api/system.drawing.bitmap.-ctor?view=windowsdesktop-8.0#system-drawing-bitmap-ctor(system-type-system-string). When I run it in Visual Studio 2022, I get the exception shown below.

ButtonBmpError

My code is shown below.

namespace BitmapCtorOL8;

public partial class FormBMCtorOL8 : Form
{
    public string path   = @"c:\users\...documents\filescache\outputs\";

    public FormBMCtorOL8 ()
    {
        InitializeComponent ();
    }

    private void OnPaintMainForm (object sender, PaintEventArgs e)
    {
        ConstructFromResourceSaveAsGif (e);
    }
    private void ConstructFromResourceSaveAsGif (PaintEventArgs e)
    {

        // Construct a bitmap from the button image resource.
        Bitmap bmp1 = new Bitmap (typeof (Button), "Button.bmp");

        // Save the image as a GIF.
        bmp1.Save (path + "button.gif",
                   System.Drawing.Imaging.ImageFormat.Gif);

        // Construct a new image from the GIF file.
        Bitmap bmp2 = new Bitmap (path + "button.gif");

        // Draw the two images.
        e.Graphics.DrawImage (bmp1, new Point (10, 10));
        e.Graphics.DrawImage (bmp2, new Point (10, 40));

        // Dispose of the image files.
        bmp1.Dispose ();
        bmp2.Dispose ();
    }
}

How do I get the application find Button.bmp?  I can create a Button.bmp, but where do I store it?
Developer technologies | .NET | Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 48,711 Reputation points Microsoft External Staff
    2023-07-24T03:07:47.4633333+00:00

    Hi,@Nicholas Piazza. Welcome Microsoft Q&A. To make the code work, you'll need to add the "Button.bmp" image as a resource in your project. Here's how you could add the "Button.bmp" image as a resource in your project:

    Add the image as a resource in your project:

    • In Visual Studio, open your project and go to the Solution Explorer.
    • Right-click on your project name and select "Properties."
    • In the properties window, go to the "Resources" tab.
    • Click on the "Add Resource" dropdown and choose "Add Existing File..."
    • Browse to the location where you saved the "Button.bmp" image, select it, and click "Open."

    User's image

    User's image

    Access the image resource in your code:

    • Once you've added the "Button.bmp" image as a resource, it will be accessible in your code as a Bitmap object. You can use the Properties.Resources class to access the resource and create the Bitmap.
       private void ConstructFromResourceSaveAsGif(PaintEventArgs e)
            {
             
               // Bitmap bmp1 = new Bitmap(typeof(Button), "Button.bmp");
                Bitmap bmp1 = Properties.Resources.Button;
                
                bmp1.Save(path + "button.gif",
                           System.Drawing.Imaging.ImageFormat.Gif);
              
               Bitmap bmp2 = new Bitmap(path + "8.gif");
    
                e.Graphics.DrawImage(bmp1, new Point(10, 10));
                e.Graphics.DrawImage(bmp2, new Point(10, 40));
                
                bmp1.Dispose();
               bmp2.Dispose();
    
            }
    
    

    If the response is helpful, please click "Accept Answer" and upvote it.

    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.


Your answer

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