Results 1 to 9 of 9

Thread: Bug in DrawImageUnscaledAndClipped?

Threaded View

  1. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: Bug in DrawImageUnscaledAndClipped?

    From your description it sounds like you might be misinterpreting exactly what the code is doing, although since we don’t see your code, we have to do some guessing ourselves.
    Using the drawimage with destination rectangle and source rectangle isn’t so much clipping as it is selecting what portion of the source image to draw to the destination rectangle. If the source and destination rectangles are the same size, then there won’t be any scaling of the portion of the image copied. But if the source rectangle is smaller, then the image will be expanded and if the source rectangle is larger, then the image will be shrunk.
    Perhaps it won’t matter in your application, but using the dst,src rectangle version of drawimage is one of the slowest versions of drawimage. I would have to test, and it might depend on the size of the source drawing, but another option would be to setup a clipping rectangle, and offset where you draw the second image to accomplish the same result, which might fit your original conception, but since we don’t have an example of what the code looked like you were trying to use, I can’t say for sure.
    I'll post a quick example that just allows dragging a rectangle around an image and see the area under the rectangle being drawn in another picturebox. You move the rectangle just by using the left mouse. Dragging on the rectangle with the Right mouse button will resize the source rectangle. You can see what the result of moving and resizing the source rectangle has on the destination rectangle. The 2010 solution is attached in a zip file, but since there is not too much code, I'll paste the code here. If you don't want to download, or have an older version of VB.net, you can just create a new project, add two pictureboxes, #1 should be fairly small, say 100x100 and #2 should hold a reasonable size image (say 800x600 or larger) to drag around on.
    Code:
    Public Class Form1
      Dim srcRect, dstRect As Rectangle
      Dim srcRectBrush As New SolidBrush(Color.FromArgb(32, Color.Black))
    
      Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        srcRect = PictureBox1.ClientRectangle  'initialized the source rectangle to match our destination so no scaling of the image occurs
        dstRect = srcRect 'initialized the dstRect as well, but we won't be changing its size, it will always fill the destination picturebox
      End Sub
    
      Private Sub PictureBox2_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseMove
        Static lp As Point, cp As Point    'lp = the last mouseMove position, cp will be the current mouse position  
    
        If srcRect.Contains(lp) Then                                'if the mouse is inside the source rectangle
          cp = e.Location                                           '  save the position in a short named variable for convenience
          Dim dx As Integer = cp.X - lp.X                           '  Calculate the distance moved in X and Y
          Dim dy As Integer = cp.Y - lp.Y                           '  since the last mouseMove event
    
          If e.Button = Windows.Forms.MouseButtons.Left Then        '  If the left button is down, we want to move the source rectangle
            srcRect.X += dx                                         '    Update the X position
            srcRect.Y += dy                                         '    Update the Y position
            PictureBox2.Invalidate()                                '    Invalidate our source picturebox so it draws the rectangle in the new position
            PictureBox1.Invalidate()                                '    Invalidate our destination picturbox so it updates its image from the new source position
    
          ElseIf e.Button = Windows.Forms.MouseButtons.Right Then   '  Else if the right button is down, we want to change the size of the source rectangle
            Dim newWidth As Integer = srcRect.Width + dx            '     Determine the new width
            Dim newHeight As Integer = srcRect.Height + dy          '     Determine the new height
            Dim redraw As Boolean                                   '     Declare a flag so we only redraw if we actually change something
    
            If newWidth > 10 And newWidth < 500 Then                '     If the new width is within our desired min/max size Then
              srcRect.Width = newWidth                              '       Update the source rectangle width
              redraw = True                                         '       flag that we want to redraw the pictureboxes
            End If                                                  '     End If
            If newHeight > 10 And newHeight < 500 Then              '     If the new height is within our desired min/max size Then
              srcRect.Height = newHeight                            '       Update the source rectangle height
              redraw = True                                         '       flag that we want to redraw the pictureboxes
            End If                                                  '     End If
            If redraw Then                                          '     If the redraw flag is true Then
              PictureBox2.Invalidate()                              '       Redraw our pictureboxes
              PictureBox1.Invalidate()
            End If
          End If
        End If
        lp = e.Location                                             'save the current position of the mouse for the next time
      End Sub
    
      Private Sub PictureBox2_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles PictureBox2.Paint
        e.Graphics.FillRectangle(srcRectBrush, srcRect)
      End Sub
    
      Private Sub PictureBox1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        e.Graphics.DrawImage(PictureBox2.Image, dstRect, srcRect, GraphicsUnit.Pixel)
      End Sub
    End Class
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width