0

I have converted an image(.tif image) to a byte array and saved in the DB. I am now retrieving that byte array from the DB and want to convert to an image again, but this byte array I am converting back to an image, is not producing the same. As a test (as below), I am only using the image and not reading from the DB, for testing purposes.

The initial convert from Image to byte array:

//This is the function I am using:
public static byte[] ImageToByteArray(Image image)
        {
            using (var ms = new MemoryStream())
            {
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Tiff);
                return ms.ToArray();
            }
        }

//Converting to byte array:
var tifImage = Image.FromFile(file.ToString());
var imageContent = ImageToByteArray(tifImage);

Now to try and convert back to an Image, I am doing the following:

var ms = new MemoryStream(imageContent);
var test1 = Image.FromStream(ms);

But it seems the results are not the same. I have a "Splitting" function that splits the pages within the tiff, and the one returns 8 pages(bitmaps) and the other just 1.

I dont know much about the above, so need a little help in filling in the knowledge gaps, please :)

Thanks for any the help!

AxleWack
  • 1,801
  • 1
  • 19
  • 51
  • https://stackoverflow.com/a/16958128/7772646 You could look at this answer for the converting and see if it makes any difference – BrightHammer Feb 20 '20 at 16:11
  • Thanks for the response BrightHammer - This seems to be Image to `Bytes[]`, my issue is from `Bytes[]` to `Image` that is not the same :( – AxleWack Feb 20 '20 at 16:25
  • Axle, I think the problem is occurring when you convert it to bytes not when you recreate the image from the bytes. – BrightHammer Feb 20 '20 at 16:46
  • Anything is a possibility, I will have a look and get back to you. – AxleWack Feb 20 '20 at 18:45
  • I gave the solution a try by replacing my current `ImageToByteArray`, but the result is still incorrect when converting `Byte[]` back to Image :( Im sure I have to be missing something small, but all the research I do shows that I am doing the right things :( – AxleWack Feb 21 '20 at 08:10

1 Answers1

1

I found a solution that ended up working. It seems that when the initial ImageToByteArray was being done, it was only doing the "1st page" and not all 8. So I used the following code to convert the whole tiff image:

var tiffArray = File.ReadAllBytes(file); //The `file` is the actual `.tiff` file

I then used the following to convert back to an image(The response is a byte[] returned from our API):

using (MemoryStream ms = new MemoryStream(response))
            {
                ms.Position = 0;
                Image returnImage = Image.FromStream(ms);

                var splitImages = ImageHelper.Split(returnImage);//This is to split the pages within the tiff
            }

I read that for the above to work(and I tested it), anything you do to the byte[]you are converting back to an image, must be done within the using, as anything after the using means the image is disposed.

AxleWack
  • 1,801
  • 1
  • 19
  • 51