vb@rchiv
VB Classic
VB.NET
ADO.NET
VBA
C#
Brandneu! sevEingabe v3.0 - Das Eingabecontrol der Superlative!  
 vb@rchiv Quick-Search: Suche startenErweiterte Suche starten   Impressum  | Datenschutz  | vb@rchiv CD Vol.6  | Shop Copyright ©2000-2024
 
zurück

 Sie sind aktuell nicht angemeldet.Funktionen: Einloggen  |  Neu registrieren  |  Suchen

VB.NET - Ein- und Umsteiger
ControlPaint (Rubberband) zeichnet an falscher Stelle 
Autor: Bazi
Datum: 24.01.23 22:03

Hallo,
in einem Programm möchte ich einen Bildausschnitt wählen und habe dafür folgenden Code eingebaut:
' Global definierte Variablen
Friend ptScreenDown As System.Drawing.Point                  ' zum zeichnen des 
' Auswahlrahmens
Friend PtScreenLast As System.Drawing.Point                  ' zum zeichnen des 
' Auswahlrahmens
 
' in Form definierte Variable:
Dim bHaveMouse As Boolean                                    ' wird bei 
' MouseDown gesetzt und bei MouseUp wieder aufgehoben
und folgender abgespeckter Code:
 Private Sub PdfViewer_MouseDown(ByVal sender As Object, ByVal e As _
   System.Windows.Forms.MouseEventArgs) Handles PdfViewer1.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Left Then
            bHaveMouse = True ' Maus ist gedrückt
            PmDataSet.ptScreenDown = Cursor.Position
        End If
    End Sub
    Private Sub PdfViewer_MouseMove(ByVal sender As Object, ByVal e As _
      System.Windows.Forms.MouseEventArgs) Handles PdfViewer1.MouseMove
        If e.Button = Windows.Forms.MouseButtons.Left Then
            If (PmDataSet.PtScreenLast.X <> -1) Then ' wenn schon gezeichnet war , 
                DrawReversibleRectangle(PmDataSet.ptScreenDown, _
                  PmDataSet.PtScreenLast) ' hebt die Zeichnung auf
            End If
            PmDataSet.PtScreenLast = Cursor.Position ' aktuelle Position merken
            DrawReversibleRectangle(PmDataSet.ptScreenDown, _
              PmDataSet.PtScreenLast) ' Rechteck zeichnen
        End If
    End Sub
    Private Sub PdfViewer_MouseUp(ByVal sender As Object, ByVal e As _
      System.Windows.Forms.MouseEventArgs) Handles PdfViewer1.MouseUp
        If e.Button = MouseButtons.Left Then
            bHaveMouse = False ' Maus nicht mehr gedrückt
            PmDataSet.ptScreenDown = New Point(-1, -1)
            PmDataSet.PtScreenLast = New Point(-1, -1)
        End If
    End Sub
    Private Sub DrawReversibleRectangle(ByVal p1 As Point, ByVal p2 As Point)
        Dim rc As Rectangle
        rc.X = Math.Min(p1.X, p2.X)
        rc.Y = Math.Min(p1.Y, p2.Y)
        rc.Width = Math.Max(p1.X, p2.X) - rc.X
        rc.Height = Math.Max(p1.Y, p2.Y) - rc.Y
        ControlPaint.DrawReversibleFrame(rc, Color.DarkBlue, FrameStyle.Dashed)
    End Sub
Es wird ein Rechteck gezeichnet, aber nicht an der richtigen Stelle.
Ich hatte vorher anstatt Cursor.Position ein PdfViewer1.PointToScreen(e.Location) drin, hat auch nicht gestimmt.
Und wenn ich die Form auf den 2. Bildschirm verschiebe dann wird trotzdem auf dem 1.Schirm gezeichnet.

Wo ist mein Denkfehler?

Danke für die Hilfe

Gruß Christian

Themenbaum einblendenGesamtübersicht  |  Zum Thema  |  Suchen

Re: ControlPaint (Rubberband) zeichnet an falscher Stelle 
Autor: Dideldum
Datum: 15.03.23 21:12

Hi,
ich hatte das Problem auch erst unlängst.
Und zwar, wenn die Picbox grösser ist, als das hinein geladene Image.
Also habe ich meine PicBox nach dem Bild laden an die Grösse des Bilds angepasst (in Abhängigkeit der für die Picbox zur Verfügung stehenden maximalen Fläche des ToolStripContainers in welchem die PicBox plaziert ist)
Seither passt der Rubberband-Ausschnitt.

    Private Sub PictureBoxAnpassen()
        Dim tmpString As String = ""
 
        Dim bildWidth As Integer = tsc_PicEditor_PictureBox.Image.Width
        Dim bildHeight As Integer = tsc_PicEditor_PictureBox.Image.Height
        tsc_PicEditor_PictureBox.Left = 0
        tsc_PicEditor_PictureBox.Top = 0
        tsc_PicEditor_PictureBox.Width = tsc_PicEditor_Panel.Width
        tsc_PicEditor_PictureBox.Height = tsc_PicEditor_Panel.Height
 
        Select Case MainformPicSizeMode ' Auswahl der vier Darstellungsmodi
            Case = 0 ' normal
                tsc_PicEditor_PictureBox.SizeMode = PictureBoxSizeMode.Normal
                tmpString = "Originalgrösse"
 
                tsc_PicEditor_PictureBox.Left = 0
                tsc_PicEditor_PictureBox.Top = 0
                If ((tsc_PicEditor_PictureBox.Image.Width > _
                  tsc_PicEditor_Panel.Width) Or ( _
                  tsc_PicEditor_PictureBox.Image.Height > _
                  tsc_PicEditor_Panel.Height)) 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.Width = _
                  tsc_PicEditor_PictureBox.Image.Width
                tsc_PicEditor_PictureBox.Height = _
                tsc_PicEditor_PictureBox.Image.Height
 
            Case = 1 ' center
                tsc_PicEditor_PictureBox.SizeMode = _
                  PictureBoxSizeMode.CenterImage
                tmpString = "Originalgrösse (zentriert)"
 
                tsc_PicEditor_PictureBox.Anchor = AnchorStyles.Top Or _
                  AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right
                If tsc_PicEditor_PictureBox.Width > _
                tsc_PicEditor_PictureBox.Image.Width Then
                    tsc_PicEditor_PictureBox.Left = (tsc_PicEditor_Panel.Width _
                    - tsc_PicEditor_PictureBox.Image.Width) / 2
                    tsc_PicEditor_PictureBox.Width = _
                    tsc_PicEditor_PictureBox.Image.Width
                Else
                    tsc_PicEditor_PictureBox.Left = 0
                    tsc_PicEditor_PictureBox.Width = tsc_PicEditor_Panel.Width
                End If
                If tsc_PicEditor_PictureBox.Height > _
                  tsc_PicEditor_PictureBox.Image.Height Then
                    tsc_PicEditor_PictureBox.Top = (tsc_PicEditor_Panel.Height _
                    - tsc_PicEditor_PictureBox.Image.Height) / 2
                    tsc_PicEditor_PictureBox.Height = _
                    tsc_PicEditor_PictureBox.Image.Height
                Else
                    tsc_PicEditor_PictureBox.Top = 0
                    tsc_PicEditor_PictureBox.Height = tsc_PicEditor_Panel.Height
                End If
 
...
Fortsetzung folgt...
Themenbaum einblendenGesamtübersicht  |  Zum Thema  |  Suchen

Re: ControlPaint (Rubberband) zeichnet an falscher Stelle 
Autor: Dideldum
Datum: 15.03.23 21:12

Fortsetzung:
           Case = 2 ' Zoom
                tsc_PicEditor_PictureBox.SizeMode = PictureBoxSizeMode.Zoom
                tmpString = "Anzeigegrösse (Verhältnis beibehalten)"
 
 
                tsc_PicEditor_PictureBox.Anchor = AnchorStyles.None
 
                Dim panelVerhältnis As Single = tsc_PicEditor_PictureBox.Width _
                  / tsc_PicEditor_PictureBox.Height
                Dim BildVerhältnis As Single = _
                tsc_PicEditor_PictureBox.Image.Width / _
                tsc_PicEditor_PictureBox.Image.Height
 
                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.Width = _
                              tsc_PicEditor_Panel.Width
                            tsc_PicEditor_PictureBox.Height = _
                            tsc_PicEditor_Panel.Width / _
                            tsc_PicEditor_PictureBox.Image.Width * _
                            tsc_PicEditor_PictureBox.Image.Height
                            tsc_PicEditor_PictureBox.Top = ( _
                            tsc_PicEditor_Panel.Height - _
                            tsc_PicEditor_PictureBox.Height) / 2
                        Else
                            ' Bild breiter als Panel
                            tsc_PicEditor_PictureBox.Height = _
                              tsc_PicEditor_Panel.Height
                            tsc_PicEditor_PictureBox.Width = bildWidth / ( _
                            bildHeight / tsc_PicEditor_Panel.Height)
                            tsc_PicEditor_PictureBox.Left = ( _
                            tsc_PicEditor_Panel.Width - _
                            tsc_PicEditor_PictureBox.Width) / 2
                        End If
                    Else
                        tsc_PicEditor_PictureBox.Height = _
                          tsc_PicEditor_Panel.Height
                        tsc_PicEditor_PictureBox.Width = _
                        tsc_PicEditor_Panel.Height / _
                        tsc_PicEditor_PictureBox.Image.Height * _
                        tsc_PicEditor_PictureBox.Image.Width
                        tsc_PicEditor_PictureBox.Left = ( _
                        tsc_PicEditor_Panel.Width - _
                        tsc_PicEditor_PictureBox.Width) / 2
                    End If
                Else
                    If BildVerhältnis > 1 Then
                        tsc_PicEditor_PictureBox.Width = _
                          tsc_PicEditor_Panel.Width
                        tsc_PicEditor_PictureBox.Height = _
                        tsc_PicEditor_Panel.Width / _
                        tsc_PicEditor_PictureBox.Image.Width * _
                        tsc_PicEditor_PictureBox.Image.Height
                        tsc_PicEditor_PictureBox.Top = ( _
                        tsc_PicEditor_Panel.Height - _
                        tsc_PicEditor_PictureBox.Height) / 2
                    Else
                        If panelVerhältnis < BildVerhältnis Then
                            tsc_PicEditor_PictureBox.Width = _
                              tsc_PicEditor_Panel.Width
                            tsc_PicEditor_PictureBox.Height = _
                            tsc_PicEditor_Panel.Width / _
                            tsc_PicEditor_PictureBox.Image.Width * _
                            tsc_PicEditor_PictureBox.Image.Height
                            tsc_PicEditor_PictureBox.Top = ( _
                            tsc_PicEditor_Panel.Height - _
                            tsc_PicEditor_PictureBox.Height) / 2
                        Else
                            tsc_PicEditor_PictureBox.Height = _
                              tsc_PicEditor_Panel.Height
                            tsc_PicEditor_PictureBox.Width = _
                            tsc_PicEditor_Panel.Height / _
                            tsc_PicEditor_PictureBox.Image.Height * _
                            tsc_PicEditor_PictureBox.Image.Width
                            tsc_PicEditor_PictureBox.Left = ( _
                            tsc_PicEditor_Panel.Width - _
                            tsc_PicEditor_PictureBox.Width) / 2
                        End If
                    End If
                End If
 
            Case = 3 ' Stretch
                tsc_PicEditor_PictureBox.SizeMode = _
                  PictureBoxSizeMode.StretchImage
                tsc_PicEditor_PictureBox.Anchor = AnchorStyles.Top Or _
                AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right
 
                tmpString = "Anzeigegrösse (Verhältnis ignorieren)"
 
        End Select
 
        If tsc_PicEditor_PictureBox.Image IsNot Nothing Then
            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"
        End If
    End Sub
Vielleicht hilft es ja

Beste Grüsse

Beitrag wurde zuletzt am 15.03.23 um 21:14:11 editiert.
Themenbaum einblendenGesamtübersicht  |  Zum Thema  |  Suchen

Re: ControlPaint (Rubberband) zeichnet an falscher Stelle 
Autor: Dideldum
Datum: 16.03.23 14:59

P.S.

Hier mein Rubberband-Code für das Bild-Beschneiden

    Dim editPicEditor As Boolean
    Dim hatRubberBand As Boolean
 
    ' Rubberband
    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 tsc_PicEditor_PictureBox_MouseEnter(sender As Object, e As _
      EventArgs) Handles tsc_PicEditor_PictureBox.MouseEnter
        If editPicEditor = True Then
            tsc_PicEditor.Cursor = Cursors.Cross
        End If
    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
            cropWidth = 0
            cropHeight = 0
            hatRubberBand = False
            If e.Button = Windows.Forms.MouseButtons.Left Then
                If editPicEditor = True Then
                    cropX = e.X
                    cropY = e.Y
 
                    cropPen = New Pen(cropPenColor, cropPenSize)
                    cropPen.DashStyle = DashStyle.DashDotDot
                End If
            End If
            tsc_PicEditor_PictureBox.Refresh()
        Catch exc As Exception
        End Try
        update_Menu_PicEditor()
    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
                If editPicEditor = True Then
                    hatRubberBand = True
                    tsc_PicEditor_PictureBox.Refresh()
                    mouseMovePosX = e.X
                    mouseMovePosY = e.Y
                    cropWidth = e.X - cropX
                    cropHeight = e.Y - cropY
                    tsc_PicEditor_PictureBox.CreateGraphics.DrawRectangle( _
                      cropPen, cropX, cropY, cropWidth, cropHeight)
                End If
            End If
            update_Statusleiste_PicEditor()
        Catch exc As 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
        update_Menu_PicEditor()
    End Sub
 
 
    Private Sub mnu_PicEditor_Bearbeiten_Beschneiden_Click(sender As Object, e _
      As EventArgs) Handles mnu_PicEditor_Bearbeiten_Beschneiden.Click
        Bild_Beschneiden()
    End Sub
 
 
    Private Sub Bild_Beschneiden()
        Try
 
            If cropWidth < 1 Then
                Exit Sub
            End If
 
            tsc_PicEditor_PictureBox.Visible = False
 
            Dim rect As Rectangle = New Rectangle(cropX, cropY, cropWidth, _
              cropHeight)
            Dim bit As Bitmap = 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
            PictureBoxAnpassen()
 
            tsc_PicEditor_PictureBox.Visible = True
        Catch exc As Exception
        End Try
        hatRubberBand = False
        PicEditorGeändert = True
        update_Menu_PicEditor()
    End Sub
Themenbaum einblendenGesamtübersicht  |  Zum Thema  |  Suchen

Sie sind nicht angemeldet!
Um auf diesen Beitrag zu antworten oder neue Beiträge schreiben zu können, müssen Sie sich zunächst anmelden.

Einloggen  |  Neu registrieren

Funktionen:  Zum Thema  |  GesamtübersichtSuchen 

nach obenzurück
 
   

Copyright ©2000-2024 vb@rchiv Dieter Otter
Alle Rechte vorbehalten.
Microsoft, Windows und Visual Basic sind entweder eingetragene Marken oder Marken der Microsoft Corporation in den USA und/oder anderen Ländern. Weitere auf dieser Homepage aufgeführten Produkt- und Firmennamen können geschützte Marken ihrer jeweiligen Inhaber sein.

Diese Seiten wurden optimiert für eine Bildschirmauflösung von mind. 1280x1024 Pixel