Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotate image (byte array) in datatable

I have a datatable set as itemsource for a datagrid (datagrid showing several columns of the datatable). All columns except one have text, the last column holds an image as a byte array. The picture-column is bound to an image control, which shows the image from the selected line in the datagrid. The images the datatable holds can be collected from a disk-location or a database. Eventually, they will all be saved in the database.

I have a button, which should rotate the shown image 90° clockwise, and save it again (as byte array) in the datatable on the exact same row (and column).

I tried about a kazillion ways to do this, but no luck whatsoever. Can someone please help me rotating and saving this picture?

like image 655
Jan Solo Avatar asked May 21 '26 23:05

Jan Solo


1 Answers

untested but should do the trick (might necessary be to load the image to a bitmap)

using (var memoryStream = new MemoryStream(byteArray))
{
    var rotateImage = Image.FromStream(memoryStream);
    rotateImage.RotateFlip(RotateFlipType.Rotate90FlipNone);
    rotateImage.Save(memoryStream, rotateImage.RawFormat);
    byteArray = memoryStream.ToArray();
}

edit: for some reason I had forgotten to save the image back to the stream... fixed that

like image 190
fuchs777 Avatar answered May 24 '26 13:05

fuchs777



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!