Unimatrix_001
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
Is there a way I can get the position of the image within the picture box at all?
Thanks,
Uni
I don't understand what you are asking...are you talking about something like SizeMode = CenterImage?
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
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.
ASKER
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. :)
ASKER
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);
}
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
PictureBox Zoom
http://www.codeproject.com/KB/graphics/PictureBoxZoom.aspx
ASKER
Hi,
I'm unsure how that would help me with the ability to draw on the image?
Thanks,
Uni
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?
ASKER
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.
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you - I'll see what I come up with. :)