如何在 C# 中以编程方式放大和缩小图像?

发布于 2024-10-09 16:01:38 字数 108 浏览 0 评论 0原文

我希望能够缩放(-/+)放置在面板下的图像。我希望在该面板中显示图像的缩放版本。我怎样才能在 C# 中实现这一目标?

简而言之,当我在 C# 中以编程方式单击图像时,如何放大和缩小图像?

I would like to be able to zoom (-/+) an image that is placed under a panel. I would like the zoomed version of the image to be displayed within that panel. How can I achieve this in C#?

In simple words, how can I zoom an image in and out when I click on the image programmatically in C#?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

少女七分熟 2024-10-16 16:01:38

如果您使用GDI+,那么您当然可以通过使用Matrix变换类来做到这一点,如下所示......

// Get hold of your graphics context...
using(Graphics g = this.CreateGraphics())
{
    // When drawing we want to apply a scaling of 2,2 (making it bigger!)
    Matrix m = new Matrix();
    m.Scale(2, 2, MatrixOrder.Append);
    g.Transform = m;

    // Draw the actual image using the assigned matrix transform
    g.DrawImage(...);        
}

当您想要放大或缩小时,您只需改变使用的缩放值。

If you use GDI+ then you can certainly do this by using the Matrix transform class in something like the following manner...

// Get hold of your graphics context...
using(Graphics g = this.CreateGraphics())
{
    // When drawing we want to apply a scaling of 2,2 (making it bigger!)
    Matrix m = new Matrix();
    m.Scale(2, 2, MatrixOrder.Append);
    g.Transform = m;

    // Draw the actual image using the assigned matrix transform
    g.DrawImage(...);        
}

When you want to zoom in or out you are just altering the scaling values used.

流心雨 2024-10-16 16:01:38

您需要使用 GDI+ 绘图函数(在 .NET Framework 中作为 Graphics 类)来执行此操作。本质上,您将在显示面板中重新绘制基础图像的缩放版本。

使用类似 Graphics.DrawImage ,您所要做的就是指定基本图像、源矩形(基本图像中要放大的部分)和目标矩形(新的缩放图像的尺寸)。

这里有一个教程,您可能会发现值得您花时间查看(源代码下载链接位于页面底部):http://www.vcskicks.com/image-zoom.php

如果这只是激起了您对 C# 图形的兴趣,请查看 本文更全面地介绍其图形相关功能。

You'll need to use the GDI+ drawing functions (exposed in the .NET Framework as methods of the Graphics class) to do this. Essentially, you'll be redrawing a zoomed version of the base image into the display panel.

Using something like Graphics.DrawImage, all you have to do is specify the base image, the source rectangle (the portion of the base image to zoom in on), and the destination rectangle (the dimensions of the new, zoomed image).

There's a tutorial available here that you may find worth your time to check out (source code download link is at the bottom of the page): http://www.vcskicks.com/image-zoom.php

If this has only just whet your appetite for graphics in C#, check out this article for a more comprehensive introduction to its graphics-related capabilities.

痴意少年 2024-10-16 16:01:38

免责声明:我在 Atalasoft 工作

我们的 DotImage Photo SDK 是免费的,并且有一个 WinForms 控件,该控件具有此功能和许多其他功能(平移、自动缩放、放大镜等)

Disclaimer: I work for Atalasoft

Our DotImage Photo SDK is free and has a WinForms control that has this and many other features (panning, autozoom, magnifying glass, etc)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文