[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Zoom in PictureBox using VB.net

Posted on 2009-12-24
16
Medium Priority
?
2,380 Views
Last Modified: 2013-12-17
Hi,

I am using PictureBox and I am applying a huge zoom in my image (800%). I am needing to see the pixels by pixles ("huge pixels"), but, unfortunately, PictureBox is performing kind off anti-alias in the zoom-mode. How can I see "huge pixels" in my Picture Box?
0
Comment
Question by:jr_barros_jr
16 Comments
 
LVL 4

Expert Comment

by:me655321
ID: 26120572
The picturebox doesn't have a way to not antialias built into the zoom. Here is how I would do it. This assumes you have Picturebox1 and it's ImageLocation is set to your image. You can modify it otherwise.
Sub fixImage()
        PictureBox1.Image = New Bitmap(PictureBox1.Width, PictureBox1.Height)
        Dim tmpImage As Image = Image.FromFile(PictureBox1.ImageLocation)
        Dim g As Graphics = Graphics.FromImage(PictureBox1.Image)
        g.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor
        Dim wRatio, hRatio, useRatio As Decimal
        Dim wOffset, hOffset As Integer
        wRatio = PictureBox1.Width / tmpImage.Width
        hRatio = PictureBox1.Height / tmpImage.Height
        If wRatio > hRatio Then
            useRatio = hRatio
            wOffset = (PictureBox1.Width - tmpImage.Width * useRatio) / 2
        Else
            useRatio = wRatio
            hOffset = (PictureBox1.Height - tmpImage.Height * useRatio) / 2
        End If
        g.DrawImage(tmpImage, wOffset, hOffset, tmpImage.Width * useRatio, tmpImage.Height * useRatio)
    End Sub

Open in new window

0
 

Author Comment

by:jr_barros_jr
ID: 26122293
Hello me655321,

I tried your code. I use the following code which is yours with almost nothing changed. unfortunately, does not work. Am I doing something wrong?



Sub MyZoom()

        Dim tmpImage As Image = frmMain.picDesktop.Image
        Dim g As Graphics = Graphics.FromImage(frmMain.picDesktop.Image)
        Dim wRatio, hRatio, useRatio As Decimal
        Dim wOffset, hOffset As Integer

        g.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor
        wRatio = frmMain.picDesktop.Width / tmpImage.Width
        hRatio = frmMain.picDesktop.Height / tmpImage.Height

        If wRatio > hRatio Then
            useRatio = hRatio
            wOffset = (frmMain.picDesktop.Width - tmpImage.Width * useRatio) / 2
        Else
            useRatio = wRatio
            hOffset = (frmMain.picDesktop.Height - tmpImage.Height * useRatio) / 2
        End If

        g.DrawImage(tmpImage, wOffset, hOffset, tmpImage.Width * useRatio, tmpImage.Height * useRatio)

    End Sub

Open in new window

0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Comment

by:jr_barros_jr
ID: 26122308
Hello CodeCruiser,

I tried your links and none of code from them worked. Could you do these code worked in your computer?

Thank you,

Jose Roberto
0
 

Author Comment

by:jr_barros_jr
ID: 26122312
Perhaps I was not able to explain my problem very well,



EE-01.png
0
 

Author Comment

by:jr_barros_jr
ID: 26122315
I would like to have the bellow image as 16x zoom result . I do not want the above image.

EE-02.png
0
 

Author Comment

by:jr_barros_jr
ID: 26134164
Folks,

I do not have a solution up to now. May I close this "question" without a solution?
0
 

Author Comment

by:jr_barros_jr
ID: 26294368
Even in C# that is not what I am looking for.
0
 
LVL 4

Expert Comment

by:me655321
ID: 26296095
Sorry I haven't responded sooner. Maybe I misunderstand what you're trying to do? I assumed the image you posted was a capture of the zoomed in image? Also assumed you were working off the original smaller image. If that's the case, I don't see why my example doesn't work. What exactly do you mean by "almost nothing changed"? What exactly DID change?
0
 

Author Comment

by:jr_barros_jr
ID: 26296475
Did you see the 2 above pictures in this question? Could you realize the difference between both?

I want to see big pixels. I want to see big squares like in EE-02.png and like any other photo editor.

Was I able to explain myself?

Thank you,

Jose Roberto


0
 
LVL 4

Expert Comment

by:me655321
ID: 26297111
Yes I understand that...but you can't go from EE-01 to EE-02(both large images). I was under the impression that you had an original image(small) and you want to go from that to EE-02 instead of EE-01. Is that not the case?
0
 

Author Comment

by:jr_barros_jr
ID: 26297946
I am attaching a new image. The image is an open file icon. I want, when apply a zoom, I want to see the picture on right-hand side (big pixels). I do NOT want to see left-hand side image.



EE.png
0
 
LVL 4

Accepted Solution

by:
me655321 earned 2000 total points
ID: 26325386
My question was about your source image. I'm assuming it's the smaller 16x16 image? That's what I was asking. My original code should work for you. Basically you just want to use graphics.DrawImage with graphics.InterpolationMode set to NearestNeighbor. Try just running this code and replacing "File Location" with your actual file location.
frmMain.picDesktop.Image = New Bitmap(256, 256)
        Dim g As Graphics = Graphics.FromImage(frmMain.picDesktop.Image)
        g.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor
        g.DrawImage(Image.FromFile("File Location"), 0, 0, 256, 256)

Open in new window

0
 

Author Comment

by:jr_barros_jr
ID: 26325555
Dear me655321,

First of all, I should thank you for your insistence. Your code really works. Actually, your code was missing only a small piece of code (peanuts), but this not affect your excellent performance.

Here is the code with this small fixes I talked above (PictureBox in Visual Basic only update images if you dispose the "the source" image):

Dim MyBitmap As New Bitmap(256, 256)
picDesktop.Image = MyBitmap.Clone
Dim g As Graphics = Graphics.FromImage(picDesktop.Image)
g.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor
g.DrawImage(picZoom.Image.Clone, 0, 0, 256, 256)
MyBitmap.Dispose()
MyBitmap = Nothing

Open in new window

0
 
LVL 4

Expert Comment

by:me655321
ID: 26325989
Strange...never had an issue with that. Glad you got it working though. By the way, maybe you could try doing picDesktop.Refresh instead? Not sure if that would do the trick or not, just an idea.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month18 days, 8 hours left to enroll

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question