我试着写的函数,将调整图像的大小。我将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格式。怎么了?
发布于 2012-10-17 15:44:46
我之前已经检查过这个问题了,试试这个线程中的代码:
发布于 2012-10-18 04:37:30
最好的帮助是如果你在CodePlex站点上关注你的帖子;) http://writeablebitmapex.codeplex.com/discussions/399624
不需要交叉过帐。
发布于 2012-10-17 04:53:42
不知道这是否会对你有帮助,但我写了一个LoadAsync()重载,以给定的解码分辨率加载一个位图,这可能会根据你的场景对你有所帮助。
http://winrtxamltoolkit.codeplex.com/SourceControl/changeset/view/0657c67a93d5#WinRTXamlToolkit%2fImaging%2fWriteableBitmapLoadExtensions.cs
https://stackoverflow.com/questions/12919516
复制相似问题