微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 调整位图图像大小

我想要保存的图像尺寸更小.
如何调整大小?
我使用这个代码来重绘图像:
Size size = new Size(surface.Width,surface.Height);
surface.Measure(size);
surface.Arrange(new Rect(size));
// Create a render bitmap and push the surface to it
rendertargetBitmap renderBitmap =
    new rendertargetBitmap(
        (int)size.Width,(int)size.Height,96d,PixelFormats.Default);
renderBitmap.Render(surface);

BmpBitmapEncoder encoder = new BmpBitmapEncoder();
// push the rendered bitmap to it
encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
// save the data to the stream
encoder.Save(outStream);

解决方法

您的“表面”视觉是否具有缩放能力?您可以将它包装在一个ViewBox中,如果没有,然后渲染您想要的大小的ViewBox.

当您调用测量和排列表面时,您应该提供您想要位图的大小.

要使用ViewBox,请将代码更改为以下内容

ViewBox viewBox = new ViewBox();
Size desiredSize = new Size(surface.Width / 2,surface.Height / 2);

viewBox.Child = surface;
viewBox.Measure(desiredSize);
viewBox.Arrange(new Rect(desiredSize));

rendertargetBitmap renderBitmap =
    new rendertargetBitmap(
    (int)desiredSize.Width,(int)desiredSize.Height,PixelFormats.Default);
renderBitmap.Render(viewBox);

原文地址:https://www.jb51.cc/csharp/92945.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐