// Load image from a file path Bitmap image = new Bitmap(@"c:\ufo.gif");
The only safe way to make sure that the image, or any file, stays with code is to embed it and load it as a resource, a named piece of data embedded in the assembly itself.
Following command line shows how to embed a file into the assembly:
csc [source file name.cs] /resource:[resource file name] csc ufo.cs /resource:UFOApplication.ufo.gif
Following codes shows how to retrieve resources from the assembly manifest:
using System; using System.IO; using System.Windows.Forms; using System.Drawing; using System.Reflection; namespace UFOApplication { public class Form1: Form { private Assembly assembly; private Bitmap image; // Constructor public Form1() { // Get current object type's assembly. this.assembly = this.GetType().Assembly; // Load the image binary stream from the assembly manifest. // Stream stream = this.assembly.GetManifestResourceStream("UFOApplication.ufo.gif"); Stream stream = this.assembly.GetManifestResourceStream(this.GetType(), "ufo.gif"); // Construct the bitmap from stream. this.image = new Bitmap(stream); this.Paint += new PaintEventHandler(Form1_Paint); } private void Form1_Paint(object sender, PaintEventArgs e) { // Draw the bitmap on the form. e.Graphics.DrawImage(this.image, 0, 0); } // The main entry point for the application. public static void Main() { Application.Run(new Form1()); } } }
The proper type of object will be needed to load the relevant type of each resource.