# How can i calculate the size of an image inside a picturebox?

### Question

• pb.Image =

global::gr_try.Properties.Resources.Ball;

The image is the Ball wich is Ball.gif

pb is the PictureBox

So i can calculate the area size of the picturebox.

But now i want to calculate the area size of the ball.gif

If i do it in my code its showing the gif as its in the original size and its bigger then the picturebox.

The picturebox is 30 on 30 wich mean its area = 900

The ball.gif is 121 on 121 wich mean its area is = 14641

But its wrong cuz in my code the ball is smaller its inside the picturebox so its fitting in the picturebox and its even smaller then the picturebox area. Im not sure how it done i think  pb.SizeMode = PictureBoxSizeMode.Zoom; make the ball.gif to be in the picturebox in smaller size.

What i want is to find somehow the ball size when its in the picturebox after zoomed or after it was fit to the picturebox.

Now i can only get the ball area size on its original size and its not good.

Thanks for help.

danieli
Tuesday, August 03, 2010 7:31 AM

### All replies

• Welcome to the MSDN Forums.

Would you like to resize the picture box to the image's original size?! So you can do something like this:

```pb.ClientSize = new Size(global::gr_try.Properties.Resources.Ball.Width, global::gr_try.Properties.Resources.Ball.Height);

pb.Image = global::gr_try.Properties.Resources.Ball;
```

Hope this helps some.

Regards,

Yasser

LEARN HOW WPF IS FLEXIBLE IN PRESENTATION DURING A QUICK SIMPLE WALKTHROUGH:
Walkthrough: Displaying multi column ComboBox by using Windows Presentation Foundation (WPF) data templating
Tuesday, August 03, 2010 8:17 AM
• Thats good but the problem is that the pb.ClientSize when i put breakpoint on it so the ball height and width are 121 and 121.

But still when im running the program the ball seems to be smaller when its in the picturebox. Wasnt the ball suppose to be larger then the picturebox and spread out the picturebox? If the picturebox height and width are 30 and 30 wich is area 900 how come the ball fit inside the picturebox?

Also when i paint the picturebox background now its color empty but when i paint it to green so i see the picturebox background green except the area of the ball wich is still blue and i see that the ball is inside the picturebox and smaller in its area then the picturebox area.

Maybe i dont understand it. But there is no way the ball in the picturebox is 121 on 121. If i take the ball.gif and watch it in external viewer i see it very big.

And in the picturebox even if it says that the size is 121 on 121 the ball looks smaller then the picturebox area.

So maybe when the ball is in the picturebox its size automatic stretch to fit to the picturebox area size? But the ball values still 121 on 121? If so so how can i get the ball size when its in the picturebox?after its automatic getting zoom or getting strecthed to fit into the picturebox ?

I want to get the ball area size like Width*Height after its in the picturtebox. if the picturebox Width and Height are both 30 so the picturebox area is 30*30 wich is 900. So the ball size somehow is strecthed to fit in the picturebox and it cant be Width 121 and Height 121 ( wich is the original size ). The ball Width and Height in the picturebox must be smaller then 30. And thats the values i want to get of the ball. When its in the picturebox. like if the picturebox size is 30 and 30 wich is 900 so the ball size should be something like 15*15 wich is 225.

Else im wrong. But i see it here.

danieli
Tuesday, August 03, 2010 8:40 AM
• OK, don't worry...

Another alternate way is using "PictureBoxSizeMode.AutoSize" for the picture box SizeMode property which does my code automatically. please verify...

LEARN HOW WPF IS FLEXIBLE IN PRESENTATION DURING A QUICK SIMPLE WALKTHROUGH:
Walkthrough: Displaying multi column ComboBox by using Windows Presentation Foundation (WPF) data templating
• Marked as answer by Tuesday, August 03, 2010 9:10 AM
Tuesday, August 03, 2010 8:58 AM
• The autosize show the ball in its original size wich is Width 121 and Height 121.

Thanks.

danieli
Tuesday, August 03, 2010 9:10 AM
• The autosize show the ball in its original size wich is Width 121 and Height 121.

Thanks.

You're welcome, Danieli :)
LEARN HOW WPF IS FLEXIBLE IN PRESENTATION DURING A QUICK SIMPLE WALKTHROUGH:
Walkthrough: Displaying multi column ComboBox by using Windows Presentation Foundation (WPF) data templating
Tuesday, August 03, 2010 9:13 AM