-1

I have a problem with Convert WriteableBitmap to BitmapImage using BmpBitmapEncoder. this is my method:

    public BitmapImage ConvertWriteableBitmapToBitmapImage(WriteableBitmap wbm)
    {
        bmp = new BitmapImage();
        using (MemoryStream stream = new MemoryStream())
        {
            /*PngBitmapEncoder encoder = new PngBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(wbm));
            encoder.Save(stream);*/
            BmpBitmapEncoder encoder = new BmpBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(wbm));
            encoder.Save(stream);

            bmp.BeginInit();
            bmp.UriSource = new Uri(MyImage.Source.ToString());
            bmp.CacheOption = BitmapCacheOption.OnLoad;
            bmp.CreateOptions = BitmapCreateOptions.IgnoreImageCache | BitmapCreateOptions.PreservePixelFormat;
            bmp.StreamSource = stream;
            bmp.EndInit();
            bmp.Freeze();
        }
        return bmp;
    }

I'm using BmpBitmapEncoder because this is only way to Save without change size of Image(*.bmp). I want to Save Image with changed table of pixels and the specified format pixel (Bgr24). Using BmpBitmapEncoder forces to set bmp.UriSource and this is a problem. WriteableBitmap doesn't have this Property. Moreover, when I comment line //bmp.UriSource shows me an exception: "System.ArgumentNullException" in bmp.EndInit(). When I change my Method to this:

    public BitmapImage ConvertWriteableBitmapToBitmapImage(WriteableBitmap wbm)
    {
        bmp = new BitmapImage();
        using (MemoryStream stream = new MemoryStream())
        {
            PngBitmapEncoder encoder = new PngBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(wbm));
            encoder.Save(stream);
            /*BmpBitmapEncoder encoder = new BmpBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(wbm));
            encoder.Save(stream);*/

            bmp.BeginInit();
            //bmp.UriSource = new Uri(MyImage.Source.ToString());
            bmp.CacheOption = BitmapCacheOption.OnLoad;
            //bmp.CreateOptions = BitmapCreateOptions.IgnoreImageCache | BitmapCreateOptions.PreservePixelFormat;
            bmp.StreamSource = stream;
            bmp.EndInit();
            bmp.Freeze();
        }
        return bmp;
    }

everything works fine but the result is the Image increase size and change pixel format to Bgr32 and this is not the result, what I expect. My method which Save Image is fine because I tested it on unchanged pixels and the result is good - Image don't change format and size. Plz help me with this.

Lukii007
  • 85
  • 1
  • 3
  • 4

2 Answers2

0

For changing pixel formats WPF has the FormatConvertedBitmap class, which is a BitmapSource:

WriteableBitmap wbm;
...
var bm = new FormatConvertedBitmap(wbm, PixelFormats.Bgr24, null, 0);

It is not a BitmapImage, but you do not really need that anywhere. All WPF methods and properties (e.g. Image.Source) use either ImageSource or the derived BitmapSource as their type. There is no API that explicitly needs a BitmapImage, so conversion to BitmapImage is never necessary.

Clemens
  • 123,504
  • 12
  • 155
  • 268
0

I resolve this problem. This is a code which Convert WriteableBitmap to BitmapImage using BmpBitmapEncoder. Using this method ensure us, that our Image while saving stay unchanged. Unchanged I mean - size don't increase and Format Pixel stay on Bgr24.

    public BitmapImage ConvertWriteableBitmapToBitmapImage(WriteableBitmap wbm)
    {
        bmp = new BitmapImage();
        using (MemoryStream stream = new MemoryStream())
        {
            BmpBitmapEncoder encoder = new BmpBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(wbm));
            encoder.Save(stream);
            bmp.BeginInit();
            bmp.CacheOption = BitmapCacheOption.OnLoad;
            bmp.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
            bmp.StreamSource = new MemoryStream(stream.ToArray()); //stream;
            bmp.EndInit();
            bmp.Freeze();
        }
        return bmp;
    }
Lukii007
  • 85
  • 1
  • 3
  • 4
  • You still haven't told us why you need a BitmapImage at all. The whole conversion is usually not necessary, because you can pass the original WriteableBitmap to all methods or properties that accept a BitmapSource or ImageSource. – Clemens Mar 27 '15 at 08:03