Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Bitmap to Memory Stream and Vice Versa

0.00/5 (No votes)
8 Oct 2013 1  
Must required functions for any Image manipulation task.

Introduction

Whenever we encounter any Image intensive application, there are lot of scenario where we require some sort of runtime image manipulation. I came across number of such scenarios, when I was working on an Image Editing Application for Windows Phone 8. At bottom level its very important to get Bitmap-To-Memory Stream and Memory Stream-to-Bitmap conversions.

Background  

For Image Editor application, I extensively used Nokia Imaging SDK, which is very sophisticated library and provides tons of extensions/functions for Image intensive task, Please give it a try as you can use it number of different scenarios. It runs on native code and often very efficient. 

Using the code 

Required Namespace

Imports System.Windows.Media.Imaging 

Code for converting BitmapImage to Stream

Private Function GetStreamFromBitmap(bitmapImage As BitmapImage) As IO.Stream
    Try
        Dim writeBMP As New WriteableBitmap(bitmapImage)
        Dim memStream As New IO.MemoryStream
        writeBMP.SaveJpeg(memStream, bitmapImage.PixelWidth, bitmapImage.PixelHeight, 0, 100)
        return memStream
    Catch ex As Exception
        MesssageBox.Show("Error Converting Bitmap To Stream")
    End Try
End Function

Code for converting Stream To Bitmap. 

Private Function GetBitmapFromStream(MemStream As IO.Stream) As BitmapImage
    Try
        Dim bitmapImage New BitmapImage
        bitmapImage.SetSource(MemStream)
        Return bitmapImage 
    Catch ex As Exception
         MesssageBox.Show("Error Converting Stream To Bitmap")
    End Try
End Function 

Points of Interest

Now as I can use this function for number of different purpose to fulfill one's image editing scenarios.

History 

  • 11:59 AM 10/8/2013 First edit.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here