Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have some code (some from MSDN) that crops an area rectangle on a form, the rectangle must be inside the borders of the form to work. I then capture the cropped area and save it to a JPG file. This works quite well, however when I add a picture window with a picture on it the code doesn't work if I try to start (start position) inside the picture window. If I start outside the window it works fine. What can I do to my code to remedy this hiccup. Here is my code:

VB
Dim isDrag As Boolean = False
   Dim myRectangle As New Rectangle(New Point(0, 0), New Size(0, 0))
   Dim startPoint As Point
   Private memoryImage As Bitmap

   Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles MyBase.MouseDown
       If (e.Button = MouseButtons.Left) Then isDrag = True
       Dim control As Control = CType(sender, Control)
       startPoint = control.PointToScreen(New Point(e.X, e.Y))
   End Sub

   Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles MyBase.MouseMove
       ' If the mouse is being dragged, undraw and redraw the rectangle as the mouse moves.
       If (isDrag) Then
           ' Hide the previous rectangle by calling the DrawReversibleFrame method with the same parameters.
           ControlPaint.DrawReversibleFrame(myRectangle, Me.BackColor, FrameStyle.Thick)
           Dim endPoint As Point = CType(sender, Control).PointToScreen(New Point(e.X, e.Y))
           Dim width As Integer = endPoint.X - startPoint.X
           Dim height As Integer = endPoint.Y - startPoint.Y
           myRectangle = New Rectangle(startPoint.X, startPoint.Y, width, height)
           'Draw the new rectangle by calling DrawReversibleFrame again.
           ControlPaint.DrawReversibleFrame(myRectangle, Me.BackColor, FrameStyle.Thick)
       End If
   End Sub

   Private Sub Form1_MouseUp(sender As Object, e As MouseEventArgs) Handles MyBase.MouseUp
       isDrag = False
       ControlPaint.DrawReversibleFrame(myRectangle, Me.BackColor, FrameStyle.Thick)
       ControlPaint.DrawReversibleFrame(myRectangle, Me.BackColor, FrameStyle.Thick)
   End Sub

   Private Sub CaptureScreen()
       memoryImage = New Bitmap(myRectangle.Width, myRectangle.Height)
       Dim memoryGraphics As Graphics = Graphics.FromImage(memoryImage)
       memoryGraphics.CopyFromScreen(myRectangle.X, myRectangle.Y, 0, 0, Me.Size)
   End Sub
Posted
Updated 12-Jun-14 7:38am
v2
Comments
Dinesh.V.Kumar 12-Jun-14 0:52am    
Check this site..
http://www.codeproject.com/Articles/20923/Mouse-Position-over-Image-in-a-PictureBox
I am not sure if this will help you. Just let me know if it helps. If not I will try to find some other solution

Regards
Member 10628309 12-Jun-14 17:41pm    
I haven't tried it yet but what I think is needed is to add in the Handles of the mouse events>> PictureBox1.MouseDown PictureBox1.MouseMove and PictureBox1.MouseUp
Duncan Edwards Jones 12-Jun-14 7:18am    
is it that the form's mouse methods are not fired when you are over the picture box?
Member 10628309 12-Jun-14 17:34pm    
Yes, the mouse's methods are not fired when over the picture box

Use
VB
System.Windows.Forms.Control.Capture
to make your form receive mouse messages even when the mouse is over a different window (like a child window for example) - and set it off again to release mouse.

For example in a standard form with a checkbox on and a picturebox if you have the code:-

VB
Public Class Form1

    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged

        Me.Capture = CheckBox1.Checked

    End Sub


    Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove

        Me.Text = e.X & "," & e.Y

    End Sub
End Class


Then when the checkbox is checked the x,y will display even when the mouse moves over the picturebox. When the checkbox is not checked it will not.
 
Share this answer
 
v2
Comments
Member 10628309 12-Jun-14 17:31pm    
Duncan Edwards Jones
Using System.Windows.Forms.Control.Capture will tell you true or false if you captured the mouse position but it does not help you capture the position.
Duncan Edwards Jones 13-Jun-14 6:43am    
It does if you set it to true - see updated code sample.
Since my last posting I have experminted with every Cropping Code I could find. I was unable to find anything that I could use for cropping a Form where the cropping rectangle paints on top of controls. I did not want to use a picturebox or panel. In my continuous learning experience I learned that if the Form BackColor and TransparencyKey property are set to the same color, the form becomes invisible. This gave me the idea to have the cropping rectangle painted on the invisible Form. After considerable expermintation I was able to write code that does exactly what I wanted it to do.
I used two Forms, Form1 has the normal controls, e.g., TextBox, DataGridView, etc. I made Form2 invisible with no border, and the same size as Form1. The cropping rectangle paints over anything on Form1 (including the boarder) and the cropped image gets saved as a JPG file.

[Edit] Removed <pre> tags [/Edit]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900