Results 1 to 5 of 5

Thread: Draw Border Around Image in Zoomed Picturebox

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    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?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

  3. #3
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    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:
    1. //In the parents resize event
    2. var pb = pictureBox1;
    3. var csz = pb.Parent.ClientSize;
    4.  
    5. int w = pb.Image.Width + pb.Width - pb.ClientSize.Width;
    6. int h = pb.Image.Height + pb.Height - pb.ClientSize.Height;
    7.  
    8. float scale = Math.Min((float)csz.Width / w, (float)csz.Height / h);
    9.  
    10. w = (int)(0.5f + scale * w);
    11. h = (int)(0.5f + scale * h);
    12. int x = (csz.Width - w) / 2;
    13. int y = (csz.Height - h) / 2;
    14.  
    15. pb.Bounds = new Rectangle(x, y, w, h);
    W o t . S i g

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: Draw Border Around Image in Zoomed Picturebox

    Quote Originally Posted by Milk View Post
    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:
    1. //In the parents resize event
    2. var pb = pictureBox1;
    3. var csz = pb.Parent.ClientSize;
    4.  
    5. int w = pb.Image.Width + pb.Width - pb.ClientSize.Width;
    6. int h = pb.Image.Height + pb.Height - pb.ClientSize.Height;
    7.  
    8. float scale = Math.Min((float)csz.Width / w, (float)csz.Height / h);
    9.  
    10. w = (int)(0.5f + scale * w);
    11. h = (int)(0.5f + scale * h);
    12. int x = (csz.Width - w) / 2;
    13. int y = (csz.Height - h) / 2;
    14.  
    15. 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.

  5. #5
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    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.
    W o t . S i g

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width