Solved

Getting image position within picturebox

Posted on 2009-06-29
13
301 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need help on C# Linq query on object 6 63
Error in JQuery 5 54
Entity Framework 7 43
Exit the loop 4 43
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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