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

天大地大妈咪最大 提交于 2019-12-17 10:53:40

问题


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

How do I do this?


回答1:


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

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);



回答2:


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

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!



来源:https://stackoverflow.com/questions/10915958/how-to-zoom-an-image-inout-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!