Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Getting image position within picturebox

Posted on 2009-06-29
13
Medium Priority
?
328 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month8 days, 6 hours left to enroll

877 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