Solved

.Net PictureBox control / Image class scaling

Posted on 2004-04-29
1
2,306 Views
Last Modified: 2013-11-22
Hi all,

I'm doing a small picture viewer and I'd like to scale the pictures so they fit in a PictureBox control (VB.Net).  The SizeMode property doesn't do what I want, because there is no option to scale the image while maintaining the aspect ratio.  Help on this would be greatly appreciated.  Thanks!
0
Comment
Question by:instruo
1 Comment
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 110 total points
ID: 10954728
Here is one way to do it.  Run the app, select an image and try resizing the form.

Idle_Mind

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents imageSelectButton As System.Windows.Forms.Button
    Friend WithEvents imageName As System.Windows.Forms.Label
    Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
    Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.imageSelectButton = New System.Windows.Forms.Button
        Me.imageName = New System.Windows.Forms.Label
        Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
        Me.PictureBox1 = New System.Windows.Forms.PictureBox
        Me.SuspendLayout()
        '
        'imageSelectButton
        '
        Me.imageSelectButton.Location = New System.Drawing.Point(8, 16)
        Me.imageSelectButton.Name = "imageSelectButton"
        Me.imageSelectButton.Size = New System.Drawing.Size(80, 24)
        Me.imageSelectButton.TabIndex = 0
        Me.imageSelectButton.Text = "Select Image"
        '
        'imageName
        '
        Me.imageName.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.imageName.Location = New System.Drawing.Point(96, 8)
        Me.imageName.Name = "imageName"
        Me.imageName.Size = New System.Drawing.Size(380, 40)
        Me.imageName.TabIndex = 1
        Me.imageName.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        '
        'PictureBox1
        '
        Me.PictureBox1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.PictureBox1.Location = New System.Drawing.Point(8, 48)
        Me.PictureBox1.Name = "PictureBox1"
        Me.PictureBox1.Size = New System.Drawing.Size(468, 368)
        Me.PictureBox1.TabIndex = 2
        Me.PictureBox1.TabStop = False
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(480, 422)
        Me.Controls.Add(Me.PictureBox1)
        Me.Controls.Add(Me.imageName)
        Me.Controls.Add(Me.imageSelectButton)
        Me.Name = "Form1"
        Me.Text = "One way to Scale an Image while maintaining its Aspect Ratio"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub imageSelectButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles imageSelectButton.Click
        OpenFileDialog1.Title = "Please select an Image to view..."
        OpenFileDialog1.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*"
        If OpenFileDialog1.ShowDialog = DialogResult.OK Then
            imageName.Text = OpenFileDialog1.FileName
            Form1_Resize(Nothing, Nothing)
        End If
    End Sub

    Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
        If imageName.Text <> "" Then
            Dim img As Image
            Dim ratio As Double
            Dim s As Size

            If PictureBox1.Width > 0 And PictureBox1.Height > 0 Then
                Try
                    img = Image.FromFile(imageName.Text)
                    ratio = img.Height / img.Width
                    s.Width = PictureBox1.Width
                    s.Height = s.Width * ratio
                    If s.Height > PictureBox1.Height Then
                        ratio = PictureBox1.Height / s.Height
                        s.Width = s.Width * ratio
                        s.Height = s.Height * ratio
                    End If
                    If s.Width > 0 And s.Height > 0 Then
                        PictureBox1.Image = New Bitmap(img, s)
                    End If
                    img.Dispose()
                Catch ex As Exception
                    MsgBox("Unable to Load Image: " & vbCrLf & imageName.Text, MsgBoxStyle.Critical, "Error Loading Image File")
                End Try
            End If
        End If
    End Sub
End Class
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now