By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,223 Members | 1,447 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,223 IT Pros & Developers. It's quick & easy.

extracting part of a graphic in a PictureBox to the clipboard

P: n/a
hi,

i need to give the user the ability to extract a rectangular area of their
choice from a graphic displayed in a picture box to the clipboard, so they
can use it elsewhere.

say the graphic has an image of a house with a door in it and the user is
interested in the door. the rectangle is selected by placing the mouse on
the upper left corner of the door, pressing down on the left mouse button,
and draging it to the lower right corner of the door and releasing the
mouse. from the time the mouse is down till when the mouse is up, a light
rectangle surrounds the selected area. the surrounding rectangle grows and
shrinks as the dragging of the mouise moves. the user indicates his choice
by releasing the mouse and letting it go up, whereupon the area selected by
the rectangle is placed upon the clipboard.

i don't have much experience with graphics but i was able to figure out part
of the solution, but i'm missing part of it.

here is what i tried

i declared 4 form level integers, miTop, miLeft, miBottom, miRight, all of
which are set to -1 when the form loads.

in the picturebox during the mouse down event, miLeft is set to e.X, and
miTop to e.Y

during the mouse move event if miTop, miLeft are not -1, miRight is set to
e.X and miBottom is set to e.Y, and the bounding rectangle is drawn with the
following code in the mouse move event

Dim ev As PaintEventArgs

pic_Paint(sender, ev)

the pic_Paint is as follows

Private Sub pic_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles pic.Paint

Dim gr As Graphics = Me.pic.CreateGraphics

dim iWidth, iHeight as integer

iWidth = miRight - miLeft

iHeight = miBottom - miTop

'MY PROBLEM IS RIGHT HERE, BECAUSE I HAVE TO REMOVE A PREVIOUS SURROUNDING
RECTANGLE (IF THERE IS ONE) BEFORE MAKING THE NEXT LARGER OR SMALLER ONE

'OTHERWISE THE RECTANGLES JUST KEEP MULTIPLYING, AND I DON'T KNOW HOW TO
REMOVE A PREVIOUS BOUNDING RECTANGLE AND RETURN THE IMAGE TO ITS

'FORMER STATE, I CAN'T JUST MAKE A RECTANGLE WITH A BACKGROUND COLOR, I NEED
TO LEAVE THE IMAGE CLEAN AS IT IS

gr.DrawRectangle(Pens.Gray, Me.miLeft, Me.miTop, iWidth, iHeight) 'this
draws the rectangle

End Sub

in the mouse up event i need to do 2 things, extract the surrounded area,
and place it on the clipboard

i don't know how to do the extraction. i can extract to a invisible
PictueBox picHidden which i would size to extracted rectangle's size and set
its SizeMode to Normal, but i don't know the code to do the extraction.

i then can place the rectangle on the clipboad with

Clipboard.SetDataObject(Me.picHIdden.Image)

in summary i don't know the code to do 2 things. 1) remove a rectangle
drawn on graphic and return the graphic to its former state without
reloading the image, 2) how to extract a rectangle from a PictureBox.Image

thanks for any code and help, or if there is better way to do what i need to
do

ray


Jan 30 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
You may be able to use the Picturebox's Graphic Object and BitBlt to copy the
area you want to a bitmap.
--
Dennis in Houston
"ray well" wrote:
hi,

i need to give the user the ability to extract a rectangular area of their
choice from a graphic displayed in a picture box to the clipboard, so they
can use it elsewhere.

say the graphic has an image of a house with a door in it and the user is
interested in the door. the rectangle is selected by placing the mouse on
the upper left corner of the door, pressing down on the left mouse button,
and draging it to the lower right corner of the door and releasing the
mouse. from the time the mouse is down till when the mouse is up, a light
rectangle surrounds the selected area. the surrounding rectangle grows and
shrinks as the dragging of the mouise moves. the user indicates his choice
by releasing the mouse and letting it go up, whereupon the area selected by
the rectangle is placed upon the clipboard.

i don't have much experience with graphics but i was able to figure out part
of the solution, but i'm missing part of it.

here is what i tried

i declared 4 form level integers, miTop, miLeft, miBottom, miRight, all of
which are set to -1 when the form loads.

in the picturebox during the mouse down event, miLeft is set to e.X, and
miTop to e.Y

during the mouse move event if miTop, miLeft are not -1, miRight is set to
e.X and miBottom is set to e.Y, and the bounding rectangle is drawn with the
following code in the mouse move event

Dim ev As PaintEventArgs

pic_Paint(sender, ev)

the pic_Paint is as follows

Private Sub pic_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles pic.Paint

Dim gr As Graphics = Me.pic.CreateGraphics

dim iWidth, iHeight as integer

iWidth = miRight - miLeft

iHeight = miBottom - miTop

'MY PROBLEM IS RIGHT HERE, BECAUSE I HAVE TO REMOVE A PREVIOUS SURROUNDING
RECTANGLE (IF THERE IS ONE) BEFORE MAKING THE NEXT LARGER OR SMALLER ONE

'OTHERWISE THE RECTANGLES JUST KEEP MULTIPLYING, AND I DON'T KNOW HOW TO
REMOVE A PREVIOUS BOUNDING RECTANGLE AND RETURN THE IMAGE TO ITS

'FORMER STATE, I CAN'T JUST MAKE A RECTANGLE WITH A BACKGROUND COLOR, I NEED
TO LEAVE THE IMAGE CLEAN AS IT IS

gr.DrawRectangle(Pens.Gray, Me.miLeft, Me.miTop, iWidth, iHeight) 'this
draws the rectangle

End Sub

in the mouse up event i need to do 2 things, extract the surrounded area,
and place it on the clipboard

i don't know how to do the extraction. i can extract to a invisible
PictueBox picHidden which i would size to extracted rectangle's size and set
its SizeMode to Normal, but i don't know the code to do the extraction.

i then can place the rectangle on the clipboad with

Clipboard.SetDataObject(Me.picHIdden.Image)

in summary i don't know the code to do 2 things. 1) remove a rectangle
drawn on graphic and return the graphic to its former state without
reloading the image, 2) how to extract a rectangle from a PictureBox.Image

thanks for any code and help, or if there is better way to do what i need to
do

ray


Jan 31 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.