Click here to Skip to main content
15,887,875 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Bitmap to Memory Stream and Vice Versa

Rate me:
Please Sign up or sign in to vote.
4.67/5 (3 votes)
8 Oct 2013CPOL 51.4K   4   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

VB.NET
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. 

VB.NET
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionWriteableBitmap has not SaveJpeg method Pin
Ejrr108526-Jun-18 5:00
Ejrr108526-Jun-18 5:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.