我创建了一个 Gray16 格式的 WriteableBitmap。我想将此 WriteableBitmap 调整为我已知的尺寸,保留像素格式(Gray16)。
有人致力于调整 WriteableBitmap 的大小吗?请帮我。
我也在网上搜索了一下,发现http://writeablebitmapex.codeplex.com/但这是由于装配引用错误造成的。
请帮助我。
最佳答案
您可以使用以下函数来调整 writableBitmap
的大小:
WriteableBitmap resize_image(WriteableBitmap img, double scale)
{
BitmapSource source = img;
var s = new ScaleTransform(scale, scale);
var res = new TransformedBitmap(img, s);
return convert_BitmapSource_to_WriteableBitmap(res);
}
WriteableBitmap convert_BitmapSource_to_WriteableBitmap(BitmapSource source)
{
// Calculate stride of source
int stride = source.PixelWidth * (source.Format.BitsPerPixel / 8);
// Create data array to hold source pixel data
byte[] data = new byte[stride * source.PixelHeight];
// Copy source image pixels to the data array
source.CopyPixels(data, stride, 0);
// Create WriteableBitmap to copy the pixel data to.
WriteableBitmap target = new WriteableBitmap(source.PixelWidth
, source.PixelHeight, source.DpiX, source.DpiY
, source.Format, null);
// Write the pixel data to the WriteableBitmap.
target.WritePixels(new Int32Rect(0, 0
, source.PixelWidth, source.PixelHeight)
, data, stride, 0);
return target;
}
关于c# - 调整 WriteableBitmap 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3300986/