0

I'm using C#, and need to process Jpeg-XR images. However, these images are presented in form of base64 strings, and need to be converted into Bitmap objects directly. I can write it into a file and convert it, but this significantly affects my running time.

I was wondering if anyone could help me with a sample code, or a hint? (I already tried Magick.Net, but that didn't work for me, and also doesn't seem to be able to load a JXR image directly).

thanks a lot

am17
  • 1
  • 2
  • I'm sure most, if not all, image libraries can take a stream in input. After converting the base64 to a byte array, put that array in a MemoryStream then feed it to your library – Kevin Gosse Jun 13 '17 at 20:23

1 Answers1

0

JPEG XR is formerly known as HD Photo and Windows Media Photo.

You can use the class WmpBitmapDecoder in System.Windows.Media.Imaging in the WPF library to manipulate .jxr images.

This class Defines a decoder for Microsoft Windows Media Photo encoded images. The following code convert JXR file to Bmp file:

       using System.IO;
       using System.Windows.Media.Imaging;

         public class JXrLib
        {
            public static void JxrToBmp(string source, string target)
            {
                Stream imageStreamSource = new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.Read);
                WmpBitmapDecoder decoder = new WmpBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                BitmapSource bitmapSource = decoder.Frames[0];

                var encoder = new BmpBitmapEncoder(); ;
                encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
                using (var stream = new FileStream(target, FileMode.Create))
                {
                    encoder.Save(stream);
                }

            }
        }

The code is tested and running fine.

Alternative 2:

If you are interested of using Magick.Net, you can use jxrlib library in https://jxrlib.codeplex.com

Copy the file JXRDecApp.exe and JXREncApp.exe to your bin directory and read from a file on disk that has a .jxr extension. (you have to compile jxrlib using visual studio)

Code example:

        // Read first frame of jxr image
        //JXRDecApp.exe ,JXREncApp.exe should be located in the path of binaries
        using (MagickImage image = new MagickImage(@"images\myimage1.jxr"))
        {
            // Save frame as bmp
            image.Write("myimage2.bmp");

            // even , Save frame as jxr
            image.Write("myimage2.jxr");
        }
M.Hassan
  • 10,282
  • 5
  • 65
  • 84
  • Thanks. I actually found a way to read a byte array of an image into a BitmapImage object, and then convert that into Bitmap. A combination of these (my input was a base64 and didn't want to put read it from a file): https://stackoverflow.com/questions/9564174/convert-byte-array-to-image-in-wpf and https://stackoverflow.com/questions/6484357/converting-bitmapimage-to-bitmap-and-vice-versa – am17 Jun 16 '17 at 22:07