c# - 调整 WriteableBitmap 的大小

标签 c# wpf writeablebitmap

我创建了一个 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/

相关文章:

c# - 编辑 WriteableBitmap 的原始像素数据?

c# - 禁用基于绑定(bind)值 WPF 的 ListBox 项的选择

C# ActionFilterAttribute 什么是默认的 AttributeUsage

c# - 不支持连接字符串关键字 'server'

c# - 如何从 FileStream 报告进度

wpf - 从 StoryBoard 访问 Grid.Columnspan

c# - 将 WPF 桌面应用程序转移到客户端服务器技术的良好体系结构

c# - 将 WriteableBitmap 转换为位图以便在 EmguCV 中使用

silverlight - 使用 FJCore 对 Silverlight WriteableBitmap 进行编码

c# - 以 36 个字母格式显示 GUID