0

我尝试编写将调整图像大小的函数。我将 WriteableBitmapEx 用于 WinRT/Win8 函数 Resize();

public class PictureExtension 
{
    private MemoryRandomAccessStream _memoryRandomAccessStream;
    private readonly Stream _dataStream;
    private readonly double _height;
    private readonly double _width;

    public PictureExtension(Stream dataStream, double height, double width)
    {
        _dataStream = dataStream;
        _memoryRandomAccessStream = (_dataStream.ToRandomAccessStream());
        _height = height;
        _width = width;
    }

    public byte[] ToArray(double maxSide)
    {
        if (_height <= maxSide && _width <= maxSide)
        {
            return _dataStream.ToArray();
        }
        else
        {
            var target = new WriteableBitmap((int) _width, (int) _height);

            var aspectRatio = (double)_width / _height;
            double newHeight;
            double newWidth;
            if (_width > _height)
            {
                newWidth = maxSide;
                newHeight = newWidth / aspectRatio;
            }
            else
            {
                newHeight = maxSide;
                newWidth = maxSide * aspectRatio;
            }

            int count = (int)_dataStream.Length;

            using (var bmpStream = target.PixelBuffer.AsStream())
            {
                bmpStream.Seek(0, SeekOrigin.Begin);
                bmpStream.Write(_dataStream.ToArray(), 0, _dataStream.ToArray().Length);
            }


            var resized = target.Resize((int)newWidth, (int)newHeight, WriteableBitmapExtensions.Interpolation.Bilinear);

            return resized.ToByteArray();

        }

    }
}

}

这个函数返回字节数组,但它不再是图像了..我用PNG和JPG格式测试过..有什么问题?

4

3 回答 3

1

我之前一直在研究这个问题,试试这个线程中的代码:
How to resize Image in C# WinRT/winmd?

于 2012-10-17T07:44:45.807 回答
1

如果您在 CodePlex 网站上关注您的帖子,最好的帮助是;) http://writeablebitmapex.codeplex.com/discussions/399624

无需交叉发布。

于 2012-10-17T20:37:29.793 回答
0

不确定这是否对您有帮助,但是我写了一个 LoadAsync() 重载,以给定的解码分辨率加载位图,这可能会根据您的情况对您有所帮助。

http://winrtxamltoolkit.codeplex.com/SourceControl/changeset/view/0657c67a93d5#WinRTXamlToolkit%2fImaging%2fWriteableBitmapLoadExtensions.cs

于 2012-10-16T20:53:42.200 回答