How to get / Base 64 String memory stream from Image.Source?

I have a dynamically created Image control that is populated via OpenFileDialog, for example:

OpenFileDialog dialog = new OpenFileDialog(); if (dialog.ShowDialog() == true) { using (FileStream stream = dialog.File.OpenRead()) { BitmapImage bmp = new BitmapImage(); bmp.SetSource(stream); myImage.Source = bmp; } } 

I want to send the image back to the server in a separate function call, like a string through a web service.

How to get / base 64 memory stream string from myImage.Source

+4
source share
2 answers

Here's an alternative that should work (without BmpBitmapEncoder). It uses a FileStream to create an array of bytes, which is then converted to a Base64 string. It is assumed that you want to do this as part of the current code.

  Byte[] bytes = new Byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); return Convert.ToBase64String(bytes); 
+10
source

Make sure you have http://imagetools.codeplex.com/

Then you can do this:

 ImageSource myStartImage; var image = ((WriteableBitmap) myStartImage).ToImage(); var encoder = new PngEncoder( false ); MemoryStream stream = new MemoryStream(); encoder.Encode( image, stream ); var myStartImageByteStream = stream.GetBuffer(); 

Then for Base64:

 string encodedData = Convert.ToBase64String(myStartImageByteStream); 
+3
source

All Articles