CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2003
    Location
    Springfield
    Posts
    190

    Question What are the Image coordinates when PictureBoxSizeMode.Zoom?

    this code draws an horizontal line on a PictureBox.
    Code:
    		void bigWindowPictureBoxCamera_Paint(object sender, PaintEventArgs e)
    		{
    			PictureBox pb = (PictureBox)sender;
    			e.Graphics.DrawLine(Pens.Red, 0, 27, pb.Width, 27);
    		}
    Since my PictureBox.SizeMode is PictureBoxSizeMode.Zoom I'd like to paint the line on the Image only (not in the unused grey area of the PictureBox).
    I tried the following:
    Code:
    		void bigWindowPictureBoxCamera_Paint(object sender, PaintEventArgs e)
    		{
    				PictureBox pb = (PictureBox)sender;
    
    				Graphics graphics = Graphics.FromImage(pb.Image);
    				graphics.DrawLine(Pens.Red, 0, 27, pb.Image.Width, 27);
    		}
    With the above code, the line appears on the Image only (in the correct coordinates), but only a few times (when resizing the form, the line appears and disappears, mainly it's not visible).

    I got back to the first code (using e.Graphics), trying to figure out how to get the Image coordinates from the PictureBox.
    Can anybody help me doing that?
    Mr. Burns

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: What are the Image coordinates when PictureBoxSizeMode.Zoom?

    First thing I'd try is to make sure my Docking & Ancor properties for the PictureBox are set up correctly. I'd also would have made use of 2 separate pictures here, one without the line, and one with the line through it whih gets loaded when the line is needed. The line is then built in to the picture and with any resize the line will obviously resize accordingly...

  3. #3
    Join Date
    Jul 2003
    Location
    Springfield
    Posts
    190

    Re: What are the Image coordinates when PictureBoxSizeMode.Zoom?

    PictureBox.Dock = DockStyle.Fill into the Form and that's how I wish it to appear.
    The PictureBox aims to show images from a camera and the DrawLine is needed to show a grid on the film from the camera.
    The grid number of lines will be configurable (and must be loaded at runtime).
    Sorry, but I don't understand how the second PictureBox would help in doing that.
    Obviously I simplified the code to make my question clearer.
    Anyway, I solved it with the following quite long calculation:
    Code:
    void bigWindowPictureBoxCamera_Paint(object sender, PaintEventArgs e)
    {
    	PictureBox pb = (PictureBox)sender;
    	int ImageWidth;
    	int ImageHeight;
    
    	if ((double)pb.Width / (double)pb.Height < (double)pb.Image.Width / (double)pb.Image.Height)
    	{
    		ImageWidth = pb.Width;
    		ImageHeight = pb.Image.Height * ImageWidth / pb.Image.Width;
    	}
    	else if ((double)pb.Width / (double)pb.Height > (double)pb.Image.Width / (double)pb.Image.Height)
    	{
    		ImageHeight = pb.Height;
    		ImageWidth = pb.Image.Width * ImageHeight / pb.Image.Height;
    	}
    	else // if ((double)pb.Width / (double)pb.Height == (double)pb.Image.Width / (double)pb.Image.Height)
    	{
    		ImageWidth = pb.Width;
    		ImageHeight = pb.Height;
    	}
    
    	int offsetX = (pb.Width - ImageWidth)/2;
    	int offsetY = (pb.Height - ImageHeight)/2;
    	e.Graphics.DrawLine(this.Pen, offsetX, offsetY+27, offsetX + ImageWidth, offsetY+27);
    }
    Mr. Burns

Tags for this Thread

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

Featured