vendredi 27 février 2015

Converting a BitmapImage byte array to an Image byte array (WinRT -> WinForms)

I have 2 solutions in this project. A windows forms solution and a Windows 8.1 Tablet project. This is what's supposed to happen:



  1. User takes a picture using the tablet and uploads it to a MySQL database in the form of a byte array.

  2. User starts up the windows forms application and loads the byte array from the MySQL database.

  3. It then converts the byte array to an image which is placed in a picturebox.


I'm storing the byte array like this:



CameraCaptureUI dialog = new CameraCaptureUI();
dialog.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
Size aspectRatio = new Size(16, 9);
dialog.PhotoSettings.CroppedAspectRatio = aspectRatio;

StorageFile file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo);
if (file != null)
{
BitmapImage bitmapImage = new BitmapImage();
using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read))
{
var readStream = fileStream.AsStreamForRead();
byte[] pixeBuffer = new byte[readStream.Length];
await readStream.ReadAsync(pixeBuffer, 0, pixeBuffer.Length);

}


The byte array is successfully stored in my database.


I'm running into a problem when converting the byte array into a WinForms Image. This is my code:



using (var ms = new MemoryStream(bytes))
{
Image i = Image.FromStream(ms);
return i;
}


This gives me an invalid parameter exception.


I'm guessing it's something with the image format? I'm really new to streams though so I have no idea.


Any help is welcome!


PS: I know storing in the SQL database runs perfectly since I can store and load images perfectly using the WinForms application only.


Aucun commentaire:

Enregistrer un commentaire