?
Solved

I have rectangle overlayed on a picture box how can I grab just the part of the picture under the rectangle and save it to a new file

Posted on 2009-12-17
4
Medium Priority
?
272 Views
Last Modified: 2012-05-08
I have rectangle overlayed on a picture box how can I grab just the part of the picture under the rectangle and save it to a new file
I do have some code that I got from the great idlemind that I tried adapting but I cant seem to get it to work below is his code and


my attempted adaptation
I have a group of rectangles in a list called boxes2
 Dim f As Rectangle
        f = Boxes2.ElementAt(2)
        f = PictureBox5.RectangleToClient(f)
        Dim bmp As New Bitmap(f.Width, f.Height)
        Dim g As Graphics = Graphics.FromImage(bmp)
        g.DrawImage(PictureBox5.Image, 0, 0, f, GraphicsUnit.Pixel)
        g.Dispose()
        'myCommnd("insert into aaa values(" & bmp.ToString & ")")
        Dim FileName As String = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "word.bmp")
        bmp.Save(FileName, System.Drawing.Imaging.ImageFormat.Bmp)

        ' ...do something with bmp...
        PictureBox2.Image = bmp
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()
            Dim FileName As String = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "abacus782.gif")
            bmp.Save(FileName, System.Drawing.Imaging.ImageFormat.Gif)

            ' ...do something with bmp...
            PictureBox2.Image = bmp
        End If
    End Sub

Open in new window

0
Comment
Question by:Dov_B
  • 2
4 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 26081371
I believe that one of the challenges with .NET languages are all the overloaded methods, and finding just the right one for the job.

The Graphics.DrawImage method has like 30 different overloaded signatures, which can accomplish different things.  I think that you are looking for this signature:

Graphics.DrawImage Method (Image, Rectangle, Rectangle, GraphicsUnit)
http://msdn.microsoft.com/en-us/library/ms142040.aspx

What you are looking for is the method that uses a source rectangle to get a portion of an image.
0
 
LVL 15

Accepted Solution

by:
x77 earned 2000 total points
ID: 26082095
I think the image is scaled - (ZOOM).
Then yo need eval Fx - Scale Factor, and Offset , blank space.
    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
            If rc.Width = 0 OrElse rc.Height = 0 Then Beep() : Return
            Cursor.Clip = Nothing
            Dim pb As PictureBox = CType(sender, PictureBox)
            rc = pb.RectangleToClient(rc)
            Dim image = pb.Image
            Dim fx = Math.Max(image.Width / pb.Width, _
                              image.Height / pb.Height)
            Dim ofsetx = CInt(pb.Width * fx - image.Width) \ 2
            Dim ofsety = CInt(pb.Height * fx - image.Height) \ 2

            rc.X = rc.X * fx - ofsetx : rc.Width *= fx
            rc.Y = rc.Y * fx - ofsety : rc.Height *= fx

            Dim bmp As New Bitmap(rc.Width, rc.Height)
            Dim g As Graphics = Graphics.FromImage(bmp)
            g.DrawImage(image, New Rectangle(Point.Empty, rc.Size), rc, GraphicsUnit.Pixel)
            g.Dispose()
            'pb.Invalidate()
            Dim FileName As String = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "abacus782.gif")
            bmp.Save(FileName, System.Drawing.Imaging.ImageFormat.Gif)

            ' ...do something with bmp...
            PictureBox2.Image = bmp
        End If
    End Sub

Open in new window

0
 
LVL 15

Expert Comment

by:x77
ID: 26082795
Correction for Factor Zoom / offset, based on client Rectangle.
            Dim image = pb.Image
            Dim sz = pb.ClientSize
            Dim fx = Math.Max(image.Width / sz.Width, _
                              image.Height / sz.Height)
            Dim ofsetx = CInt((sz.Width - image.Width / fx) / 2)
            Dim ofsety = CInt((sz.Height - image.Height / fx) / 2)

            rc.X -= ofsetx : rc.X *= fx : rc.Width *= fx
            rc.Y -= ofsety : rc.Y *= fx : rc.Height *= fx

Open in new window

0
 

Author Closing Comment

by:Dov_B
ID: 31667422
I dont know how you figure this stuff out
I am asking a related question if you could take a look at it I would appreciate it
I am however leaving my computer till sunday or monday at 3:30 eastern
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month15 days, 16 hours left to enroll

850 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