Re: Zoom function in VB.NET
If you are trying for a magnifier effect, like a movable square that showed a zoomed view of whatever was under it, then you were on the right track with the first choice, I would say. However, you are always going to have a problem with blur when expanding an image unless the image was compressed to show at normal size. For some kinds of images, such as text, and other high contrast images, you could use a filter to sharpen the image, but for pictures and the like, this might not make sense. The problem is that if the image is displayed at normal size, then anything you do to zoom in is going to get increasingly distorted simply because there isn't sufficient information in the image to show greater detail.
Re: Zoom function in VB.NET
Hi gumgl,
I can suggest a way to do it and I don't think you will find it too complicated. You have already captured the screen image and you are showing it (presumably) on a form background or on a picture box. You need to code the MouseMove event of the Form or PictureBox to grab a small square, let's say 5 x 5 pixels, around the tip of the mouse pointer.
I'll assume you have added a small PictureBox on your form for showing the blown-up pixels -- let's be original and call it PictureBox1. Also a small lable Label1 to show details of the colour.
First you declare a 5 x 5 pixel Bitmap at the top level:
Code:
Dim bmp As New Bitmap(5, 5)
Next you add these lines to the MouseMove event of the Form (or PictureBox) you are using to show the screen image:
Code:
Using g As Graphics = Graphics.FromImage(bmp)
Dim p As Point = MousePosition
p.Offset(-2, 2)'centre the square around the mouse position
g.CopyFromScreen(p, New Point(0, 0), bmp.Size)
End Using
PictureBox1.Invalidate()
Label1.Text = bmp.GetPixel(2, 2).ToString 'show the color of the middle pixel
That will capture the 5 x 5 pixels in bmp and show it in PictureBox1. Finally, you code the Paint event of PictureBox1. The main points to note are:
1. InterpolationMode = NearestNeighbor. That will blow up the Pixels to neat squares instead of blurry.
2. PixelOffsetMode = Half. Keeps the blown-up pixels centred in the PictureBox.
As a final flourish, we'll also draw a little rectangle around the middle pixel. Here's the code:
Code:
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
Dim w As Integer = PictureBox1.Width
Dim h As Integer = PictureBox1.Height
e.Graphics.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor
e.Graphics.PixelOffsetMode = Drawing2D.PixelOffsetMode.Half
e.Graphics.DrawImage(bmp, 0, 0, w, h)
'draw a rectangle around the middle pixel:
Dim r As Rectangle
r.X = w \ 2 - w \ 10
r.Y = h \ 2 - h \ 10
r.Width = w \ 5
r.Height = h \ 5
e.Graphics.DrawRectangle(Pens.LightGray, r)
End Sub
As you can see, there is really only one line of drawing in the Paint Sub (apart from the rectangle). NearestNeighbor does the rest.
BB
Re: Zoom function in VB.NET
Thanks! I will try it as soon as I go home, will let you know if it works.
Thanks again!
gumgl