6

I have a byte array of jp2, how can I convert that to JPG file? Thank you

Thank all the answers. I made some differences and nearly succeeded. Here is how I do it:

using (var reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        String id = (String)reader["ID"];
                        blob = (byte[])reader["Data"];

                        using (MemoryStream ms = new MemoryStream(blob))
                        {
                            FIBITMAP dib = FreeImage.LoadFromStream(ms);                            
                            if (dib.IsNull)
                            {
                                continue;
                            }                           
                            string jpgName = getJpgName(id);
                            FreeImage.Save(FREE_IMAGE_FORMAT.FIF_JPEG, dib, jpgName, FREE_IMAGE_SAVE_FLAGS.JPEG_QUALITYNORMAL);
                        }
                    }
                }

I read byte[] from database. Now another problem arises; there exists memory leak! Could someone pick it out?

Nikson Kanti Paul
  • 3,394
  • 1
  • 35
  • 51
ChanDon
  • 391
  • 1
  • 5
  • 15

2 Answers2

3

We don't have anything built in .Net to do this but, You can use FreeImage which is a free library that can do this.

Here is an Example on doing this.

FIBITMAP dib = FreeImage.LoadEx("test.jp2");
//save the image out to disk    
FreeImage.Save(FREE_IMAGE_FORMAT.FIF_JPEG, dib, "test.jpg", FREE_IMAGE_SAVE_FLAGS.JPEG_QUALITYNORMAL);
//or even turn it into a normal Bitmap for later use
Bitmap bitmap = FreeImage.GetBitmap(dib);

For converting from a stream of bytes u can try this:

byte[] myByte = new byte[10];
MemoryStream theMemStream = new MemoryStream();
theMemStream.Write(myByte, 0, myByte.Length);
FreeImageBitmap fbm = FreeImageBitmap.FromStream(theMemStream);
fbm.Save("text.jpg",FREE_IMAGE_STREAM.FIF_JPEG);
Community
  • 1
  • 1
Shekhar_Pro
  • 18,056
  • 9
  • 55
  • 79
  • I does support, see [here](http://freeimage.sourceforge.net/features.html) on the right side panel – Shekhar_Pro Feb 16 '11 at 08:21
  • That's where I was looking. I think you are confusing JPEG2000 compression with the jp2 file format. jp2 fles use JPEG2000 compression, but that doesn't mean that the library can read the jp2 format. – Ed S. Feb 16 '11 at 08:33
  • 1
    @Ed S, see this SO question: http://stackoverflow.com/questions/590471/jpeg-2000-support-in-c-net. The answer has been accepted - that seems to imply that jp2 file format is also supported. – VinayC Feb 16 '11 at 08:37
  • Ok, I was just pointing out that it's not in the list. I haven't used the library myself. It does however appear that the OP of that thread may be confusing terms as well - *"It seems that .NET can't open JP2 (Jpeg 2000) files"* – Ed S. Feb 16 '11 at 08:45
  • 1
    @Ed S: you are wrong .. according to [**Wikipedia**](http://en.wikipedia.org/wiki/JPEG_2000) jp2 is the Standerd FileExtention for JPG2000 files – Shekhar_Pro Feb 16 '11 at 08:54
  • These are the Valid FileExtentions for a JPG 2000 File ".jp2, .j2k, .jpf, .jpx, .jpm, .mj2" – Shekhar_Pro Feb 16 '11 at 08:55
  • @Shekhar_Pro: The example converts a jp2 file to jpg, but I need to do with a byte array of jp2. Is there such an example? – ChanDon Feb 16 '11 at 11:20
  • @Shekhar_Pro: You still seem to not understand the distinction between a file format and a compression type, but ok, I don't really care. I was just saying that I didn't see the **file format** .jp2 in the list of supported formats. I also said that I've never used the library and I never said that it wouldn't work. – Ed S. Feb 16 '11 at 17:40
  • @Shekhar_Pro: No, it doesn't work, an exception occurs that shows "unable to upload bitmap" when executing `FreeImageBitmap fbm = FreeImageBitmap.FromStream(theMemStream);` – ChanDon Feb 17 '11 at 01:47
  • @Shekhar_Pro: I don't know if it's the problem of the byte[], but I've tried some jp2 files, and the result is some works fine while others show nothing. – ChanDon Feb 17 '11 at 05:47
  • @Shekhar_Pro: I read the data from database, which is originally blob. To give an example, the blob data may be `64 65 66 61 75 6c 74 0A 7B 0A 20 20 20 20 73 74`, can you turn it into jpg? – ChanDon Feb 18 '11 at 06:37
0

Download FreeImageAPI from nuget package. Add the reference of FreeImageAPI.dll in your project. Check your package folder in that you will have FreeImage.dll, keep that dll in project bin folder. Download FreeImageAPI from nuget package. Add the reference of FreeImageAPI.dll in your project. Check your package folder in that you will have FreeImage.dll, keep that dll in project bin folder.

    public ActionResult FileUpload(HttpPostedFileBase file)
    {
      Stream str =  file.InputStream;
      MemoryStream theMemStream = new MemoryStream();
      FIBITMAP dib = FreeImage.LoadFromStream(str);
      FreeImage.SaveToStream(dib,theMemStream,FREE_IMAGE_FORMAT.FIF_JPEG);
      return RedirectToAction("Index", theMemStream);
    }

Change theMemStream to byte[], base64string etc.