0

I want to write an application that takes an image(jpeg, png, tiff, gif,...) as stream and convert it to jrx(jpeg xr) with lossless compression.

Thats is what i have tried so far with no useable result:

using System.Windows.Media.Imaging;
    //decode jpg
    public static BitmapSource ReadJpeg(Stream imageStreamSource)
    {
        JpegBitmapDecoder decoder = new JpegBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
        BitmapSource bitmapSource = decoder.Frames[0];
        return bitmapSource;
    }
    //encode
    public static Stream Encode(BitmapSource image)
    {
        WmpBitmapEncoder encoder = new WmpBitmapEncoder();
        MemoryStream s = new MemoryStream();
        encoder.Frames.Add(BitmapFrame.Create(image));
        encoder.Save(s);
        return s;
    }

Can someone point me in the right direction? I am hanging here for some time now.

If you need more informations please ask.

System.Drawing.Bitmap to JPEG XR is working for the given input formats but doesnt fully cover my question because the part of decoding the image is missing.

Thank you all for pointing me in the right direction! I do know now, how to proceed.

Community
  • 1
  • 1
b1ub
  • 90
  • 8
  • Does .NET even support `.jrx`? Never heard of that format and I doubt it is supported. I think you need to write code that writes out that image yourself. – Lasse V. Karlsen Jan 09 '15 at 11:34
  • What is the problem? Messages? Results? Expected Results? – DrKoch Jan 09 '15 at 11:34
  • What is "no usable result"? What _does_ happen? – CodeCaster Jan 09 '15 at 11:34
  • The WmpBitmapEncode should create an .jxr compatible image. Thats what i learned from msdn and wikipedia. – b1ub Jan 09 '15 at 11:47
  • @LasseV.Karlsen It does. It's the new name of Windows Media Photo (WMP), hence the usage of WmpBitmapEncoder. – sk_ Jan 09 '15 at 11:48
  • 2
    This was already answered here: http://stackoverflow.com/questions/13469631/system-drawing-bitmap-to-jpeg-xr – sk_ Jan 09 '15 at 11:50
  • The result is an slightly bigger file i cant open. My IExplorer can display jxr files but cant open the result. There is no Exception thrown. – b1ub Jan 09 '15 at 11:51

1 Answers1

-2

try this:

public static MemoryStream SaveJpegXr(this Bitmap bitmap, float quality) 
    {
        var stream = new MemoryStream();
        SaveJpegXr(bitmap, quality, stream);
        stream.Seek(0, SeekOrigin.Begin);
        return stream;
    }

    public static void SaveJpegXr(this Bitmap bitmap, float quality, Stream output) 
     {
        var bitmapSource = bitmap.ToWpfBitmap();
        var bitmapFrame = BitmapFrame.Create(bitmapSource);
        var jpegXrEncoder = new WmpBitmapEncoder();
        jpegXrEncoder.Frames.Add(bitmapFrame);
        jpegXrEncoder.ImageQualityLevel = quality / 100f;
        jpegXrEncoder.Save(output);
    }


    public static BitmapSource ToWpfBitmap(this Bitmap bitmap)
 {
        using (var stream = new MemoryStream()) 
        {
            bitmap.Save(stream, ImageFormat.Bmp);
            stream.Position = 0;
            var result = new BitmapImage();
            result.BeginInit();


            result.CacheOption = BitmapCacheOption.OnLoad;
            result.StreamSource = stream;
            result.EndInit();
            result.Freeze();
            return result;
        }
    }
David Abaev
  • 690
  • 5
  • 22
  • 1
    OK you basically copy-pasted an already existing answer... It would have been a lot more useful to point to the answer directly. – sk_ Jan 09 '15 at 11:51
  • I already tried this. ToWpfBitmap() is not known for my compiler. – b1ub Jan 09 '15 at 11:56
  • 1
    This is an extension method. After including this class in your project, you can use it like this: BitmapSource wmpBmpSource = myBitmap.ToWpfBitmap(); – sk_ Jan 09 '15 at 12:00
  • 1
    For the reference on how to use extension methods: http://msdn.microsoft.com/fr-fr/library/bb383977.aspx – sk_ Jan 09 '15 at 12:40