Solved

need help selecting part of a picture and save it as its own picture file

Posted on 2009-05-14
2
134 Views
Last Modified: 2012-05-07
I would like to open a picture file say bmp or a gif or whatever then select parts of the picture and save them as individual files
I have the following code that allows me to drag a selection box across part of an image which I have listed below




Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove

        

            If e.Button = Windows.Forms.MouseButtons.Left Then

                ControlPaint.DrawReversibleFrame(NormalizedRC(screenPtA, screenPtB), Color.Black, FrameStyle.Dashed)

                screenPtB = sender.PointToScreen(New Point(e.X, e.Y))

                ControlPaint.DrawReversibleFrame(NormalizedRC(screenPtA, screenPtB), Color.Black, FrameStyle.Dashed)

            End If

    
 

    End Sub

Open in new window

0
Comment
Question by:Dov_B
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 24389485
Assuming your image in PictureBox1 is not being stretched or scrolled:
Public Class Form1
 

    Private RC As Rectangle

    Private screenPtA, screenPtB As Point
 

    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown

        If e.Button = Windows.Forms.MouseButtons.Left Then

            Dim pb As PictureBox = CType(sender, PictureBox)

            Cursor.Clip = pb.RectangleToScreen(pb.ClientRectangle)

            screenPtA = Cursor.Position

            screenPtB = screenPtA

            RC = NormalizedRC(screenPtA, screenPtB)

            ControlPaint.DrawReversibleFrame(RC, Color.Black, FrameStyle.Dashed)

        End If

    End Sub
 

    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove

        If e.Button = Windows.Forms.MouseButtons.Left Then

            ControlPaint.DrawReversibleFrame(RC, Color.Black, FrameStyle.Dashed)

            screenPtB = Cursor.Position

            RC = NormalizedRC(screenPtA, screenPtB)

            ControlPaint.DrawReversibleFrame(RC, Color.Black, FrameStyle.Dashed)

        End If

    End Sub
 

    Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp

        If e.Button = Windows.Forms.MouseButtons.Left Then

            Cursor.Clip = Nothing

            Dim pb As PictureBox = CType(sender, PictureBox)

            RC = pb.RectangleToClient(RC)
 

            Dim bmp As New Bitmap(RC.Width, RC.Height)

            Dim g As Graphics = Graphics.FromImage(bmp)

            g.DrawImage(PictureBox1.Image, 0, 0, RC, GraphicsUnit.Pixel)

            g.Dispose()
 

            ' ...do something with bmp...

            PictureBox2.Image = bmp

        End If

    End Sub
 

    Public Function NormalizedRC(ByVal ptA As Point, ByVal ptB As Point) As Rectangle

        Return New Rectangle(Math.Min(ptA.X, ptB.X), Math.Min(ptA.Y, ptB.Y), Math.Abs(ptA.X - ptB.X), Math.Abs(ptA.Y - ptB.Y))

    End Function
 

End Class

Open in new window

0
 

Author Closing Comment

by:Dov_B
ID: 31581627
Idle mind you have rescued me so many times can you check out a related question I am posting
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now