Results 1 to 4 of 4

Thread: Zoom function in VB.NET

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    2

    Question Zoom function in VB.NET

    I wrote a program to pick a color from the screen (takes a screenshot, then displays it fullscreen and gets the pixel color with MyBitmap.GetPixel()) and I would like to add a zoomed view of the area around the cursor to be more precise. I searched a lot on the web for different solutions:
    • I first tried to draw an image and set each pixel around the cursor to the according pixel in my image, and put this image in a pictureBox with a bigger size and SizeMode set to Stretch but unfortunately, the image is really blurry and that's not what I want.
    • Another way would be to create a lot of panels and make a square with it and then set their BackColor accourding to the pixel colors around the cursor.
      This would be really long (I don't want to create 36 panels). I could make a 2d array of System.Windows.Forms.Panel and place them but I think this would be very slow and I did not try it yet so I'm not even sure if it is possible.
    • Another way would be to draw an image with ratio 1:1 of my zoomed area and then scale it using GDI+ but I never used this and it would be complicated.

    What solution would be the best considering my skills (I started coding in VB.NET 1 month ago) and also the performance of the program (zoom would be refreshed on MouseMove)?

    I never created threads for a problem before but now I have absolutely no idea what would be the best solution. Thank you for helping me!

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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.
    My usual boring signature: Nothing

  3. #3
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    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

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    2

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width