1

I have a task to extract and save image from database to local folder using C#. I succeed to save when there is only a single image, but failed when there are multiple images in a single column. I don't have the code that save this multiple images, so I don't know how they save that multiple images in that database.

Is there anyone who have experience in this? I have search everywhere how to save or extract multiple images from a single column, but there is no answer yet.

There is a column that tells how many images are stored in the Image column.

enter image description here

This is my code that succeed save single image, but for multiple image i don't know how to do with that blob data. I tried to save with '.rar'/'.zip' but it won't open.

FileStream FS1 = new FileStream("image.png", FileMode.Create);

Migrate.DataMigrasi item = ListDataMigrasi.Where(x => x.CustNo == "00000000001").FirstOrDefault();

byte[] blob = (byte[])item.Image;
FS1.Write(blob, 0, blob.Length);
FS1.Close();
FS1 = null;
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • @Aybe I'm guessing that the viewer is just showing that as the start of a very large hex sequence, as it really doesn't make sense to have. The `Image` field is probably a `Blob`. In addition to storing the number of images, the OP needs a way to identify how many bytes are for each image, like prepending an Int64 to each that tells it how many of the next bytes belong to this image, so the pattern is [Int64][Image][Int64][Image]... – Ron Beyer Feb 26 '18 at 03:44
  • 2
    By the way, it is not the best idea to store images in a database, it would be better to store them as files and then just store the references in the database. Images can make your database grow very large and slow it down significantly. – Ron Beyer Feb 26 '18 at 03:46
  • @RonBeyer That was pure speculation, I agree with you that this design is flawed from the start :) – aybe Feb 26 '18 at 03:48
  • Yeah, i agree with you about saving image in database is not recommended. If i check in other row that has more than 1 image, there is only single '0x' – Farifiyanto Feb 26 '18 at 03:56
  • Well extract it to local folder with "tiff" extension then click and see if windows can open it I guess. – Evk Feb 26 '18 at 04:45
  • Possible duplicate of https://stackoverflow.com/questions/55869/determine-file-type-of-an-image . – mjwills Feb 26 '18 at 04:52
  • @Evk, ok i'll try it first – Farifiyanto Feb 26 '18 at 13:54
  • 1
    @mjwills nope, but i can use that code to detect file type. Yep it is tiff file. – Farifiyanto Feb 26 '18 at 13:54
  • Strange that my (I think useful) comment about it being tiff file somehow got deleted... – Evk Feb 26 '18 at 15:11
  • @Evk OMG, yeah my comment was deleted too – Farifiyanto Feb 26 '18 at 18:39
  • OK, so until now i can save the single tif image with success. But with multipage tiff i still failed to show it in windows photo viewer. Windows photo viewer show how many image, but didn't show the image. But i can open it with gimp. Any other ideas how to extract this into a single tiff? I get an error "parameter is not valid" using code that i googled. – Farifiyanto Mar 01 '18 at 03:34

0 Answers0