Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

.Net PictureBox control / Image class scaling

Posted on 2004-04-29
1
Medium Priority
?
2,323 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 440 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
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…
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

604 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