DotNetNewsgroup.com  
web access to complete list of Microsoft.NET newsgroups
   home   |   control panel login   |   archive  |  
 
  carried group
academic
adonet
aspnet
aspnet.announcements
aspnet.buildingcontrols
aspnet.caching
aspnet.datagridcontrol
aspnet.mobile
aspnet.security
aspnet.webcontrols
aspnet.webservices
assignment_manager
datatools
dotnet.distributed_apps
dotnet.general
dotnet.myservices
dotnet.nternationalization
dotnet.scripting
dotnet.security
dotnet.vjsharp
dotnet.vsa
dotnet.xml
dotnetfaqs
framework
framework.clr
framework.compactframework
framework.component_services
framework.controls
framework.databinding
framework.drawing
framework.enhancements
framework.interop
framework.odbcnet
framework.performance
framework.remoting
framework.sdk
framework.setup
framework.webservices
framework.windowsforms
framework.wmi
frwk.windowsforms.designtime
lang.csharp
lang.jscript
lang.vb
lang.vb.controls
lang.vb.data
lang.vb.upgrade
lang.vc
lang.vc.libraries
  
 
start date: Sun, 05 Aug 2007 23:32:34 -0700,    posted on: microsoft.public.dotnet.framework.drawing        back       

Thread Index
  1    unknown
          2    Bob Powell [MVP]
          3    unknown


Copying a selected area of PictureBox   
Currently I have the code that draws a dotted rectangle within a
picture box as selected area. I want to replace the current image with
that selected image segment. The problem is that the picturebox is
located in a Panel of a TabControl and the x,y references the x,y
relative to the main form rather than the picturebox itself. When
creating a clone of the rectangle, I have to substract the offset of
the picturebox location relative to the form. However the copied area
that supposely the segment within the rectangle keep showing up the
wrong area... The following is the code. Currently it just superimpose
on the current image so that I can see whether it copied the right
segment..


    Dim isDrag As Boolean = False
    Dim theRectangle As New Rectangle(New Point(0, 0), New Size(0, 0))
    Dim startPoint As Point

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

        ' Set the isDrag variable to true and get the starting point
        ' by using the PointToScreen method to convert form
coordinates to
        ' screen coordinates.
        If (e.Button = Windows.Forms.MouseButtons.Left) Then
            isDrag = True
        End If

        Dim control As Control = CType(sender, Control)

        ' Calculate the startPoint by using the PointToScreen
        ' method.
        startPoint = control.PointToScreen(New Point(e.X, e.Y))
        LbStartPoint.Text = "(" + startPoint.X.ToString() + "," +
startPoint.Y.ToString() + ")"
    End Sub

    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e
As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.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(theRectangle,
Me.BackColor, FrameStyle.Dashed)

            ' Calculate the endpoint and dimensions for the new
rectangle,
            ' again using the PointToScreen method.
            Dim endPoint As Point = PictureBox1.PointToScreen(New
Point(e.X, e.Y))
            Dim width As Integer = endPoint.X - startPoint.X
            Dim height As Integer = endPoint.Y - startPoint.Y
            theRectangle = New Rectangle(startPoint.X, startPoint.Y,
width, height)

            ' Draw the new rectangle by calling DrawReversibleFrame
again.
            ControlPaint.DrawReversibleFrame(theRectangle,
Me.BackColor, FrameStyle.Dashed)
            LbEndPoint.Text = "(" + endPoint.X.ToString() + "," +
endPoint.Y.ToString() + ")"

        End If
    End Sub

    Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        ' If the MouseUp event occurs, the user is not dragging.
        isDrag = False

        If g_OrgBitmap Is Nothing Then Exit Sub

        ' Draw the rectangle to be evaluated. Set a dashed frame
style
        ' using the FrameStyle enumeration.
        ControlPaint.DrawReversibleFrame(theRectangle, Me.BackColor,
FrameStyle.Dashed)

        Dim CopyRectX As Integer, CopyRectY As Integer, CopyRectWidth
As Integer, CopyRectHeight As Integer
        Dim MaxWidth As Integer, MaxHeight As Integer
        CopyRectX = startPoint.X - 343
        CopyRectY = startPoint.Y - 288
        CopyRectWidth = theRectangle.Width
        CopyRectHeight = theRectangle.Height

        'fail safe measures
        If CopyRectX < 0 Then CopyRectX = 0
        If CopyRectY < 0 Then CopyRectY = 0
        MaxWidth = g_OrgBitmap.Width - CopyRectX
        MaxHeight = g_OrgBitmap.Height - CopyRectY

        If CopyRectWidth > MaxWidth Then CopyRectWidth = MaxWidth
        If CopyRectHeight > MaxHeight Then CopyRectHeight = MaxHeight


        PictureBox1.CreateGraphics.DrawImage(g_OrgBitmap.Clone(New
Rectangle(CopyRectX, CopyRectY, CopyRectWidth, CopyRectHeight),
Drawing.Imaging.PixelFormat.Format24bppRgb), 0, 0)

        ' Reset the rectangle.
        theRectangle = New Rectangle(0, 0, 0, 0)

    End Sub
Date:Sun, 05 Aug 2007 23:32:34 -0700   Author:  

Re: Copying a selected area of PictureBox   
If you use the ClientToScreen method of the form then the ScreenToClient 
method of the PictureBox to adjust the positions you will be able to refer 
to the image in picturebox coordinates.

-- 
-- 
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.


 wrote in message 
news:1186381954.778511.182380@e9g2000prf.googlegroups.com...

> Currently I have the code that draws a dotted rectangle within a
> picture box as selected area. I want to replace the current image with
> that selected image segment. The problem is that the picturebox is
> located in a Panel of a TabControl and the x,y references the x,y
> relative to the main form rather than the picturebox itself. When
> creating a clone of the rectangle, I have to substract the offset of
> the picturebox location relative to the form. However the copied area
> that supposely the segment within the rectangle keep showing up the
> wrong area... The following is the code. Currently it just superimpose
> on the current image so that I can see whether it copied the right
> segment..
>
>
>    Dim isDrag As Boolean = False
>    Dim theRectangle As New Rectangle(New Point(0, 0), New Size(0, 0))
>    Dim startPoint As Point
>
>   Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e
> As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
>
>        ' Set the isDrag variable to true and get the starting point
>        ' by using the PointToScreen method to convert form
> coordinates to
>        ' screen coordinates.
>        If (e.Button = Windows.Forms.MouseButtons.Left) Then
>            isDrag = True
>        End If
>
>        Dim control As Control = CType(sender, Control)
>
>        ' Calculate the startPoint by using the PointToScreen
>        ' method.
>        startPoint = control.PointToScreen(New Point(e.X, e.Y))
>        LbStartPoint.Text = "(" + startPoint.X.ToString() + "," +
> startPoint.Y.ToString() + ")"
>    End Sub
>
>    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e
> As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.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(theRectangle,
> Me.BackColor, FrameStyle.Dashed)
>
>            ' Calculate the endpoint and dimensions for the new
> rectangle,
>            ' again using the PointToScreen method.
>            Dim endPoint As Point = PictureBox1.PointToScreen(New
> Point(e.X, e.Y))
>            Dim width As Integer = endPoint.X - startPoint.X
>            Dim height As Integer = endPoint.Y - startPoint.Y
>            theRectangle = New Rectangle(startPoint.X, startPoint.Y,
> width, height)
>
>            ' Draw the new rectangle by calling DrawReversibleFrame
> again.
>            ControlPaint.DrawReversibleFrame(theRectangle,
> Me.BackColor, FrameStyle.Dashed)
>            LbEndPoint.Text = "(" + endPoint.X.ToString() + "," +
> endPoint.Y.ToString() + ")"
>
>        End If
>    End Sub
>
>    Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As
> System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
>        ' If the MouseUp event occurs, the user is not dragging.
>        isDrag = False
>
>        If g_OrgBitmap Is Nothing Then Exit Sub
>
>        ' Draw the rectangle to be evaluated. Set a dashed frame
> style
>        ' using the FrameStyle enumeration.
>        ControlPaint.DrawReversibleFrame(theRectangle, Me.BackColor,
> FrameStyle.Dashed)
>
>        Dim CopyRectX As Integer, CopyRectY As Integer, CopyRectWidth
> As Integer, CopyRectHeight As Integer
>        Dim MaxWidth As Integer, MaxHeight As Integer
>        CopyRectX = startPoint.X - 343
>        CopyRectY = startPoint.Y - 288
>        CopyRectWidth = theRectangle.Width
>        CopyRectHeight = theRectangle.Height
>
>        'fail safe measures
>        If CopyRectX < 0 Then CopyRectX = 0
>        If CopyRectY < 0 Then CopyRectY = 0
>        MaxWidth = g_OrgBitmap.Width - CopyRectX
>        MaxHeight = g_OrgBitmap.Height - CopyRectY
>
>        If CopyRectWidth > MaxWidth Then CopyRectWidth = MaxWidth
>        If CopyRectHeight > MaxHeight Then CopyRectHeight = MaxHeight
>
>
>        PictureBox1.CreateGraphics.DrawImage(g_OrgBitmap.Clone(New
> Rectangle(CopyRectX, CopyRectY, CopyRectWidth, CopyRectHeight),
> Drawing.Imaging.PixelFormat.Format24bppRgb), 0, 0)
>
>        ' Reset the rectangle.
>        theRectangle = New Rectangle(0, 0, 0, 0)
>
>    End Sub
> 
Date:Mon, 6 Aug 2007 13:38:53 +0200   Author:  

Re: Copying a selected area of PictureBox   
Thanks for the advice. I was able to translate the point from screen
to picturebox via similar method in vb.net

Dim PicStartPoint As Point = PictureBox1.PointToClient(startPoint)
Date:Tue, 07 Aug 2007 08:29:52 -0700   Author:  

Google
 
Web dotnetnewsgroup.com


COPYRIGHT ?2005, EUROFRONT WORLDWIDE LTD., ALL RIGHT RESERVE  |   Contact us