我有下一个问题,我需要使用resize将字节数组转换为WriteableBitmap
。我写下一段代码。
private byte[] ResizeImage(byte[] array, double maxWidth, double maxHeight)
{
WriteableBitmap wb = null;
var stream = new MemoryStream(array);
stream.Seek(0, SeekOrigin.Begin);
var bmp = new WriteableBitmap(0, 0);
bmp.SetSource(stream);
stream.Close();
var img = new Image();
img.Source = bmp;
double scaleX = 1;
double scaleY = 1;
if (bmp.PixelHeight > maxHeight)
{
scaleY = maxHeight / bmp.PixelHeight;
}
if (bmp.PixelWidth > maxWidth)
{
scaleX = maxWidth / bmp.PixelWidth;
}
wb = new WriteableBitmap(0, 0);
var scale = Math.Min(scaleY, scaleX);
wb.Render(img, new ScaleTransform() { ScaleX = scale, ScaleY = scale });
wb.Invalidate();
return Utils.Encode(wb);
}
致电wb.Render(img, new ScaleTransform() { ScaleX = scale, ScaleY = scale });
后,wb
的像素为零。
请帮助。
答案 0 :(得分:1)
private byte[] ResizeImage(byte[] array, int maxWidth, int maxHeight)
{
var stream = new MemoryStream(array);
stream.Seek(0, SeekOrigin.Begin);
var bmp = new BitmapImage();
bmp.SetSource(stream);
stream.Close();
var img = new Image();
img.Source = new BitmapImage();
double scaleX = 1;
double scaleY = 1;
if (bmp.PixelHeight > maxHeight)
{
scaleY = maxHeight / bmp.PixelHeight;
}
if (bmp.PixelWidth > maxWidth)
{
scaleX = maxWidth / bmp.PixelWidth;
}
WriteableBitmap wb = new WriteableBitmap(maxWidth, maxHeight);
var scale = Math.Min(scaleY, scaleX);
wb.Render(img, new ScaleTransform() { ScaleX = scale, ScaleY = scale });
wb.Invalidate();
return Utils.Encode(wb);
}
答案 1 :(得分:0)
尝试更改:
wb = new WriteableBitmap(0, 0);
要:
wb = new WriteableBitmap(maxWidth, maxHeight);