1

我看到了这个问题的解决方案: Saving as jpeg from memorystream in c#

但它在winRT中不起作用。有没有一种使用 FileSavePicker 将 MemoryStream 保存为 JPEG 的简单方法?

我试过了:

private async void Save_Image(MemoryStream image)
{ 
// Launch file picker
    FileSavePicker picker = new FileSavePicker();
    picker.FileTypeChoices.Add("JPeg", new List<string>() { ".jpg", ".jpeg" });
    StorageFile file = await picker.PickSaveFileAsync();

    if (file == null)
        return;

    Stream x = await file.OpenStreamForWriteAsync();
    image.WriteTo(x)
}

但它正在保存空白文件。可能是我做错了什么。

尝试了另一种方法,但又是空白图像:

private async void Save_Image(MemoryStream image)
    {
        // Launch file picker
        FileSavePicker picker = new FileSavePicker();
        picker.FileTypeChoices.Add("JPeg", new List<string>() { ".jpg", ".jpeg" });
        StorageFile file = await picker.PickSaveFileAsync();            

        if (file == null)
            return;

        int end = (int)image.Length;
        byte[] buffer = new byte[end];                        
        await image.ReadAsync(buffer, 0, end);
        await FileIO.WriteBytesAsync(file, buffer);
    }
4

1 回答 1

3

知道了!这是我缺少的寻找位置,也是“使用”。必须在外部将其设置为 0。这是代码:

private async void Save_Image(MemoryStream image)
    {
        // Launch file picker
        FileSavePicker picker = new FileSavePicker();
        picker.FileTypeChoices.Add("JPeg", new List<string>() { ".jpg", ".jpeg" });
        StorageFile file = await picker.PickSaveFileAsync();            

        if (file == null)
            return;
        using (Stream x = await file.OpenStreamForWriteAsync())
        {
            x.Seek(0, SeekOrigin.Begin);
            image.WriteTo(x);
        }

    }
于 2013-05-13T17:58:54.697 回答