调整位图图像的大小 [英] Resize bitmap image

查看:32
本文介绍了调整位图图像的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在保存图像时使用更小的尺寸.我怎样才能调整它的大小?我使用此代码重新绘制图像:

I want to have smaller size at image saved. How can I resize it? I use this code for redering the image:

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, 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.

Does your "surface" visual have scaling capability? You can wrap it in a Viewbox if not, then render the Viewbox at the size you want.

当您在表面上调用测量和排列时,您应该提供您希望位图的大小.

When you call Measure and Arrange on the surface, you should provide the size you want the bitmap to be.

要使用 Viewbox,请将您的代码更改为如下所示:

To use the Viewbox, change your code to something like the following:

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, 96d, 96d,
    PixelFormats.Default);
renderBitmap.Render(viewbox);

这篇关于调整位图图像的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆