We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Getting image position within picturebox

Unimatrix_001
on
Medium Priority
656 Views
Last Modified: 2012-06-22
Hello,

Is there a way I can get the position of the image within the picture box at all?

Thanks,
Uni
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
I don't understand what you are asking...are you talking about something like SizeMode = CenterImage?

Author

Commented:
Yes, sort of except I'm having to use SizeMode=Zoom, which has the nasty habit of centering the image within the picturebox. The dark background indicated the picture box:


Screen.JPG
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
If the PictureBox has a certain width, and the image has a certain width, and the image is centered, then the image left position is half the difference between the PictureBox width and the image width.

Author

Commented:
Hm, I hadn't thought of that!... I'll be back in a little while as that may be what I'm after, I'll just do a few tests. :)

Author

Commented:
Hm, I think it may be doable, but perhaps I'm messing up the maths. What I've got is a picture box set to zoom and the picture box is inside a panel which is set to autoscroll, this lets me have a scrollable picture. I've implemented zoom in/out buttons by simply altering the picturebox width and height, although I'm now wanting to be able to 'draw' on the picture, which is fine by handling the picturebox draw event, but I'm having difficulty getting the shape to draw in the same place all the time in relation to the image.
private void picboxMainImage_Paint(object sender, PaintEventArgs e) {
 
	int imgTop=0;
	if(picboxMainImage.Height-picboxMainImage.Image.Height>0)
		imgTop=(picboxMainImage.Height-picboxMainImage.Image.Height)/2;
 
	int imgLeft=0;
	if(picboxMainImage.Width-picboxMainImage.Image.Width>0)
		imgLeft=(picboxMainImage.Width-picboxMainImage.Image.Width)/2;
	else if(picboxMainImage.Image.Width-picboxMainImage.Width>0)
		imgLeft=(picboxMainImage.Image.Width-picboxMainImage.Width)/2;
 
	e.Graphics.FillRectangle(Brushes.AliceBlue, 20+imgLeft, 50+imgTop, 100, 100);
}

Open in new window

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
You might think about zooming, by setting the SizeMode = Stretch, and then changing the image size, not the PictureBox size.

PictureBox Zoom
http://www.codeproject.com/KB/graphics/PictureBoxZoom.aspx

Author

Commented:
Hi,

I'm unsure how that would help me with the ability to draw on the image?

Thanks,
Uni
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
What do you mean by "ability to draw on the image"?  Are drawing and zooming, or just zooming?

Author

Commented:
Drawing and zooming.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
In order to draw on the image, you would create a Graphics object from the Image, and then draw with the Graphics object.  It also gives you the ability to change the image dimensions, as shown in the article above.

Author

Commented:
Ah, so you're saying I shouldn't be drawing in the PictureBox draw event, but should draw to the actual Image that PictureBox has?
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
Thank you - I'll see what I come up with. :)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.