mzurita
mzurita

Reputation: 690

Simple Zoom tool in .NET

In WinForms (preferable C#), how could I make a simple "zoom" tool to show a rectangle-view below cursor position? Ideally, only zooming in over controls (buttons, labels…)

Although, first at all, Is this possible with the standard libraries (.dll)? I'm completely newbie in working with graphics…

Thanks in advance!

Edit: This question/answer (Zoom a Rectangle in .NET) treats about zooming images, not input controls. I just want to zoom the control.


Edit2: Through MouseEnter event for each control I position a panel which should contain the image of the control enlarged. I only get the panel in right site…

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

Upvotes: 0

Views: 745

Answers (2)

mzurita
mzurita

Reputation: 690

With the following code I solved my problem, but the final image is a few blurred… (I "zoomed" the image to 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;
 }

Upvotes: 0

Kell
Kell

Reputation: 3317

Well you can get an image from the screen with this code (thanks 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);

so using this and the link you shared you should be sorted

or use DrawToBitmap method of the form if you're not keen on capturing screen portions (thanks Capture screenshot of only a section of a form?)

Upvotes: 1

Related Questions