C# Programming > Drawing GDI+

How to Load Image
From File System in C#

Bitmap and Image

Pictures and images are handled by C# with one of two objects, Image or Bitmap. Image objects are usually directed towards user interface operations, while Bitmaps are easier for image processing. It is relatively straight foward how to process images once you have a Bitmap or Image variable with the image loaded.

Any image development will require to know how to load an image in C#.

FromFile

There are two direct ways to read an image file and load it into either a Bitmap or Image. Behold the C# code:

Image myImg = Image.FromFile("path here");
Bitmap myBmp = Bitmap.FromFile("path here");

Alternatively a Bitmap object can also be loaded with:

Bitmap myBmp = new Bitmap("path here");

The code above does not work with Image objects though, so it is best to stick with FromFile.

Dialog Box

Finally, to write an application that loads an image from a file, your C# program needs a dialogbox to select files. Using the .Net OpenFileDialog is simple enough. Just apply the image-loading code to the Filename chosen by the user, so for example:

Bitmap loadedBitmap = Bitmap.FromFile(openFileDialog1.Filename);

Of course, you don't have to necessarily load an image from a file in this manner, but it is a useful thing to know.

Conclusion

Loading an image from a file is of infinite uses. Photo processing applications for example require extensive use of the code above. It can also be used if you need to handle temporary image files for any reason.

Back to C# Article List