2

Hi I can’t find sample for convert MULTI PAGE tiff image to byte array.

For convert byte array to Tiff I use this method

    public static Tiff CreateTiffFromBytes(byte[] bytes)
    {
        using (var ms = new MemoryStream(bytes))
        {
            Tiff tiff = Tiff.ClientOpen("in-memory", "r", ms, new TiffStream());
            return tiff;
        }
    }

EDITED:

This method convert TIFF image with more pages to byte Array. I think in this method will be root of problem.

    //imageIn is tif image with 12 pages
    public static byte[] ImageToByteArray(System.Drawing.Image imageIn)
    {       
        using (var ms = new MemoryStream())
        {
            imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Tiff);
            return ms.ToArray();
        }
    }

   public static List<System.Drawing.Image> GetAllPages(System.Drawing.Image multiTiff)
    {
        var images = new List<System.Drawing.Image>();
        var bitmap = (Bitmap)multiTiff;
        int count = bitmap.GetFrameCount(FrameDimension.Page);

        for (int idx = 0; idx < count; idx++)
        {
            bitmap.SelectActiveFrame(FrameDimension.Page, idx);
            using (var byteStream = new MemoryStream())
            {
                bitmap.Save(byteStream, ImageFormat.Tiff);

                images.Add(System.Drawing.Image.FromStream(byteStream));
            }
        }
        return images;
    }

After conversion to byte array I lose pages. Image from byte array has only one page.

  Image src = Image.FromFile(source);
    //imagesInSource.Count (pages) is 12
    List<Image> imagesInSource = GetAllPages(src);

    byte[] imageData = ImageToByteArray(src);

    Image  des = ImageConvert.ByteArrayToImage(imageData);
    //imagesInSource.Count (pages) is 1
    List<Image> imagesInDes = GetAllPages(des);
whyme
  • 51
  • 3
  • 9
  • it looks like you've post 3 questions about the same issue. what are you trying to accomplish? what do you want to do with the byte array? – Bobrovsky Aug 21 '12 at 14:42
  • @Bobrovsky. I need do this: Convert BitMiracle.LibTiff.Classic.Tiff object to byte array sent from client on web service. On web servise side I need convert back byte array to Convert BitMiracle.LibTiff.Classic.Tiff, do something (retrieve all pages as Image from multi TIFF) and last I need convert BitMiracle.LibTiff.Classic.Tiff object to System.Drawing.Image object. Yes for me is little compilcated because image processing is not my field. Could you help me? I google it, but I little afraid memory leaks when I use LibTiff because I dont have any experience with this library. – whyme Aug 21 '12 at 17:43

1 Answers1

1

I am not sure why you can't send TIFF file to the service? The file is just bytes, after all.

And your code in the first snippet is incorrect because you dispose memory stream that is passed to Tiff object. You shouldn't do that. The Tiff object will dispose the stream itself.

EDIT:

In the third snippet you create images for each page of the System.Drawing.Image but the you convert only first produced image to byte array. You should use something like

List<byte[]> imagesBytes = new List<byte[]>();
foreach (Image img in imagesInSource)
{
    byte[] imageData = ImageToByteArray(src);
    imageBytes.Add(imageData);
}

Then you should send imagesBytes to your server and create several TIFF images from that.

Anyway, it seems like you should think more about what are you really trying to do. Because for now it unclear to me what all these conversions are for.

Bobrovsky
  • 13,789
  • 19
  • 80
  • 130
  • @Bobrovky. I think problem is in method (byte[] ImageToByteArray(System.Drawing.Image imageIn) ) which convert multi page TIFF to byte array. I add this method definiton to my question. Because if I try convert byte array which returns this method I get tiff image with one page. So what is correct way for convert System.Drawing.Image which hold multi page TIFF to byte array? – whyme Aug 21 '12 at 18:30
  • First, thank your for your response. I pass to method ImageToByteArray object type of System.Drawing.Image and I initialize this object from multi page TIF Image src = Image.FromFile(multiTIFFfile), I dont understand why reponse of method ImageToByteArray return only one page as byte array. – whyme Aug 21 '12 at 19:30
  • @ Bobrovsky: I would like to ask you a favor could you post a example how convert BitMiracle.LibTiff.Classic.Tiff object to byte array and how convert this byte array to BitMiracle.LibTiff.Classic.Tiff object. I think it will be better use BitMiracle.LibTiff then .NET types. Thank you very much – whyme Aug 21 '12 at 19:58
  • @whyme Please provide more details and ask *new* question. What have you got (file name, `System.Drawing.Image` or something else)? What do you want to do on the server? – Bobrovsky Aug 22 '12 at 05:03
  • Ok I posted new question here: http://stackoverflow.com/questions/12122976/converting-tiff-obect-from-bitmiracle-libtiff-to-net-types – whyme Aug 25 '12 at 15:33