Hi Ihr,
nachdem ich einige Zeit damit verbracht habe, Bilder in meiner Anwendung stets passend in einem Parent-Container und mit möglicher Auswahl vom PicSizeMode Normal, Center, Zoom und Stretch anzeigen zu können, hier mein Script:
Vielleicht kann es ja mal jemand brauchen.
Das Bild resized automatisch beim Resizen der Mainform, wenn das Tsc_PicEditor an dieser verankert ist.
Ich bitte um Nachsicht, wenn es nicht perfekt gecodet ist, bin aber noch nicht so lange mit VB.Net unterwegs.
Benötigt werden:
- Ein ToolStripControl als Container für die PicBox mit Namen "Tsc_PicEditor"
- Eine PictureBox im CenterPanel des o.g. Tsc_PicEditor mit Namen "PictureBox"
- Fünf Buttons für MainformPicSizeMode Normal, Center, Zoom und Stretch sowie Bild Beschneiden
- Vier Buttons für Links und Rechts drehen, sowie Horizontal und Vertikal spiegeln
nachdem ich einige Zeit damit verbracht habe, Bilder in meiner Anwendung stets passend in einem Parent-Container und mit möglicher Auswahl vom PicSizeMode Normal, Center, Zoom und Stretch anzeigen zu können, hier mein Script:
Vielleicht kann es ja mal jemand brauchen.
Das Bild resized automatisch beim Resizen der Mainform, wenn das Tsc_PicEditor an dieser verankert ist.
Ich bitte um Nachsicht, wenn es nicht perfekt gecodet ist, bin aber noch nicht so lange mit VB.Net unterwegs.
Benötigt werden:
- Ein ToolStripControl als Container für die PicBox mit Namen "Tsc_PicEditor"
- Eine PictureBox im CenterPanel des o.g. Tsc_PicEditor mit Namen "PictureBox"
- Fünf Buttons für MainformPicSizeMode Normal, Center, Zoom und Stretch sowie Bild Beschneiden
- Vier Buttons für Links und Rechts drehen, sowie Horizontal und Vertikal spiegeln
VB.NET-Quellcode
- Dim MainformPicSizeMode as integer = 0
- ' Rubberband
- Dim inRubberBand As Boolean = False
- dim clearImage as Boolean = false
- Dim cropX As Integer
- Dim cropY As Integer
- Dim cropWidth As Integer
- Dim cropHeight As Integer
- Dim mousePosX As Integer
- Dim mousePosY As Integer
- Dim mouseMovePosX As Integer
- Dim mouseMovePosY As Integer
- Dim oCropX As Integer
- Dim oCropY As Integer
- Dim cropBitmap As Bitmap
- Public cropPen As Pen
- Public cropPenSize As Integer = 2
- Public cropDashStyle As Drawing2D.DashStyle = Drawing2D.DashStyle.Solid
- Public cropPenColor As Color = Color.Red
- Private Sub StateChangedForm_LocationChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LocationChanged
- FormWindowState_Changed(False)
- End Sub
- Private Sub StateChangedForm_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.SizeChanged
- FormWindowState_Changed(True)
- End Sub
- Private Sub FormWindowState_Changed(grössenänderung As Boolean)
- Select Case Me.WindowState
- Case = FormWindowState.Normal
- If grössenänderung = True andalso tsc_PicEditor.Visible = True Then
- resize_PicEditor()
- End If
- Case = FormWindowState.Maximized
- If grössenänderung = True andalso tsc_PicEditor.Visible = True Then
- tsc_PicEditor_PictureBox.Visible = False
- resize_PicEditor()
- End If
- End Select
- End Sub
- Private Sub Tsc_PicEditor_PictureBox_Paint(sender As Object, e As PaintEventArgs) Handles tsc_PicEditor_PictureBox.Paint
- If clearImage Then
- clearImage = False
- e.Graphics.Clear(Color.White)
- End If
- End Sub
- Private Sub Tsc_PicEditor_PictureBox_MouseEnter(sender As Object, e As EventArgs) Handles tsc_PicEditor_PictureBox.MouseEnter
- tsc_PicEditor.Cursor = Cursors.Cross
- End Sub
- Private Sub Tsc_PicEditor_PictureBox_MouseLeave(sender As Object, e As EventArgs) Handles tsc_PicEditor_PictureBox.MouseLeave
- tsc_PicEditor.Cursor = Cursors.Default
- End Sub
- Private Sub Tsc_PicEditor_PictureBox_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tsc_PicEditor_PictureBox.MouseDown
- Try
- cropPenColor = My.Settings.PicEditor_RubberbandFarbe
- cropPenSize = My.Settings.PicEditor_RubberbandSize
- cropWidth = 0
- cropHeight = 0
- inRubberBand = False
- If e.Button = Windows.Forms.MouseButtons.Left Then
- cropX = e.X
- cropY = e.Y
- cropPen = New Pen(cropPenColor, cropPenSize) With {
- .DashStyle = DashStyle.DashDotDot
- }
- End If
- tsc_PicEditor_PictureBox.Refresh()
- Catch exc As System.Exception
- End Try
- End Sub
- Private Sub Tsc_PicEditor_PictureBox_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tsc_PicEditor_PictureBox.MouseMove
- Try
- If tsc_PicEditor_PictureBox.Image Is Nothing Then Exit Sub
- mousePosX = e.X
- mousePosY = e.Y
- If e.Button = Windows.Forms.MouseButtons.Left Then
- tsc_PicEditor_PictureBox.Refresh()
- If e.X <= tsc_PicEditor_PictureBox.Width - My.Settings.PicEditor_RubberbandSize Then
- cropWidth = e.X - cropX
- mouseMovePosX = e.X
- Else
- mouseMovePosX = tsc_PicEditor_PictureBox.Width
- cropWidth = tsc_PicEditor_PictureBox.Width - cropX - My.Settings.PicEditor_RubberbandSize
- End If
- If e.Y <= tsc_PicEditor_PictureBox.Height - My.Settings.PicEditor_RubberbandSize Then
- cropHeight = e.Y - cropY
- mouseMovePosY = e.Y
- Else
- mouseMovePosY = tsc_PicEditor_PictureBox.Height
- cropHeight = tsc_PicEditor_PictureBox.Height - cropY - My.Settings.PicEditor_RubberbandSize
- End If
- inRubberBand = True
- tsc_PicEditor_PictureBox.CreateGraphics.DrawRectangle(cropPen, cropX, cropY, cropWidth, cropHeight)
- End If
- System.Windows.Forms.Application.DoEvents()
- Catch exc As System.Exception
- If Err.Number = 5 Then Exit Sub
- End Try
- End Sub
- Private Sub Tsc_PicEditor_PictureBox_MouseUp(sender As Object, e As MouseEventArgs) Handles tsc_PicEditor_PictureBox.MouseUp
- inRubberBand = False
- End Sub
- Private Sub Bild_LadenUndAnzeigen(name As String)
- tsc_PicEditor_PictureBox.Visible = False
- Using fs As New System.IO.FileStream(name, IO.FileMode.Open)
- tsc_PicEditor_PictureBox.Image = New Bitmap(Image.FromStream(fs))
- End Using
- resize_PicEditor()
- inRubberBand = False
- End Sub
- Private Sub Bild_Normal()
- tsc_PicEditor_PictureBox.Visible = False
- MainformPicSizeMode = 0
- resize_PicEditor()
- End Sub
- Private Sub Bild_Center()
- tsc_PicEditor_PictureBox.Visible = False
- MainformPicSizeMode = 1
- resize_PicEditor()
- End Sub
- Private Sub Bild_Zoom()
- tsc_PicEditor_PictureBox.Visible = False
- MainformPicSizeMode = 2
- resize_PicEditor()
- End Sub
- Private Sub Bild_Stretch()
- tsc_PicEditor_PictureBox.Visible = False
- MainformPicSizeMode = 3
- resize_PicEditor()
- End Sub
- Private Sub Bild_LinksDrehen()
- Dim Bitmap As Image
- tsc_PicEditor_PictureBox.Visible = False
- Bitmap = tsc_PicEditor_PictureBox.Image
- Bitmap.RotateFlip(RotateFlipType.Rotate270FlipNone)
- tsc_PicEditor_PictureBox.Image = Bitmap
- resize_PicEditor()
- End Sub
- Private Sub Bild_RechtsDrehen()
- Dim Bitmap As Image
- tsc_PicEditor_PictureBox.Visible = False
- Bitmap = tsc_PicEditor_PictureBox.Image
- Bitmap.RotateFlip(RotateFlipType.Rotate90FlipNone)
- tsc_PicEditor_PictureBox.Image = Bitmap
- resize_PicEditor()
- End Sub
- Private Sub Bild_VertikalSpiegeln()
- Dim Bitmap As Image
- tsc_PicEditor_PictureBox.Visible = False
- Bitmap = tsc_PicEditor_PictureBox.Image
- Bitmap.RotateFlip(RotateFlipType.Rotate180FlipY)
- tsc_PicEditor_PictureBox.Image = Bitmap
- resize_PicEditor()
- End Sub
- Private Sub Bild_HorizontalSpiegeln()
- Dim Bitmap As Image
- tsc_PicEditor_PictureBox.Visible = False
- Bitmap = tsc_PicEditor_PictureBox.Image
- Bitmap.RotateFlip(RotateFlipType.Rotate180FlipX)
- tsc_PicEditor_PictureBox.Image = Bitmap
- resize_PicEditor()
- End Sub
- Private Sub Bild_Beschneiden()
- Try
- If cropWidth < 1 Then
- Exit Sub
- End If
- tsc_PicEditor_PictureBox.Visible = False
- Dim rect As New Rectangle(cropX, cropY, cropWidth, cropHeight)
- Dim bit As New Bitmap(tsc_PicEditor_PictureBox.Image, tsc_PicEditor_PictureBox.Width, tsc_PicEditor_PictureBox.Height)
- cropBitmap = New Bitmap(cropWidth, cropHeight)
- Dim g As Graphics = Graphics.FromImage(cropBitmap)
- g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
- g.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
- g.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
- g.DrawImage(bit, 0, 0, rect, GraphicsUnit.Pixel)
- tsc_PicEditor_PictureBox.Image = cropBitmap
- resize_PicEditor()
- Catch exc As System.Exception
- End Try
- inRubberBand = False
- End Sub
- Private Sub resize_PicEditor()
- Dim tmpString As String = ""
- tsc_PicEditor_PictureBox.SuspendLayout()
- If tsc_PicEditor_PictureBox.Image IsNot Nothing Then
- tsc_PicEditor.Width = spc_DateienInhalte.Panel2.Width
- tsc_PicEditor.Height = spc_DateienInhalte.Panel2.Height
- tsc_PicEditor_Panel.Width = tsc_PicEditor.Width - 2
- Dim bildWidth As Integer = tsc_PicEditor_PictureBox.Image.Width
- Dim bildHeight As Integer = tsc_PicEditor_PictureBox.Image.Height
- Dim panelWidth As Integer = tsc_PicEditor_Panel.Width
- Dim panelHeight As Integer = tsc_PicEditor_Panel.Height
- Select Case MainformPicSizeMode
- Case = 0 ' normal
- tmpString = "Originalgrösse"
- If ((bildWidth > panelWidth) Or (bildHeight > panelHeight)) Then
- tsc_PicEditor_PictureBox.Anchor = AnchorStyles.Top Or AnchorStyles.Left
- Else
- tsc_PicEditor_PictureBox.Anchor = AnchorStyles.Top Or AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right
- End If
- tsc_PicEditor_PictureBox.SizeMode = PictureBoxSizeMode.Normal
- tsc_PicEditor_PictureBox.Left = 0
- tsc_PicEditor_PictureBox.Top = 0
- tsc_PicEditor_PictureBox.Width = bildWidth
- tsc_PicEditor_PictureBox.Height = bildHeight
- Case = 1 ' center
- tmpString = "Originalgrösse (zentriert)"
- tsc_PicEditor_PictureBox.Anchor = AnchorStyles.Top Or AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right
- tsc_PicEditor_PictureBox.SizeMode = PictureBoxSizeMode.CenterImage
- If panelWidth > bildWidth Then
- tsc_PicEditor_PictureBox.Left = CInt((panelWidth - bildWidth) / 2)
- tsc_PicEditor_PictureBox.Width = bildWidth
- Else
- tsc_PicEditor_PictureBox.Left = 0
- tsc_PicEditor_PictureBox.Width = panelWidth
- End If
- If panelHeight > bildHeight Then
- tsc_PicEditor_PictureBox.Top = CInt((panelHeight - bildHeight) / 2)
- tsc_PicEditor_PictureBox.Height = bildHeight
- Else
- tsc_PicEditor_PictureBox.Top = 0
- tsc_PicEditor_PictureBox.Height = panelHeight
- End If
- Case = 2 ' Zoom
- tmpString = "Anzeigegrösse (Verhältnis beibehalten)"
- tsc_PicEditor_PictureBox.Anchor = AnchorStyles.None
- tsc_PicEditor_PictureBox.SizeMode = PictureBoxSizeMode.Zoom
- If panelWidth > 0 AndAlso panelHeight > 0 Then
- Dim panelVerhältnis As Single = CSng(panelWidth / panelHeight)
- Dim BildVerhältnis As Single = CSng(bildWidth / bildHeight)
- If panelVerhältnis > 1 Then
- ' Panel breiter als Hoch
- If BildVerhältnis > 1 Then
- ' Bild breiter als Hoch
- If panelVerhältnis < BildVerhältnis Then
- ' Bild schmaler als Panel
- tsc_PicEditor_PictureBox.Top = CInt((panelHeight - (panelWidth / bildWidth * bildHeight)) / 2)
- tsc_PicEditor_PictureBox.Left = 0
- tsc_PicEditor_PictureBox.Width = panelWidth - 2
- tsc_PicEditor_PictureBox.Height = CInt(panelWidth / bildWidth * bildHeight)
- Else
- ' Bild breiter als Panel
- tsc_PicEditor_PictureBox.Top = 0
- tsc_PicEditor_PictureBox.Left = CInt((panelWidth - (bildWidth / (bildHeight / panelHeight))) / 2)
- tsc_PicEditor_PictureBox.Height = panelHeight
- tsc_PicEditor_PictureBox.Width = CInt(bildWidth / (bildHeight / panelHeight))
- End If
- Else
- ' Bild höher als Breit
- ' Bild höher als Panel
- tsc_PicEditor_PictureBox.Top = 0
- tsc_PicEditor_PictureBox.Left = CInt((panelWidth - (panelHeight / bildHeight * bildWidth)) / 2)
- tsc_PicEditor_PictureBox.Height = panelHeight
- tsc_PicEditor_PictureBox.Width = CInt(panelHeight / bildHeight * bildWidth)
- End If
- Else
- ' Panel Höher als Breit
- If BildVerhältnis > 1 Then
- ' Bild breiter als hoch
- tsc_PicEditor_PictureBox.Top = CInt((panelHeight - (panelWidth / bildWidth * bildHeight)) / 2)
- tsc_PicEditor_PictureBox.Left = 0
- tsc_PicEditor_PictureBox.Width = panelWidth - 2
- tsc_PicEditor_PictureBox.Height = CInt(panelWidth / bildWidth * bildHeight)
- Else
- If panelVerhältnis < BildVerhältnis Then
- ' Bild höher als Panel
- tsc_PicEditor_PictureBox.Top = CInt((panelHeight - (panelWidth / bildWidth * bildHeight)) / 2)
- tsc_PicEditor_PictureBox.Left = 0
- tsc_PicEditor_PictureBox.Width = panelWidth - 2
- tsc_PicEditor_PictureBox.Height = CInt(panelWidth / bildWidth * bildHeight)
- Else
- ' Bild niedriger als Panel
- tsc_PicEditor_PictureBox.Top = 1
- tsc_PicEditor_PictureBox.Left = CInt((panelWidth - (panelHeight / bildHeight * bildWidth)) / 2)
- tsc_PicEditor_PictureBox.Height = panelHeight - 2
- tsc_PicEditor_PictureBox.Width = CInt(panelHeight / bildHeight * bildWidth)
- End If
- End If
- End If
- End If
- Case = 3 ' Stretch
- tmpString = "Anzeigegrösse (Verhältnis ignorieren)"
- tsc_PicEditor_PictureBox.Anchor = AnchorStyles.Top Or AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right
- tsc_PicEditor_PictureBox.SizeMode = PictureBoxSizeMode.StretchImage
- tsc_PicEditor_PictureBox.Left = 0
- tsc_PicEditor_PictureBox.Top = 0
- tsc_PicEditor_PictureBox.Width = panelWidth - 2
- tsc_PicEditor_PictureBox.Height = panelHeight
- End Select
- dateiinfo = tmpString & " - (" & Format(tsc_PicEditor_PictureBox.Image.PhysicalDimension.Width, "###,###,##0") & " x " & Format(tsc_PicEditor_PictureBox.Image.PhysicalDimension.Height, "###,###,##0") & " - Format: " & Chr(34) & tsc_PicEditor_PictureBox.Image.PixelFormat.ToString.Substring(6) & Chr(34) & ")"
- Else
- dateiinfo = "Pic-Viewer - Kein Bild geladen"
- End If
- tsc_PicEditor_PictureBox.Visible = True
- tsc_PicEditor_PictureBox.ResumeLayout()
- End Sub
Dieser Beitrag wurde bereits 3 mal editiert, zuletzt von „Dideldum“ ()