Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

[VB.NET]Resize image to fit in picturebox

originally posted on dotnet-snippets.com

Description:

Automatically resize an image so it shows ok in the picturebox on your form. It doesn't change the actual image.


Code
Public Sub AutosizeImage(ByVal ImagePath As String, ByVal picBox As PictureBox, Optional ByVal pSizeMode As PictureBoxSizeMode = PictureBoxSizeMode.CenterImage)
        Try
            picBox.Image = Nothing
            picBox.SizeMode = pSizeMode
            If System.IO.File.Exists(ImagePath) Then
                Dim imgOrg As Bitmap
                Dim imgShow As Bitmap
                Dim g As Graphics
                Dim divideBy, divideByH, divideByW As Double
                imgOrg = DirectCast(Bitmap.FromFile(ImagePath), Bitmap)

                divideByW = imgOrg.Width / picBox.Width
                divideByH = imgOrg.Height / picBox.Height
                If divideByW > 1 Or divideByH > 1 Then
                    If divideByW > divideByH Then
                        divideBy = divideByW
                    Else
                        divideBy = divideByH
                    End If

                    imgShow = New Bitmap(CInt(CDbl(imgOrg.Width) / divideBy), CInt(CDbl(imgOrg.Height) / divideBy))
                    imgShow.SetResolution(imgOrg.HorizontalResolution, imgOrg.VerticalResolution)
                    g = Graphics.FromImage(imgShow)
                    g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
                    g.DrawImage(imgOrg, New Rectangle(0, 0, CInt(CDbl(imgOrg.Width) / divideBy), CInt(CDbl(imgOrg.Height) / divideBy)), 0, 0, imgOrg.Width, imgOrg.Height, GraphicsUnit.Pixel)
                    g.Dispose()
                Else
                    imgShow = New Bitmap(imgOrg.Width, imgOrg.Height)
                    imgShow.SetResolution(imgOrg.HorizontalResolution, imgOrg.VerticalResolution)
                    g = Graphics.FromImage(imgShow)
                    g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
                    g.DrawImage(imgOrg, New Rectangle(0, 0, imgOrg.Width, imgOrg.Height), 0, 0, imgOrg.Width, imgOrg.Height, GraphicsUnit.Pixel)
                    g.Dispose()
                End If
                imgOrg.Dispose()

                picBox.Image = imgShow
            Else
                picBox.Image = Nothing
            End If


        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

    End Sub


If You Like this tutorial, please check out my other tutorials by clicking ==> H3R3
PHP & .NET Contributor.
Lol or you could set the PictureBox's SizeMode to Zoom.
(May 1, 2010 09:03 PM)Zer0Fact0r Wrote:  Lol or you could set the PictureBox's SizeMode to Zoom.

no comment...considering your post count.
PHP & .NET Contributor.
(May 1, 2010 09:03 PM)Zer0Fact0r Wrote:  Lol or you could set the PictureBox's SizeMode to Zoom.

This is correct.
(May 1, 2010 09:03 PM)Zer0Fact0r Wrote:  Lol or you could set the PictureBox's SizeMode to Zoom.

but some time it doesnt works it has happened to me

thanks crow
(May 1, 2010 09:06 PM)codepoet Wrote:  no comment...considering your post count.

why.. what he said is absolutely right .. i believe... 😉
Or you can set the picturebox´s sizemode to STRETCHIMAGE
(May 2, 2010 12:07 AM)๖ۣۜBlueAce™ Wrote:  why.. what he said is absolutely right .. i believe... 😉

he is right but the point of this tutorial is to give you an option...and he made it seem like this code is useless...but i don't want to flame so just for get it.
PHP & .NET Contributor.
(May 2, 2010 02:12 AM)HaXZX0r Wrote:  Or you can set the picturebox´s sizemode to STRETCHIMAGE

/thread


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by C++Dec 29, 2019 06:16 PM 16 827 Dec 29, 2019 06:16 PM
Last Post: DNA
[.NET] by ShapelessSep 27, 2019 12:15 PM 16 600 Sep 27, 2019 12:15 PM
Last Post: 0xDEAD10CC
[Python] by DistrictApr 7, 2019 03:14 AM 4 228 Apr 7, 2019 03:14 AM
Last Post: lockhead