Solved

Getting image position within picturebox

Posted on 2009-06-29
13
293 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

914 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now