0

so i can save the image? The image can be found only in those format its not a file its just a BitmapImage.

I know this way in c# any ideas how to make it in C++?

    private static async Task<StorageFile> SaveAsJpeg(WriteableBitmap wb)
{
     byte[] pixels;
     using (var stream = wb.PixelBuffer.AsStream())
     {
        pixels = new byte[(uint)stream.Length];
        await stream.ReadAsync(pixels, 0, pixels.Length);
     }
     var name = String.Format("{0}_{1:yyyy-MM-dd_HH-mm-ss}.jpg", "MyApp", DateTime.Now);
     var outputFile = await KnownFolders.PicturesLibrary.CreateFileAsync(name, CreationCollisionOption.ReplaceExisting);
     using (var writeStream = await outputFile.OpenAsync(FileAccessMode.ReadWrite))
     {
        var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, writeStream);
        encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied,
                             (uint)wb.PixelWidth, (uint)wb.PixelHeight,
                             96, 96, pixels);
        await encoder.FlushAsync();

        using (var outputStream = writeStream.GetOutputStreamAt(0))
        {
           await outputStream.FlushAsync();
        }
     }
     return outputFile;

No need to be task it can be void no problem...

Stamos
  • 3,938
  • 1
  • 22
  • 48

1 Answers1

1

Well, first thing is - if you loaded the BitmapImage in the first place - then you have access to the source file, so the easiest solution would be to copy the source file. As for the WriteableBitmap - it's a bit similar to the C# version, but of course slightly more complicated as you start working with COM-like techniques. See if James's answer to my question about getting access to the pixels helps you a bit:

How to get access to WriteableBitmap.PixelBuffer pixels with C++?

Community
  • 1
  • 1
Filip Skakun
  • 31,624
  • 6
  • 74
  • 100
  • It really complicated and I can just tell you that I cant use the source file stream. – Stamos Apr 18 '14 at 20:09
  • Well, using the source stream is the best way - re-encoding the image is a bad performance choice. If you absolutely can't do that (e.g. if the image was on an SD card that might have been removed) - use `WriteableBitmaps` and the pixel buffer access method from the answer I linked. – Filip Skakun Apr 18 '14 at 20:28
  • Im trying to understand how to save the Ibuffer to a file any ideas on that? – Stamos Apr 19 '14 at 09:32
  • The same way as in your example. Do you have a specific problem? As I said - you can get the byte array from the IBuffer the way it was described in the link. – Filip Skakun Apr 20 '14 at 04:11