Solved

Getting image position within picturebox

Posted on 2009-06-29
13
304 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
0
Comment
Question by:Unimatrix_001
  • 7
  • 6
13 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24736456
I don't understand what you are asking...are you talking about something like SizeMode = CenterImage?
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 24736494
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
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24736555
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.
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 3

Author Comment

by:Unimatrix_001
ID: 24736582
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. :)
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 24736665
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

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24736812
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
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 24736846
Hi,

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

Thanks,
Uni
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24736949
What do you mean by "ability to draw on the image"?  Are drawing and zooming, or just zooming?
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 24736961
Drawing and zooming.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24737079
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.
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 24737100
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?
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 24737140
Here is an example of what I mean:

Draw shapes to the bitmap in memory
http://www.java2s.com/Code/CSharp/2D-Graphics/Drawshapestothebitmapinmemory.htm
        private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            gForm.FillRectangle(Brushes.White, this.ClientRectangle);
 
            // Create a Bitmap image in memory and set its CompositingMode
            using (Bitmap bmp = new Bitmap(260, 260, PixelFormat.Format32bppArgb))
            {
                Graphics gBmp = Graphics.FromImage(bmp);
                gBmp.CompositingMode = CompositingMode.SourceCopy;
 
                // draw a red circle to the bitmap in memory
                Color red = Color.FromArgb(0x60, 0xff, 0, 0);
                using (Brush redBrush = new SolidBrush(red))
                {
                    gBmp.FillEllipse(redBrush, 70, 70, 160, 160);
 
                    // draw a green rectangle to the bitmap in memory
                    Color green = Color.FromArgb(0x40, 0, 0xff, 0);
                    using (SolidBrush greenBrush = new SolidBrush(green))
                    {
                        gBmp.FillRectangle(greenBrush, 10, 10, 140, 140);
 
                        // draw the bitmap on our window
                        gForm.DrawImage(bmp, 20, 20, bmp.Width, bmp.Height);
 
                    }
                }
            }
        }

Open in new window

0
 
LVL 3

Author Closing Comment

by:Unimatrix_001
ID: 31597888
Thank you - I'll see what I come up with. :)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question