简体   繁体   中英

Windows form C# Bitmap Image

I am getting this error when I try to put a image in a Bitmap on Windows Form ...

class PlayerPlane
    {
        private Bitmap playerPlaneImg { get; set; }
        private Graphics graphics { get; set; }
        private int playerLife { get; set; }
        private int playerSpeed { get; set; }

    public PlayerPlane()
    {
        playerPlaneImg = new Bitmap("Back.bmp"); //ERROR HERE ?!
        graphics.DrawImage(playerPlaneImg, 60, 10);
    }

}

The image is located in my project: http://i.imgur.com/ZOEZFFA.png ....

According to MSDN :

The file name and path can be relative to the application or an absolute path. Use this constructor to open images with the following file formats: BMP, GIF, EXIF, JPG, PNG and TIFF. For more information about supported formats, see Types of Bitmaps. The file remains locked until the Bitmap is disposed.

You could either place the absolute path of your image or the relative path of your image based on the executable part of your program.

From Bitmap Constructor (String)

Parameters
filename
Type: System.String
The bitmap file name and path.

I think you can try with full path of image. Or try like;

playerPlaneImg = new Bitmap("~/Back.bmp");

change your back image an copy output directory option to copy always or copy if newer then you can use relative path. for example: http://i.imgur.com/rp4Bd2d.png

using System.Drawing;

namespace ConsoleApplication11
{
    class Program
    {
        static void Main(string[] args)
        {
            var bmp = new Bitmap("Bitmap1.bmp");
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM