Solved

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

Posted on 2009-05-14
2
132 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

706 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

21 Experts available now in Live!

Get 1:1 Help Now