Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: graphics VB.NET drawing
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
Edited 26-Apr-12 16:37pm
v2
Comments
VJ Reddy at 26-Apr-12 23:29pm
   
Thank you for accepting the solution.
Rate this: bad
good
Please Sign up or sign in to vote.

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
  Permalink  
v2
Comments
Prasad_Kulkarni at 28-Apr-12 0:05am
   
My +5!
SAKryukov at 29-Apr-12 8:35am
   
Thank you, Prasad,
--SA
VJ Reddy at 28-Apr-12 0:47am
   
Good points. 5!
SAKryukov at 29-Apr-12 8:35am
   
Thank you, VJ.
--SA
lokeyg at 15-Feb-13 10:07am
   
How to draw a line in rectangle according to ratio in your solution
Sergey Alexandrovich Kryukov at 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: bad
good
Please Sign up or sign in to vote.

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
  Permalink  
v2
Comments
SAKryukov at 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 at 27-Apr-12 23:22pm
   
Thank you, SA.
You are absolutely correct. I hope, OP will take cognizance of your observation and implements it.
SAKryukov at 27-Apr-12 23:21pm
   
...so I added some detail on that in my answer -- please see.
--SA
VJ Reddy at 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 at 28-Apr-12 0:04am
   
My +5!
VJ Reddy at 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)

  Print Answers RSS
0 OriginalGriff 280
1 Sergey Alexandrovich Kryukov 279
2 CPallini 205
3 Maciej Los 197
4 Afzaal Ahmad Zeeshan 160
0 OriginalGriff 5,635
1 DamithSL 4,496
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 15 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100