如何在 C# 中放大和缩小图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10915958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:45:05  来源:igfitidea点击:

How to zoom an image in&out in C#?

c#imageresizepicturebox

提问by Mehmet Ince

I want to implement zoom for an image. I don't want to resize the PictureBox, but the image itself.

我想对图像实现缩放。我不想调整PictureBox 的大小,而是调整图像本身。

How do I do this?

我该怎么做呢?

采纳答案by Thorsten Dittmar

One solution is:

一种解决方案是:

  1. Create new image of the desired size (for example 200% or 50% of original image size)
  2. Draw original image to new image using Graphics.DrawImage(Image, Rectangle);, which draws the given image to the new image at the given position with the given size
  3. Set new image as source for the PictureBox
  1. 创建所需尺寸的新图像(例如原始图像尺寸的 200% 或 50%)
  2. 使用Graphics.DrawImage(Image, Rectangle) 将原始图像绘制为新图像, 将给定的图像绘制到给定位置的给定大小的新图像上
  3. 将新图像设置为源 PictureBox

Another way is to simple create a new bitmap instance like that:

另一种方法是简单地创建一个新的位图实例,如下所示:

Size newSize = new Size((int)(originalBitmap.Width * zoomFactor), (int)(originalBitmap.Height * zoomFactor));
Bitmap bmp = new Bitmap(originalBitmap, newSize);

回答by Thorsten Dittmar

I used a web browser to achieve this.

我使用网络浏览器来实现这一点。

//loads the image
myWebBrowser.Navigate(@"C:\myimage.png");

From there I used SendKeys to zoom in and out

从那里我使用 SendKeys 来放大和缩小

myWebBrowser.Select(); //Selects browser.
SendKeys.Send("^{+}"); //Sends the control + key combo, causing the browser to zoom in. Replace the "+" with a "-" to zoom out.

It's a bit of a weird method, but it worked really well for me. I hope you find this helpful!

这是一个有点奇怪的方法,但它对我来说非常有效。我希望你觉得这有用!