Solved

Getting image position within picturebox

Posted on 2009-06-29
13
292 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Query datatable in LINQ 6 39
Chat Room 1 25
Get String split 5 32
Achieve json result 2 29
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

15 Experts available now in Live!

Get 1:1 Help Now