locked
Extract Multi Image from a field in SQL RRS feed

  • Question

  • User1862817645 posted

    I have a task to extract multiple image from a single field in SQL. It has another field that show how many images in Image field. 

    Design Table

    Any idea how to extract and save this image to local folder?
    I have a code to save a single image, but failed to save multi image into multi file.

    Saturday, February 24, 2018 2:19 PM

Answers

  • User753101303 posted

    So it seems those TIFF files are using something special. Try maybe the links I posted earlier to inspect the TIFF header and maybe understand which particular feature is used or to see if another TIFF library can handle this format variation.

    Edit: seems quite common to see software not handling this tag. According to a post in French it seems to be used by a Wang imaging system to add text annotations. Check maybe if someone knows about this software.

    Edit2 : http://alliancegroup.co.uk/imaging-for-windows.html and scroll down for the evaluation software which may allow to open those files. Seems you have to check which TIFF software is currently in use and maybe plan to upgrade those TIFF data.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 1, 2018 8:18 AM

All replies

  • User753101303 posted

    Hi,

    More likely it uses some kind of format (possibly custom?) to stuff multiple images into a single column. You would know first to know which format is used.

    Saturday, February 24, 2018 4:31 PM
  • User1862817645 posted

    Hi,

    More likely it uses some kind of format (possibly custom?) to stuff multiple images into a single column. You would know first to know which format is used.

    Do you know some of format it used?

    So, i have to know how they save that multi image to DB first? 
    Is there anyone experienced inserting multi image in a single field?

    Saturday, February 24, 2018 4:34 PM
  • User753101303 posted

    According to https://en.wikipedia.org/wiki/List_of_file_signatures it seems to be TIFF files ? It might then give something such as (assuming a single frame size):

                using (var ms = new System.IO.MemoryStream(bytes))
                {
                    using (var b = new Bitmap(ms))
                    {
                        foreach (var dim in b.FrameDimensionsList)
                        {
                            var frameDim = new FrameDimension(dim);
                            for (int i = 0; i < b.GetFrameCount(frameDim); i++)
                            {
                                b.SelectActiveFrame(frameDim, i);
                                b.Save(@"c:\windows\temp\" + i + ".png", System.Drawing.Imaging.ImageFormat.Png);
                            }
                        }
                    }
                }
            }
      

    Monday, February 26, 2018 9:53 AM
  • User1862817645 posted

    It is exactly  a tiff file.
    I've got an error "A generic error occurred in GDI+." in this code.

    using (var b = new Bitmap(ms))

    Edit :

    I Succeed using that code when i insert new data and multipage tiff from my local folder. But when i tried to use that code with existing data, it show error like i said.

    Tuesday, February 27, 2018 7:32 AM
  • User1400794712 posted

    Hi farifiyanto,

    I Succeed using that code when i insert new data and multipage tiff from my local folder. But when i tried to use that code with existing data, it show error like i said.

    What do you mean using the code with existing data?

    I've tried to convert the same byte to image and save it in folder. It seems works well. Do you mean that? Could you please share your code with us? Because I'm not clear about 'use that code with existing data'.

    Best Regards,

    Daisy

    Tuesday, February 27, 2018 9:16 AM
  • User753101303 posted

    It's a bit unclear if you mean it works when reading new files from the db or when reading files directly and never from the db. try ms.Position=0 and double check your db reading code.

    My first move would be likely to dump data to a file using https://msdn.microsoft.com/en-us/library/system.io.file.writeallbytes(v=vs.110).aspx and see if the resulting file seems to be valid.

    Tuesday, February 27, 2018 9:17 AM
  • User1862817645 posted

    Hi farifiyanto,

    farifiyanto

    I Succeed using that code when i insert new data and multipage tiff from my local folder. But when i tried to use that code with existing data, it show error like i said.

    What do you mean using the code with existing data?

    I've tried to convert the same byte to image and save it in folder. It seems works well. Do you mean that? Could you please share your code with us? Because I'm not clear about 'use that code with existing data'.

    Best Regards,

    Daisy

    So in my existing table, i insert new data. So there are my new data and existing data. when i tried to use this code

     using (var ms = new System.IO.MemoryStream(blob))
                    {
                        using (var b = new Bitmap(ms))
                        {
                            foreach (var dim in b.FrameDimensionsList)
                            {
                                var frameDim = new FrameDimension(dim);
                                for (int i = 0; i < b.GetFrameCount(frameDim); i++)
                                {
                                    b.SelectActiveFrame(frameDim, i);
                                    b.Save(@"D:\MyImages\" + i + ".png", System.Drawing.Imaging.ImageFormat.Png);
                                }
                            }
                        }
                    }

    It succeeded with new data that i insert manually using query insert image. But when i tried using that code to existing data, it show an eror like my previous reply.

    Tuesday, February 27, 2018 11:06 AM
  • User753101303 posted

    I  would really try my earlier suggestion ie just dump file data to a .tiff file and see if Windows Photo Viewer can show that. If seems fine I would then try to process this file with the Bitmap class. Do that as well to new TIFF data as a sanity check and to make sure it is turned back to the same exact file content (using for example https://ss64.com/nt/fc.html)

    I'm trying to make 100% sure this is an issue with old TIFF data (rather than with reading back data from your db).

    If it turns out to be the case I would perhaps try something such as https://bitmiracle.com/libtiff/ or maybe https://www.awaresystems.be/imaging/tiff/astifftagviewer.html to understand which kind of difference I have between working and non workiing TIFF files.

    Tuesday, February 27, 2018 12:15 PM
  • User1400794712 posted

    Hi farifiyanto,

    There are many standard formats for saving a bitmap to a file. GDI+ supports the following file formats: BMP, GIF, EXIF, JPG, PNG and TIFF.

    For more information, see the introduction to Bitmap Class.

    Bitmap Class inherits from Image Class.

    There may be some special file formats, we can try to use the methods of Image Class to handle the data.

    FromStream(Stream)

    Creates an Image from the specified data stream.

    using (var ms = new System.IO.MemoryStream(bytes))
    {
        using (var b = Image.FromStream(ms))
        {
            foreach (var dim in b.FrameDimensionsList)
            {
                var frameDim = new FrameDimension(dim);
                for (int i = 0; i < b.GetFrameCount(frameDim); i++)
                {
                    b.SelectActiveFrame(frameDim, i);
                    b.Save(@"d:\" + i + ".png", System.Drawing.Imaging.ImageFormat.Png);
                }
            }
        }
    }

    Best Regards,

    Daisy

    Wednesday, February 28, 2018 7:45 AM
  • User1862817645 posted

    I tried using this code

    using (var ms = new System.IO.MemoryStream(bytes))
    {
        using (var b = Image.FromStream(ms))
        {
            foreach (var dim in b.FrameDimensionsList)
            {
                var frameDim = new FrameDimension(dim);
                for (int i = 0; i < b.GetFrameCount(frameDim); i++)
                {
                    b.SelectActiveFrame(frameDim, i);
                    b.Save(@"d:\" + i + ".png", System.Drawing.Imaging.ImageFormat.Png);
                }
            }
        }
    }

    But still got an error "Parameter is not valid". 
    Here what i've accomplish until now. 

    1.  I tried to save this image file using this code

                    FileStream FS1 = new FileStream("image_me.tiff", FileMode.Create);
    
                    byte[] blob = (byte[])item.Image;
                  
                    FS1.Write(blob, 0, blob.Length);
                    FS1.Close();
                    FS1 = null;

    2. If the image is single image, it open successfully on windows photo viewer. But if it is multipage imaget tiff file. it failed like this but show the total images.

    Error Photo Viewer

    3.  I tried to install gimp, and it can show the 12 image. But with error like this

    Eror gimp

    4. When i open pages as Images and Select All Images, it show all images but with error like this.

    Other eror gimp

    Any other suggestion?

    Thursday, March 1, 2018 4:42 AM
  • User753101303 posted

    So it seems those TIFF files are using something special. Try maybe the links I posted earlier to inspect the TIFF header and maybe understand which particular feature is used or to see if another TIFF library can handle this format variation.

    Edit: seems quite common to see software not handling this tag. According to a post in French it seems to be used by a Wang imaging system to add text annotations. Check maybe if someone knows about this software.

    Edit2 : http://alliancegroup.co.uk/imaging-for-windows.html and scroll down for the evaluation software which may allow to open those files. Seems you have to check which TIFF software is currently in use and maybe plan to upgrade those TIFF data.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 1, 2018 8:18 AM
  • User1862817645 posted

    So it seems those TIFF files are using something special. Try maybe the links I posted earlier to inspect the TIFF header and maybe understand which particular feature is used or to see if another TIFF library can handle this format variation.

    Edit: seems quite common to see software not handling this tag. According to a post in French it seems to be used by a Wang imaging system to add text annotations. Check maybe if someone knows about this software.

    Edit2 : http://alliancegroup.co.uk/imaging-for-windows.html and scroll down for the evaluation software which may allow to open those files. Seems you have to check which TIFF software is currently in use and maybe plan to upgrade those TIFF data.



    Yup, this is a Wang Tiff data. Some "paid" software may open it and we could use .Net library from that.

    Thank you for every suggestionsmile

    Tuesday, March 6, 2018 1:51 AM