Upload Datagrid Image to Image

Asked

Viewed 237 times

0

Variable x is associated with an image (blob field) that I loaded into a Datagrid from a Mysql database.

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

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

  • x is the type byte[]?

  • I suppose so!

1 answer

1


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

// 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;
  • This line throws an exception: var mStream = new Memorystream(bytes); Additional information: The intermediate memory cannot be null.

  • Then likely the top line returned null. selectedRecord.Row.Itemarray[2] must be of another type.

  • And how can I verify this? We have some command that allows me to see the type returned by the above command?

  • You can try var x= selectedRecord.Row.Itemarray[2]; x.Gettype(). Tostring(). Or put a breakpoint, and hover the cursor over x, which should show the type of x.

  • Error my ... was selectedRecord.Row.Itemarray[1]. Had wrong index. Works perfectly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.