Solved

.Net PictureBox control / Image class scaling

Posted on 2004-04-29
1
2,313 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

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
error 1.1 400 Bad request idhttp delphi 18 272
firemonkey Android Listview Sort items 7 102
Delphi Firemonkey send email on Android 1 78
Modify Text File with Excel Macro 13 46
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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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…

732 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