Solved

.Net PictureBox control / Image class scaling

Posted on 2004-04-29
1
2,317 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 86

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

695 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