放大和缩小代码 [英] Code for Zoom in and Zoom out

查看:147
本文介绍了放大和缩小代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您能告诉我如何放大和缩小Windows窗体中的图像吗?我有一个显示图像的图片框.现在我不知道如何编写代码来放大和缩小图片框中的图像.你能帮我吗?感谢

Can u tell me how to zoom in and zoom out image in windows form. i have a picture box which display image. now i dont know how to write code to zoom in and zoom out image in picture box. can u help me, please. thanks

推荐答案

最简单的方法是根本不使用图片框:使用面板,并通过处理面板上的Paint事件来自己绘制图像:
The easiest way is not to use a picture box at all: use a Panel, and paint the image yourself by handling the Paint event foe the panel:
private void panImage_Paint(object sender, PaintEventArgs e)
    {
    Graphics g = e.Graphics;
    g.ScaleTransform(sx, sy);
    g.DrawImage(myImage, new Point(0, 0));
    }
private void butZoomIn_Click(object sender, EventArgs e)
    {
    sx *= 2;
    sy *= 2;
    panImage.Invalidate();
    }
private void butZoomOut_Click(object sender, EventArgs e)
    {
    sx /= 2;
    sy /= 2;
   panImage.Invalidate();
    }


可能是,您不需要使用图片框,请参见: http://www.switchonthecode.com/tutorials/csharp-tutorial-image-editing-saving-cropping-and-resizing [
Chances are, you don''t need to use picture box, see this: How do I clear a panel from old drawing[^].

What you need is to re-sample the bitmap. Too bad you did not tag your question: is it WPF or System.Drawing or something else, but as you mention forms and picture box, I guess you mean Forms. (You need to tag you UI/Graphics library anyway!)

This will explain how to do it with System.Drawing: http://www.switchonthecode.com/tutorials/csharp-tutorial-image-editing-saving-cropping-and-resizing[^].

—SA


哇.请不要一遍又一遍地问同样的问题.所显示的代码正在完全按照我告诉您的方式进行.如果您要求提供更多详细信息并加以澄清,那将是您的最佳选择.当然,不可避免地,此代码会随着缩放而失去质量.因此,您正在使用本地的图像吗?
Wow. Please do not ask the same question over and over. The code you''ve been shown, is doing exactly what I told you to do. If you''d asked for more detail and clarified, then that would have been the way to go. Of course, this code will lose quality as you zoom, as is inevitable. So, you ARE using images you have locally ?


这篇关于放大和缩小代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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