Home > Guides > Programming > .NET and Windows Programming

Toggle Open Guide Table of ContentsGuide Contents

Close Table of ContentsGuide Contents

Close Table of Contents

Loading and Saving Bitmap Images

Last updated Sep 9, 2004.

The Bitmap constructor allows you to create a new Bitmap object instance from a file, from a stream, or from another Image object. In addition, you can create a Bitmap by calling the Image.FromFile method. If you create a Bitmap from a file, the file remains locked until you dispose of the Bitmap object. Similarly, if you create a Bitmap from a stream, you must keep the stream open for the lifetime of the Bitmap object.

The inherited Save method allows you to save a Bitmap to a file or to a stream. Be careful, though, not to attempt to save a Bitmap to the stream from which it was loaded. Doing so could damage the stream and corrupt the data. In addition, if you try to save a bitmap to the file from which it was loaded, the runtime will throw an exception. The code samples below illustrate different ways to load and save bitmap images.

[C#]

// load a bitmap from a file
Bitmap bmp = new Bitmap("image.jpg");

// load a bitmap from a Web response stream
WebRequest req = WebRequest.Create("http://www.mischel.com/diary/2002/charlie9.jpg");
WebResponse resp = req.GetResponse();
using (Stream s = resp.GetResponseStream())
{
    using (Bitmap bmp = new Bitmap(s))
    {
        // do something with the bitmap
    }
}

// save a bitmap to a file
Bitmap bmp = new Bitmap("image.jpg");
try
{
    bmp.Save("newimage.jpg");
}
finally
{
    bmp.Dispose();
}

// save a bitmap to a stream
Bitmap bmp = new Bitmap("image.jpg");
try
{
    using (FileStream fs = new FileStream("image2.jpg", FileMode.Create, FileAccess.Write))
    {
        bmp.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
    }
}
finally
{
    bmp.Dispose();
}

[Visual Basic]

' Load a bitmap from a file
Dim bmp As New Bitmap("image.jpg")

' load a bitmap from a Web response stream
Dim req As WebRequest = WebRequest.Create("http://www.mischel.com/diary/2002/charlie9.jpg")
Dim resp As WebResponse = req.GetResponse()
Dim s As Stream = resp.GetResponseStream()
Try
    Dim bmp As New Bitmap(s)
    Try
        ' do something with the bitmap
    Finally
        bmp.Dispose()
    End Try
Finally
    s.Close()
End Try

' Save a bitmap to a file
Dim bmp As New Bitmap("image.jpg")
Try
    bmp.Save("newimage.gif", System.Drawing.Imaging.ImageFormat.Gif)
Finally
    bmp.Dispose()
End Try

' save a bitmap to a stream
Dim bmp As New Bitmap("image.jpg")
Try
    Dim fs As New FileStream("image2.jpg", FileMode.Create, FileAccess.Write)
    Try
        bmp.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
    Finally
        fs.Close()
    End Try
Finally
    bmp.Dispose()
End Try

The Image class implements the IDisposable interface, meaning that it allocates unmanaged system resources that should be released when you're done using the object. That's why I've wrapped all use of Bitmap object instances in using or try/finally blocks.

Note also that you can change image formats easily by simply saving the image and specifying a different ImageFormat. You could use this method to quickly convert a directory full of images from one format to another.

Discussions

Copies of the array?
Posted Dec 23, 2008 03:40 PM by luige21
1 Replies
Hi
Posted Dec 5, 2008 05:10 AM by ajay2000bhushan
2 Replies
You have no clue.
Posted Jun 10, 2008 03:28 PM by theinternetmaster
1 Replies

Make a New Comment

You must log in in order to post a comment.

Related Resources

Jim Mischel"Highly unlikely" does not mean "impossible"
By Jim MischelJuly 18, 2009 No Comments

One of my programs crashed the other day in a very unexpected place.  A call to System.Threading.ConcurrentQueue.TryDequeue (from the Parallel Extensions to .NET) resulted in an OverflowException being thrown.  Investigation revealed a pretty serious bug in the System.Random constructor.

It's Here; Put Away Your Pre-Conceptions on What an OS Must Be: Part II
By John TraenkenschuhMay 24, 2009 No Comments

In the last blog in this series, Traenk relates his first experiences with computers and with coding.  But now, some years have passed. . .

It's Here; Put Away Your Pre-Conceptions on What an OS Must Be: Part I
By John TraenkenschuhMay 24, 2009 No Comments

Traenk relates his past experience with Operating Systems that goes back 25 years, ok, more than that but he ain't tellin'

See More Blogs

Informit Network