|
Tipp 0284
|
Grafik an PictureBox anpassen und zentrieren
|
|
|
Autor/Einsender: Datum: |
|
Angie 01.11.2002 |
|
Entwicklungsumgebung: |
|
VB 5 |
|
|
Dieser Tipp zeigt, wie man die Größe einer Grafik proportional an eine PictureBox anpassen kann, und wie diese Grafik dann auch in der PictureBox zentriert wird.
|
|
|
Option Explicit
Private Sub Form_Load()
With picQuelle
.Visible = False
.AutoSize = True
.AutoRedraw = True
End With
With picZiel
.AutoRedraw = True
End With
On Error Resume Next
picQuelle.Picture = LoadPicture(App.Path & "\" & "VBfun.gif")
ResizeGraficToPicBox picQuelle, picZiel
End Sub
Private Sub ResizeGraficToPicBox(ByVal vpicQuelle As PictureBox, _
ByVal vpicZiel As PictureBox)
Dim sngRatio As Single
Dim sngWidth As Single
Dim sngHeight As Single
Dim sngPosX As Single
Dim sngPosY As Single
vpicZiel.Cls
If optGroesse(0) Then
sngWidth = vpicQuelle.ScaleWidth
sngHeight = vpicQuelle.ScaleHeight
If vpicQuelle.ScaleWidth > vpicZiel.ScaleWidth Or _
vpicQuelle.ScaleHeight > vpicZiel.ScaleHeight Then
sngPosX = 0
sngPosY = 0
Else
sngPosX = (vpicZiel.ScaleWidth - sngWidth) / 2
sngPosY = (vpicZiel.ScaleHeight - sngHeight) / 2
End If
Else
sngRatio = vpicQuelle.ScaleWidth / vpicQuelle.ScaleHeight
sngWidth = vpicZiel.ScaleWidth
sngHeight = vpicZiel.ScaleHeight
If (sngWidth / sngHeight) > sngRatio Then
sngWidth = sngRatio * sngHeight
Else
sngHeight = sngWidth / sngRatio
End If
sngPosX = (vpicZiel.ScaleWidth - sngWidth) / 2
sngPosY = (vpicZiel.ScaleHeight - sngHeight) / 2
End If
vpicZiel.PaintPicture vpicQuelle.Picture, _
sngPosX, sngPosY, sngWidth, sngHeight
End Sub
Private Sub optGroesse_Click(Index As Integer)
ResizeGraficToPicBox picQuelle, picZiel
End Sub
Private Sub cmdGrafikLaden_Click()
Dim strFilter As String
CommonDialog1.CancelError = True
On Error GoTo ErrHandler
strFilter = "Alle Grafiken (*.gif;*.jpg;*.ico;*.bmp;*.wmf)" & _
"|*.gif;*.jpg;*.ico;*.bmp;*.wmf|"
strFilter = strFilter & "GIF Files (*.gif)|*.gif|"
strFilter = strFilter & "JPEG Files (*.jpg)|*.jpg|"
strFilter = strFilter & "Icon Files (*.ico)|*.ico|"
strFilter = strFilter & "Windows Bitmap (*.bmp)|*.bmp|"
strFilter = strFilter & "Windows Meta File (*.wmf)|.wmf"
With CommonDialog1
.Filter = strFilter
.DialogTitle = "Grafik laden"
.ShowOpen
If Len(.FileName) <> 0 Then
picQuelle.Picture = LoadPicture(.FileName)
ResizeGraficToPicBox picQuelle, picZiel
End If
End With
On Error GoTo 0
Exit Sub
ErrHandler:
End Sub
|
|
|
|
|
Windows-Version |
95 |
|
|
98/SE |
|
|
ME |
|
|
NT |
|
|
2000 |
|
|
XP |
|
|
Vista |
|
|
Win
7 |
|
|
|
VB-Version |
VBA 5 |
|
|
VBA 6 |
|
|
VB 4/16 |
|
|
VB 4/32 |
|
|
VB 5 |
|
|
VB 6 |
|
|
|
|
Download (3,2 kB)
|
Downloads bisher: [ 2408 ]
|
|
|