Hack Forums
[.NET] [VB.NET]Resize image to fit in picturebox - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] [VB.NET]Resize image to fit in picturebox (/showthread.php?tid=397063)



[VB.NET]Resize image to fit in picturebox - codepoet - May 1, 2010

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


* - Zer0Fact0r - May 1, 2010

Lol or you could set the PictureBox's SizeMode to Zoom.


* - codepoet - May 1, 2010

(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.


* - DeadCode - May 1, 2010

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

This is correct.


* - Dragon Hawk - May 2, 2010

(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


* - ๖ۣۜBlueAce™ - May 2, 2010

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

why.. what he said is absolutely right .. i believe... 😉


* - HaXZX0r - May 2, 2010

Or you can set the picturebox´s sizemode to STRETCHIMAGE


* - codepoet - May 2, 2010

(May 2, 2010 12:07 AM)BlueAce_Pragy 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.


* - Deathader - May 2, 2010

(May 2, 2010 02:12 AM)HaXZX0r Wrote:  Or you can set the picturebox´s sizemode to STRETCHIMAGE

/thread