TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Steve Miller
NA
1
3.1k
Extracting a portion of an image
Mar 25 2014 10:42 PM
I'm trying to extract a portion of a bitmap image that I've shown in a picture box (source). I want to place the extracted part into another (destination) picture box for further processing. The extracted portion may be a rectangle, ellipse or polygon. I want to
center
the extracted portion in the destination picture box. (The source and destination boxes are the same dimensions)
I am able to draw "rubber band" rectangles and ellipses (haven't made it to polygons yet) and create graphics paths from these primitives. I use set clip on the graphics object in the destination picture box to draw only the portion of the image I want in the destination box and I calculate the offset (origin) to position the piece of image from the "getbounds" method of the graphics path in the destination box and also to translate the clip so that the piece of the image and the clipping path align in the destination box.
But they don't seem to be lining up. Could it be that taking coordinates from the source box and using them in the destination box doesn't work even though I've made the boxes the same size?
The method is shown below:
private void CombineElements_ShowMask()
{
int X, Y,h,w, tx,ty;
Rectangle src,dest;
Point ptOrigin;
//Draw the masked image in the preview picturebox. We're really drawing the whole image under the clipping mask
//defined by the graphics path "grpthMask"
//We want to center the portion of the masked image in the preview box
//This means moving both the image and the clipping mask
//Calculate the translation values to center the masked area in the picturebox
//The center of the masked image is found from the bounds of the encompassing rectangle
ptOrigin = new Point((int)this.grpthMask.GetBounds().X,(int)this.grpthMask.GetBounds().Y);//UL corner of the mask
w = (int)this.grpthMask.GetBounds().Width;//Width of the mask
h = (int)this.grpthMask.GetBounds().Height;//Height of the mask
X = (this.pbxPreview.Width - w)/2;
Y = (this.pbxPreview.Height - h)/2;
dest = new Rectangle(X, Y, w, h);
src = new Rectangle(ptOrigin.X,ptOrigin.Y, w, h);
//mover = new Matrix(src,corners);
//this.grpthMask.Transform(mover);
this.pGraphics.SetClip(this.grpthMask, CombineMode.Replace);
this.pGraphics.TranslateClip(X, Y);
this.bmpMaskedImage = this.bmpInputImage.Clone(src, System.Drawing.Imaging.PixelFormat.DontCare);
tx = (int)this.pGraphics.ClipBounds.Location.X;
ty = (int)this.pGraphics.ClipBounds.Location.Y;
this.pGraphics.DrawImage(this.bmpMaskedImage, X, Y);
//this.pbxPreview.Image = this.bmpMaskedImage;
//this.pbxPreview.Invalidate();
}
I've also tried to manipulate the image and then just set the image in the destination picture box to be the manipulated image. But clipping doesn't seem to work unless one is using "graphics.drawimage" method.
Any guidance?
Reply
Answers (
1
)
draw sprite
Image processing