Tipp 0039 Grafik skalieren
Autor/Einsender:
Datum:
  Bernhard Froschauer
17.04.2004
Entwicklungsumgebung:   VB.Net 2003
Framework:   1.1
In diesem Tipp wird gezeigt wie man Grafikdateien auch ohne PictureBox skaliert anzeigen kann.
Es wird direkt auf ein Panel gezeichnet. Das Formular enthält zwei Panels und einen Splitter. Wird nun die Position des Splitters verändert, werden beide Panels neu gezeichnet. Dabei wird die Grafikausgabe auf die jeweilig verfügbare Anzeigefläche skaliert. Man kann über ein Menü beliebige Grafiken auf den Panels anzeigen.
 
Public Class Form1
  Inherits System.Windows.Forms.Form
Vom Windows Form Designer generierter Code
  Private _imgl As Image
  Private _imgr As Image
  Private _links As Boolean = True
  Private Puffer() As Byte

  Private Sub Form1_Load(ByVal sender As Object, ByVal _
           e As System.EventArgs) Handles MyBase.Load
    _imgl = New Bitmap(Me.GetType, "links.JPG")
    _imgr = New Bitmap(Me.GetType, "rechts.JPG")
  End Sub

#Region "Skalieren"

  Private Sub ScaleImageIsotropically(ByVal grfx As Graphics, _
           ByVal img As Image, ByVal rect As Rectangle)

    Dim szf As New SizeF(img.Width / img.HorizontalResolution, _
                         img.Height / img.VerticalResolution)

    Dim fScale As Single = Math.Min(rect.Width / szf.Width, _
                                    rect.Height / szf.Height)
    szf.Width *= fScale
    szf.Height *= fScale

    grfx.DrawImage(img, rect.X + (rect.Width - szf.Width) / 2, _
                        rect.Y + (rect.Height - szf.Height) / 2, _
                        szf.Width, szf.Height)
  End Sub

  Private Sub Panel1_Paint(ByVal sender As Object, _
           ByVal e As System.Windows.Forms.PaintEventArgs) _
           Handles Panel1.Paint
    If Not _imgl Is Nothing Then
      Dim sz As New Size(Panel1.Width, Panel1.Height)
      Dim p As New Point(0, 0)
      Dim rect As New Rectangle(p, sz)
      Dim grfx As Graphics = e.Graphics
      ScaleImageIsotropically(grfx, _imgl, rect)
    End If
  End Sub

  Private Sub Panel2_Paint(ByVal sender As Object, _
          ByVal e As System.Windows.Forms.PaintEventArgs) _
          Handles Panel2.Paint
    If Not _imgr Is Nothing Then
      Dim sz As New Size(Panel2.Width, Panel2.Height)
      Dim p As New Point(0, 0)
      Dim rect As New Rectangle(p, sz)
      Dim grfx As Graphics = e.Graphics
      ScaleImageIsotropically(grfx, _imgr, rect)
    End If
  End Sub

#End Region

  Private Sub Panel1_Resize(ByVal sender As Object, _
          ByVal e As System.EventArgs) Handles Panel1.Resize
    Panel1.Invalidate()
  End Sub

  Private Sub Panel2_Resize(ByVal sender As Object, _
          ByVal e As System.EventArgs) Handles Panel2.Resize
    Panel2.Invalidate()
  End Sub
End Class
 
Weitere Links zum Thema
Grafik drehen und spiegeln
Grafik in beliebigem Winkel drehen

Windows-Version
98/SE
ME
NT
2000
XP
Vista
Win 7


Download  (55,2 kB) Downloads bisher: [ 1394 ]

Vorheriger Tipp Zum Seitenanfang Nächster Tipp

Startseite | Tipps | Projekte | Tutorials | Bücherecke | VB-/VBA-Tipps | API-Referenz | Komponenten | VB.Net-Forum | VB/VBA-Forum | DirectX-Forum | Foren-Archiv | DirectX | Chat | Spielplatz | Links | Suchen | Stichwortverzeichnis | Feedback | Impressum

Seite empfehlen Bug-Report
Letzte Aktualisierung: Sonntag, 22. Januar 2012