Upload Image from DataGrid to Image

0

The variable x is associated with an image (blob field) that I uploaded to a Datagrid from a MySQL database.

DataRowView selectedRecord = (DataRowView)dataGridImagem.SelectedItem;
var x= selectedRecord.Row.ItemArray[2];

How can I now proceed to load the image into an Image control?

    
asked by anonymous 14.12.2016 / 22:02

1 answer

1

The blob field is probably represented as byte[] in C #. Assuming this, you can create the image in memory with object BitmapImage and then assign it to the Source of it.

// assumindo que ItemArray[2] é um array de bytes, faça um cast
var bytes = selectedRecord.Row.ItemArray[2] as byte[];

// crie uma memory-stream com os bytes vindo do banco de dados
var mStream = new MemoryStream(bytes);

//cria e manipula o objeto do tipo BitmapImage
var image = new BitmapImage();
image.BeginInit();
image.StreamSource = mStream;
image.EndInit();

// atribui para a propriedade Source do controle Image o objeto criado
imageControl.Source = image;
    
16.12.2016 / 11:25