I am working with windows application, I have pictureBox to load image from the directory. I am trying to get some idea how can I display magnified image of particular area depending on the coordinates of that specific area on textbox focus.

Dani AI

Generated

A compact, in-form magnifier works best by drawing a scaled region of the source image into a second PictureBox (the "zoomBox") instead of using a system magnifier. For 's workflow (show a magnified area when a textbox with coordinates gets focus) the pattern is: parse the coordinates, map those control coordinates into image pixels, clip to the image bounds, then draw that source rectangle into the zoomBox scaled to the zoomBox size. 's system-level idea is valid, but the code below keeps everything inside your form and keeps fields editable.

Key mapping notes: PictureBox.SizeMode matters. For Zoom compute the scale = Min(pb.Width/img.Width, pb.Height/img.Height), find the displayed size and offset, then translate control point -> image point by reversing that scale and offset. For StretchImage use separate X/Y scale factors. Always clip your source Rect to the image bounds.

Example helper methods (call UpdateZoom with an image-coordinate rectangle parsed from your textboxes):

private Point ControlToImage(Point ctlPt, PictureBox pb)
{
    var img = pb.Image;
    if (img == null) return Point.Empty;
    if (pb.SizeMode == PictureBoxSizeMode.StretchImage)
    {
        float sx = img.Width / (float)pb.Width;
        float sy = img.Height / (float)pb.Height;
        return new Point((int)(ctlPt.X * sx), (int)(ctlPt.Y * sy));
    }
    float ratio = Math.Min(pb.Width / (float)img.Width, pb.Height / (float)img.Height);
    int dispW = (int)(img.Width * ratio), dispH = (int)(img.Height * ratio);
    int offX = (pb.Width - dispW) / 2, offY = (pb.Height - dispH) / 2;
    return new Point((int)((ctlPt.X - offX) / ratio), (int)((ctlPt.Y - offY) / ratio));
}

private void UpdateZoom(Rectangle src, PictureBox mainPb, PictureBox zoomPb)
{
    var img = mainPb.Image as Bitmap;
    if (img == null) return;
    var clipped = Rectangle.Intersect(src, new Rectangle(0,0,img.Width,img.Height));
    var zoomBmp = new Bitmap(zoomPb.Width, zoomPb.Height);
    using (var g = Graphics.FromImage(zoomBmp))
    {
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        g.DrawImage(img, new Rectangle(0,0,zoomBmp.Width,zoomBmp.Height), clipped, GraphicsUnit.Pixel);
    }
    var old = zoomPb.Image;
    zoomPb.Image = zoomBmp;
    old?.Dispose();
}

Practical tips: wire UpdateZoom on TextBox.Enter/TextChanged/Validated depending on when you want updates; parse and validate the typed coords; avoid creating huge temp Bitmaps repeatedly (reuse when possible); set interpolation to HighQualityBicubic for nicer zoom; clip coordinates and guard nulls; enable double buffering to reduce flicker. This gives a simple, responsive in-form magnifier tied to textbox-driven coordinates.

Recommended Answers

All 3 Replies

Thanks tinstaafl for the suggestion. But I am looking for something specific for just picture box area. As the requirement is user needs to look at the zoom part and type in the fields associated

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.