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?
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.
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);
Re: Draw Border Around Image in Zoomed Picturebox
Quote:
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.
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.