Cut image parts

  • 14 years ago

    Hi,

     

    How can I cut just some parts in an image? If somebody here know please.

  • 14 years ago

    Here is a start.  This code simply takes a small piece of an image.  I'm not exactly sure what your requirements are and my knowledge here is limited so I don't know if I'll be much help.

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'A rectangle that describes the area we want from our source image
            'In this I created a 30x30 square that begins at the top left
            Dim rec As New Rectangle(0, 0, 30, 30)
            'A bitmap object sized to match the size of our desired cutout
            'This image will hold our cut-out peace
            Dim bmp As New Bitmap(rec.Width, rec.Height)
            'A graphics object to draw the cut-out onto our bitmap object (bmp)
            Dim gr As Graphics = Graphics.FromImage(bmp)
    
            'I passed our source image (the image of PictureBox1) and the rectangle/piece I wanted
            gr.DrawImageUnscaledAndClipped(PictureBox1.Image, rec)
    
            'Cleanup
            gr.Dispose()
    
            'Display the piece in PictureBox2
            PictureBox2.Image = bmp
        End Sub
  • 14 years ago
    Okay, somehow I got back to playing around with cropping images.  I'd ignore the code I posted earlier.  It appears to work but doesn't really.  I realized it didn't work while working on the new code below.  All you need is a form with a picturebox named pbDraw.  Load an image into the picture box and make sure the sizemode is set to normal or AutoSize.  This is a must for the image cropping to work correctly.  Paste this code into your project and test it out.  I've commented it so you should be able to understand it.  The code was inspired by code that Nimpo had posted in this thread: http://www.developerfusion.co.uk/forums/thread/110179/.  I bookmarked it a while ago and decided to check it out.  It was a great help even though it didn't work perfectly for me.  I also learned how to successfully take a peace of an image from this web page: http://abstractvb.com/code.asp?A=1088  I'm not sure if it will help you but I want to give credit where credit is due.  Well enough talk, here is the code.  Hope it helps.
    'This is an example that allows a user to select a portion
    'of an image using a rectangular selection and crop that area
    Public Class Form1
        'The pen we'll use to draw our selection rectangle
        Dim PenDashed As New Pen(Color.White)
        'Drawing flag (If True draw selection rectangle)
        Dim Drawing As Boolean = False
        'Location mouse button was initially pressed
        Dim DrawStart As Point
        'Our selection rectangle
        Dim DrawRect As Rectangle
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            'Use a dashed style
            PenDashed.DashStyle = Drawing2D.DashStyle.Dash
        End Sub
    
        Private Sub pbDraw_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbDraw.MouseDown
            'Allow drawing of selection rectangle if user pressed left mouse button
            If e.Button = Windows.Forms.MouseButtons.Left Then
                Drawing = True
                'Make note of our start location
                DrawStart = e.Location
            End If
        End Sub
    
        Private Sub pbDraw_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbDraw.MouseMove
            'If we are not drawing then return
            If Not Drawing Then Return
    
            'clear the previous selection rectangle
            If DrawRect <> Nothing Then
                DrawRect.Inflate(1, 1)
                pbDraw.Invalidate(DrawRect)
                pbDraw.Update()
            End If
    
            'Current mouse location
            Dim newLocation As Point = e.Location
    
            'Adjust the X location if it's outside pbDraw's bounds
            If newLocation.X < 0 Then
                newLocation.X = 0
            ElseIf newLocation.X > pbDraw.ClientSize.Width Then
                newLocation.X = pbDraw.ClientSize.Width
            End If
    
            'Adjust the Y location if it's outside pbDraw's bounds
            If newLocation.Y > pbDraw.ClientSize.Height Then
                newLocation.Y = pbDraw.ClientSize.Height
            ElseIf newLocation.Y < 0 Then
                newLocation.Y = 0
            End If
    
            'Get a new rectangle for our selection
            DrawRect = GetRectangle(DrawStart, newLocation)
    
            'Draw a selection rectangle
            pbDraw.CreateGraphics.DrawRectangle(PenDashed, DrawRect)
        End Sub
    
        Private Sub pbDraw_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbDraw.MouseUp
            'Crop image if left mouse button was released.
            'Cropping the image immediatly is probably a bad idea
            'but that behavior could easily be changed.
            If e.Button = Windows.Forms.MouseButtons.Left Then
                Drawing = False
                CropImage()
            End If
        End Sub
    
        'Returns a rectangle defined by two points
        Private Function GetRectangle(ByVal point1 As Point, ByVal point2 As Point) As Rectangle
            Dim Left, Right, Top, Bottom As Integer
    
            If point1.X < point2.X Then
                Left = point1.X
                Right = point2.X
            Else
                Left = point2.X
                Right = point1.X
            End If
    
            If point1.Y < point2.Y Then
                Top = point1.Y
                Bottom = point2.Y
            Else
                Top = point2.Y
                Bottom = point1.Y
            End If
    
            Return Rectangle.FromLTRB(Left, Top, Right, Bottom)
        End Function
    
        'Crops the image defined by DrawRect
        Private Sub CropImage()
            'Return if no selection has been made
            If DrawRect = Nothing Then Return
            'A new image to hold our cropped region
            Dim bmp As New Bitmap(DrawRect.Width, DrawRect.Height)
            'Graphics object
            Dim gr As Graphics = Graphics.FromImage(bmp)
            'Draw the defined region onto our new image
            gr.DrawImage(pbDraw.Image, 0, 0, DrawRect, GraphicsUnit.Pixel)
    
            pbDraw.Image = bmp
        End Sub
    
    End Class
  • 14 years ago
    Thanks. It helps.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Java is to JavaScript what Car is to Carpet.” - Chris Heilmann