12,754,090 members (29,812 online)
Rate this:
See more:
I have a size of real rectangle in real life that I want Draw in a picturebox to scale.

In the old days (VB6 Days) I would get the Width if the picture box and divide it by the size of the real rectangle to the X Scale. I would repeat this for Y also.

The just procedure to draw retangle with in the picture box.

But I can seam to get this to work.

I was wondering if anyone could show me with some samples on how to translate a real world point to scale it to fit with in a picture box.

i.e.
VS 2010 Project
Rectangle is 10000 x 4500 in real life and me picture box is 450,400

So I want to fit the 10000 x 4500 Rectangle (to Sacle) within this picture box (with some clear space around the rectangle)

Regards
Stephan
Posted 26-Apr-12 16:31pm
Updated 26-Apr-12 16:37pm
v2
VJ Reddy 26-Apr-12 23:29pm

Thank you for accepting the solution.

Rate this:

## Solution 2

Please see my past answer explaining why you should not use `PictureBox` and what to do instead:
How do I clear a panel from old drawing[^].

This answer explains how to write in both image and a control:
draw a rectangle in C#[^].

Append a picture within picturebox[^].

Some more detail on how to do rendering and invalidation:
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^].

—SA
v2

My +5!
VJ Reddy 28-Apr-12 0:47am

Good points. 5!
SAKryukov 29-Apr-12 8:35am

--SA
SAKryukov 29-Apr-12 8:35am

Thank you, VJ.
--SA
lokeyg 15-Feb-13 10:07am

How to draw a line in rectangle according to ratio in your solution
Sergey Alexandrovich Kryukov 15-Feb-13 11:43am

What's a problem? It's geometry for elementary school. You have required aspect ratio, actual size, whatever. It's not according to my solution, it's a general thing...
—SA
Rate this:

## Solution 1

In the following code a `gap` variable is declared to maintain a gap on the sides of the `Rectangle`. The scale is calculated as the `Maximum `value of the `ratio `of the `Widths and Heights` of `PictureBox `and `Rectangle `respectively.
To run the sample, create a `Windows Forms` application and replace the contents of the `Form1 `code file with the following code and run the application.
```Public Class Form1
Dim button1 As New Button()
Dim pictureBox1 As New PictureBox()

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

button1.Text = "Draw Rectangle"
button1.Dock = DockStyle.Bottom

pictureBox1.Dock = DockStyle.Fill

pictureBox1.BackColor = Color.White
pictureBox1.Size = New Size(450, 400)
End Sub

Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim gap As Integer = 20
Dim rectangle1 As New Rectangle(0, 0, 10000, 4500)

Dim scale As Double = Math.Max(rectangle1.Width / (pictureBox1.Width - gap * 2), _
rectangle1.Height / (pictureBox1.Height - gap * 2))
Dim scaledRectangle = New Rectangle(gap, gap, _
rectangle1.Width / scale, rectangle1.Height / scale)
Dim graphics1 As Graphics = pictureBox1.CreateGraphics()
graphics1.DrawRectangle(Pens.Red, scaledRectangle)
graphics1.Dispose()
End Sub
End Class```
v2
SAKryukov 27-Apr-12 23:14pm

I voted 4 here, but I must note that using PictureBox is absolutely redundant. It's much better to use a custom control derived from Control and overriding OnPaint for rendering. With PictureBox, double work is done. This control is only good for simplest cases. When you go a bit more dynamic or interactive, PictureBox becomes more of a hassle rather than help and eats up extra resources, as well as development time.
--SA
SAKryukov 27-Apr-12 23:21pm

--SA
VJ Reddy 27-Apr-12 23:22pm

Thank you, SA.
You are absolutely correct. I hope, OP will take cognizance of your observation and implements it.

My +5!
VJ Reddy 28-Apr-12 0:12am

VJ Reddy 28-Apr-12 0:48am

Thank you.

Top Experts
Last 24hrsThis month
 Graeme_Grant 325 ppolymorphe 246 Peter Leow 160 Maciej Los 150 Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ) 150
 OriginalGriff 3,971 Peter Leow 3,051 Karthik Bangalore 2,375 ppolymorphe 2,353 Richard MacCutchan 1,594