14,695,329 members
0.00/5 (No votes)
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
Updated 26-Apr-12 16:37pm
v2
Comments
VJ Reddy 26-Apr-12 23:29pm

Thank you for accepting the solution.

Solution 2

I'm adding this answer for clarification of my comment to the answer by VJ.

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#[^].

See also this answer:
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
Comments
Prasad_Kulkarni 28-Apr-12 0:05am

My +5!

Thank you, Prasad,
--SA
VJ Reddy 28-Apr-12 0:47am

Good points. 5!

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

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
Controls.Add(button1)
AddHandler button1.Click, AddressOf button1_Click

pictureBox1.Dock = DockStyle.Fill
Controls.Add(pictureBox1)

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
Comments
Sergey Alexandrovich Kryukov 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
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.
Sergey Alexandrovich Kryukov 27-Apr-12 23:21pm

...so I added some detail on that in my answer -- please see.
--SA
VJ Reddy 28-Apr-12 0:48am

Had a quick look at the answer. Good points. Book marked the answer for studying all the links given in the answer.
Thank you.
Prasad_Kulkarni 28-Apr-12 0:04am

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

Thank you, Prasad.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 OriginalGriff 240 Richard Deeming 100 Sandeep Mewara 100 RickZeeland 80 Rick York 50
 OriginalGriff 6,069 Richard Deeming 3,165 Richard MacCutchan 2,824 CPallini 2,012 Patrice T 1,650

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900