-
Mar 20th, 2011, 01:59 PM
#1
Thread Starter
Frenzied Member
Draw Border Around Image in Zoomed Picturebox
Hello,
how do I draw a border around the Image of a Picturebox. I can't use the PictureBox's border styles because the SizeMode is set to zoom.
Basically if I have the Rectangle of the image in the PictureBox then I can draw the border so my question is: How do I get the dimensions/location of the 'zoomed' image in a PictureBox?
-
Mar 20th, 2011, 08:03 PM
#2
Re: Draw Border Around Image in Zoomed Picturebox
I would think that the easiest option would be to just create another Image. Make it as much bigger than the original as you want the thickness of your border to be. Call Graphics.FromImage and then Graphics.Clear to make the whole Image the colour you want the border to be. You then call Graphics.DrawImage to draw the original Image in the centre, leaving your coloured border around the outside.
-
Mar 21st, 2011, 10:35 AM
#3
Re: Draw Border Around Image in Zoomed Picturebox
Would it be inappropriate to manually resize the picturebox? You could maintain the aspect ratio keeping the border tight to the image (SizeMode would be set to stretch)
c# Code:
//In the parents resize event var pb = pictureBox1; var csz = pb.Parent.ClientSize; int w = pb.Image.Width + pb.Width - pb.ClientSize.Width; int h = pb.Image.Height + pb.Height - pb.ClientSize.Height; float scale = Math.Min((float)csz.Width / w, (float)csz.Height / h); w = (int)(0.5f + scale * w); h = (int)(0.5f + scale * h); int x = (csz.Width - w) / 2; int y = (csz.Height - h) / 2; pb.Bounds = new Rectangle(x, y, w, h);
-
Mar 21st, 2011, 04:18 PM
#4
Thread Starter
Frenzied Member
Re: Draw Border Around Image in Zoomed Picturebox
Originally Posted by Milk
Would it be inappropriate to manually resize the picturebox? You could maintain the aspect ratio keeping the border tight to the image (SizeMode would be set to stretch)
c# Code:
//In the parents resize event
var pb = pictureBox1;
var csz = pb.Parent.ClientSize;
int w = pb.Image.Width + pb.Width - pb.ClientSize.Width;
int h = pb.Image.Height + pb.Height - pb.ClientSize.Height;
float scale = Math.Min((float)csz.Width / w, (float)csz.Height / h);
w = (int)(0.5f + scale * w);
h = (int)(0.5f + scale * h);
int x = (csz.Width - w) / 2;
int y = (csz.Height - h) / 2;
pb.Bounds = new Rectangle(x, y, w, h);
Well the image has to fit in a certain space on the screen. It can't exceed a certain rectangle - otherwise it would overlap other controls. Also if the image is very large then the picturebox would be larger than the form.
-
Mar 21st, 2011, 06:03 PM
#5
Re: Draw Border Around Image in Zoomed Picturebox
Do you follow what I'm suggesting. You would place the picturebox in some container that defines the certain rectangle. Set the Picturebox SizeMode to Stretch and give it a border. The code works like the Zoom SizeMode except it alters the size and position of the picturebox rather than its image.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|