【问题标题】:Simple Zoom tool in .NET.NET 中的简单缩放工具
【发布时间】:2014-11-24 17:02:41
【问题描述】:

在 WinForms(最好是 C#)中,我如何制作一个简单的“缩放”工具来在光标位置下方显示一个矩形视图?理想情况下,只放大控件(按钮、标签……)

不过,首先,标准库 (.dll) 可以做到这一点吗?我在处理图形方面完全是新手……

提前致谢!

编辑:此问题/答案 (Zoom a Rectangle in .NET) 涉及缩放图像,而不是输入控件。我只想缩放控件。


Edit2:通过每个控件的 MouseEnter 事件,我定位一个面板,该面板应包含放大的控件图像。我只在正确的站点上获得面板......

private void anyControl_MouseEnter(object sender, EventArgs e)
{
    Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, 
                     Screen.PrimaryScreen.Bounds.Height);
    //Create the Graphic Variable with screen Dimensions
    Graphics graphics = Graphics.FromImage(printscreen as Image);
    //Copy Image from the screen
    graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size);

    Control auxControl = (Control) sender;
    panel.Width = auxControl + 20;
    panel.Height = auxControl + 20;
    panel.Location = new Point (auxControl.Location.X - 10, auxControl.Location.Y - 10);

    control.DrawToBitmap(printscreen, panel.Bounds)
}

【问题讨论】:

    标签: c# graphics .net-4.5


    【解决方案1】:

    您可以使用此代码从屏幕上获取图像(感谢http://www.codeproject.com/Articles/485883/Create-your-own-Snipping-Tool):

    Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, 
                             Screen.PrimaryScreen.Bounds.Height);
    //Create the Graphic Variable with screen Dimensions
    Graphics graphics = Graphics.FromImage(printscreen as Image);
    //Copy Image from the screen
    graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size);
    

    所以使用这个和你分享的链接应该排序

    如果您不热衷于捕获屏幕部分,请使用表单的 DrawToBitmap 方法(感谢Capture screenshot of only a section of a form?

    【讨论】:

    • 这是一个好的开始。接下来的步骤是: 1. 创建一个面板并在每次鼠标移动时定位它以跟随光标。 2. 用答案中的代码显示您抓取的位图的放大版本。 (将 DrawImage 与两个 Rectangle 一起使用!) 3. 确保在需要再次抓取 Screen 或 Form 时隐藏 Panel。
    【解决方案2】:

    使用下面的代码我解决了我的问题,但最终的图像有些模糊......(我将图像“缩放”到 20%)

     private void anyControl_MouseEnter(object sender, EventArgs e)
     {
    
        Control auxControl = (Control) sender;
    
        int enlargedWidth = (int) Math.Round(auxControl.Width * 1.20);
        int enlargedHeight = (int) Math.Round(auxControl.Height * 1.20);
    
        panel.Width = enlargedWidth;
        panel.Height = enlargedHeight;
        panel.Location = new Point (auxControl.Location.X - (int) Math.Round(auxControl.Width * 0.10), auxControl.Location.Y - (int) Math.Round(auxControl.Height * 0.10));
    
        Bitmap aBitmap = new System.Drawing.Bitmap(auxControl.Width, auxControl.Height);
        auxControl.DrawToBitmap(aBitmap, auxControl.ClientRectangle);
    
        Bitmap aZoomBitmap = ZoomImage(aBitmap, panel.Bounds);
        panel.ContentImage = aZoomBitmap;
    
        panel.Visible = true;
     }
    
     private Bitmap ZoomImage(Bitmap pBmp, Rectangle pDestineRectangle)
     {
    
        Bitmap aBmpZoom = new Bitmap(pDestineRectangle.Width, pDestineRectangle.Height);
        Graphics g = Graphics.FromImage(aBmpZoom);
        Rectangle srcRect = new Rectangle(0, 0, pBmp.Width, pBmp.Height);
        Rectangle dstRect = new Rectangle(0, 0, aBmpZoom.Width, aBmpZoom.Height);
        g.DrawImage(pBmp, dstRect, srcRect, GraphicsUnit.Pixel);
    
        return aBmpZoom;
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-11
      • 2016-10-30
      相关资源
      最近更新 更多