How to serialize an image (bitmap) in bytes [] C # UWP?

0

What happens is that I have an image captured from a PC folder

//Capturar imagen
//https://docs.microsoft.com/es-es/windows/uwp/files/quickstart-using-file-and-folder-pickers

var picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".jpeg");
picker.FileTypeFilter.Add(".png");

Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
if (file == null)
      return;

After capturing it I show it in a <Image/> by means of Binding to the ViewModel from the XAML

<Image Source="{Binding CurrentImage, Mode=TwoWay}" Width="150" Height="150" HorizontalAlignment="Left"/>

and this is the code of the View Model, I believe the property to which I will make Binding:

public ImageSource CurrentImage { get { return currentImage; } set { Set(ref currentImage, value); } }
private ImageSource currentImage;

and within the method goes like this:

//Mostrar en <Image/>
//https://stackoverflow.com/questions/34214553/binding-image-source-to-page
var bitmap = new BitmapImage();
await bitmap.SetSourceAsync(await file.OpenReadAsync());
this.CurrentImage = bitmap;

Now what I need is Serialize it in bytes to save it in the database and I could not

I saw this video and it does not work for me, the Image property generates an error, it does not appear, I do not know if I have to import any libraries: link

also look at this Blog and it does not work for me, it does not let me import System.Drawing.Image : link

and I do not know how to do serialization in UWP with C #

    
asked by Wilmilcard 08.11.2018 в 16:33
source

1 answer

1

I already solve it. the code to pass to Bytes a BitmapImage in UWP is this, it leaves the other had it and this is done:

using Windows.Storage.Streams; //libreria que usaremos
byte[] imageByte = null; //parametro que guardara la conversion de la imagen

//Serializar
using (var inputStream = await file.OpenSequentialReadAsync())
{
    var readStream = inputStream.AsStreamForRead();
    var byteArray = new byte[readStream.Length];
    await readStream.ReadAsync(byteArray, 0, byteArray.Length);
    imageByte = byteArray;
}

and what I do is send imageBytes to SQL

and to bring it back we do this, taking into account that we already have the Binding of <image/> in XAML (that although above I explain how to do it I repeat it):

//este es el parametro global que hace Binding en el <image/>
public ImageSource CurrentImage { get { return currentImage; } set { Set(ref currentImage, value); } } 
private ImageSource currentImage;

//Deserializar
using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
{
    using (DataWriter writer = new DataWriter(stream.GetOutputStreamAt(0)))
    {
        writer.WriteBytes(imageByte);
        await writer.StoreAsync();
    }
    var image = new BitmapImage();
    await image.SetSourceAsync(stream);

    this.CurrentImage = image;
}

And ready the chicken, the truth is not easy to find documentation of UWP but there is done what you can

    
answered by 09.11.2018 / 15:57
source