0

我有一个 Windows 8 应用程序,我想在其中旋转图像文件。

在镜头中,我想打开一个图像文件,旋转它并将内容保存回文件。

这在 WinRT 中可能吗?如果是这样,怎么做?谢谢。

更新:

根据 Vasile 的回答,我可以在这方面做一些工作。但是我不确定下一步该怎么做:

    public static async Task RotateImage(StorageFile file)
    {
        if (file == null)
            return;

        var data = await FileIO.ReadBufferAsync(file);

        // create a stream from the file
        var ms = new InMemoryRandomAccessStream();
        var dw = new DataWriter(ms);
        dw.WriteBuffer(data);
        await dw.StoreAsync();
        ms.Seek(0);

        // find out how big the image is, don't need this if you already know
        var bm = new BitmapImage();
        await bm.SetSourceAsync(ms);

        // create a writable bitmap of the right size
        var wb = new WriteableBitmap(bm.PixelWidth, bm.PixelHeight);
        ms.Seek(0);

        // load the writable bitpamp from the stream
        await wb.SetSourceAsync(ms);
        wb.Rotate(90);

        //How should I save the image to the file now?
    }
4

2 回答 2

1

当然这是可能的。您可以自己进行像素操作并创建一个新对象,或者,您可以重用(WriteableBitmap Extensions)WriteableBitmap中已经实现的功能。WriteableBitmapEx您可以通过NuGet.

在这里,您可以找到它提供的已实现功能的描述,以及一些简短的示例。

于 2013-09-17T11:27:10.837 回答
0

使用它保存WriteableBitmapStorageFile

private async Task<StorageFile> WriteableBitmapToStorageFile(WriteableBitmap writeableBitmap)
{
    var picker = new FileSavePicker();
    picker.FileTypeChoices.Add("JPEG Image", new string[] { ".jpg" });
    StorageFile file = await picker.PickSaveFileAsync();
    if (file != null && writeableBitmap != null)
    {
        using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
        {
            BitmapEncoder encoder = await BitmapEncoder.CreateAsync(
                BitmapEncoder.JpegEncoderId, stream);
            Stream pixelStream = writeableBitmap.PixelBuffer.AsStream();
            byte[] pixels = new byte[pixelStream.Length];
            await pixelStream.ReadAsync(pixels, 0, pixels.Length);

            encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore,
                (uint)writeableBitmap.PixelWidth, (uint)writeableBitmap.PixelHeight, 96.0, 96.0, pixels);
            await encoder.FlushAsync();
        }
        return file;
    }
    else
    {
        return null;
    }
}
于 2013-09-17T13:56:19.193 回答