Link to home
Start Free TrialLog in
Avatar of Unimatrix_001
Unimatrix_001Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Getting image position within picturebox

Hello,

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

Thanks,
Uni
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

I don't understand what you are asking...are you talking about something like SizeMode = CenterImage?
Avatar of Unimatrix_001

ASKER

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
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.
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. :)
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

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
Hi,

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

Thanks,
Uni
What do you mean by "ability to draw on the image"?  Are drawing and zooming, or just zooming?
Drawing and zooming.
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.
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?
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you - I'll see what I come up with. :)