Har*_*sha 3 c# wpf writeablebitmap
我创建了一个 Gray16 格式的 WriteableBitmap。我想将此 WriteableBitmap 调整为我已知的尺寸,保留像素格式(Gray16)。
是否有人致力于调整可写位图的大小。请帮我。
我还搜索了互联网并找到了http://writeablebitmapex.codeplex.com/但这通过一个assebmly 引用错误。
请帮我。
您可以使用以下函数来调整 a 的大小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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8095 次 |
| 最近记录: |