3

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?

Chris Mantle
  • 6,595
  • 3
  • 34
  • 48
Jan Solo
  • 183
  • 1
  • 8
  • 19

2 Answers2

5

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

fuchs777
  • 993
  • 1
  • 14
  • 30
  • 1
    Hi, I tried to do as you advised, however when I am doing "rotateImage.Save(memoryStream, rotateImage.RawFormat" I get an exception saying "An exception of type 'System.ArugmentNullException' happened in System.Drawing.dll mais has not been managed by the user code. The Value cannot be null". However, I checked my variables and none of them are null. Do you have any idea why I get this exception ? (I'm sorry if the exception is not well written, I translated it from french) – NicolasR Jan 17 '17 at 09:10
  • @NicolasR sorry for the late reply... as stated the code is untested but in the past I had problems with the Image class and specific files (e.g. a corrupt jpeg file). If your problem is still relevant you might want to ask a new question and include any additional information that could be related. – fuchs777 Aug 21 '17 at 08:22
  • 2
    @NicolasR, I found that I had to set the image type: System.Drawing.Imaging.ImageFormat.Jpeg. eg. rotateImage.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg); – Ads Mar 11 '19 at 00:25
  • @Ads that would work for jpeg only... usually the RawFormat should hold the correct type... there are other ways to detect image formats, e.g. magic numbers or something like https://stackoverflow.com/a/5209604/1328536 – fuchs777 Oct 21 '19 at 13:36
0

It's tested. I dont know about Image.Save() method though. There may be issues with quality of the new image (depending on image format of the input image)

    public static Image GetImageFromDB(byte[] tab)
    {
         if (tab == null) return null;

        try
        {
            MemoryStream ms = new MemoryStream(tab);
            if (ms != null)
            {
                Image im = Image.FromStream(ms, true);
                // or: Image.FromFile(imagepath);
                im.RotateFlip(RotateFlipType.Rotate90FlipNone);
                ms.Dispose();

                im.Save(savedImagePath));
                return im;
            }
        }
        catch (Exception)
        {
        }

        return null;
    }
Arie
  • 5,251
  • 2
  • 33
  • 54