Pregunta

I'm currently writing a small image editing app for the Windows Store in which I'd like to make use of the share charm for edited images. As some apps do only accept StorageFiles and not Bitmaps (like the default Mail App) I'd like to offer the images in both ways as explained here: http://www.jeffblankenburg.com/2012/11/07/31-days-of-windows-8-day-7-share-contract However, I don't know how to create a StorageFile from a WrieableBitmap.

Perhaps you could help me completing my code below.

// Share Image
async void dtm_DataRequested(DataTransferManager sender, DataRequestedEventArgs e)
{
    DataRequestDeferral deferral = e.Request.GetDeferral();

    DataPackage requestData = e.Request.Data;
    requestData.Properties.Title = "My Image";
    requestData.Properties.Description = "Created using a Windows Store App.";


    // This stream is to create a bitmap image later
    IRandomAccessStream stream = new InMemoryRandomAccessStream();

    // Determin which type of BitmapEncoder we should create
    Guid encoderId = BitmapEncoder.JpegEncoderId;

    BitmapEncoder encoder = await BitmapEncoder.CreateAsync(encoderId, stream);
    Stream pixelStream = theImage.PixelBuffer.AsStream();
    byte[] pixels = new byte[pixelStream.Length];
    await pixelStream.ReadAsync(pixels, 0, pixels.Length);
    encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)theImage.PixelWidth, (uint)theImage.PixelHeight, 96.0, 96.0, pixels);

    requestData.SetBitmap(RandomAccessStreamReference.CreateFromStream(stream));



    // What goes here?
    StorageFile storageFileImage = 



    List<IStorageItem> files = new List<IStorageItem>();
    files.Add(storageFileImage);
    requestData.SetStorageItems(files);


    requestData.Properties.Thumbnail = RandomAccessStreamReference.CreateFromStream(stream);
    await encoder.FlushAsync();


    deferral.Complete();
}
¿Fue útil?

Solución

Below is shown how you would encode a WriteableBitmap wb to StorageFile which is in the Pictures lib.

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;
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top