图片框中的DrawRectangle ..... [英] DrawRectangle in a Picture box.....

查看:452
本文介绍了图片框中的DrawRectangle .....的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在现实生活中有一个真正的矩形大小我想要在图片框中绘制以扩展。



在过去(VB6天)我会得到如果是图片框的宽度,则将其除以真实矩形的大小到X比例。我也会为Y重复这个。



在图片框中绘制纠结的正常程序。



但是我可以通过缝合来实现这一点。



我想知道是否有人可以向我展示如何翻译现实世界点以扩展它的一些示例适用于图片框。





VS 2010项目

矩形为10000 x 4500 in真实生活和我的照片框是450,400



所以我想在这个图片框中安装10000 x 4500矩形(到Sacle)(在矩形周围有一些清晰的空间) )



问候

Stephan

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

推荐答案

我在添加这个回答澄清我对VJ答案的评论。



请参阅我的过去答案解释为什么你不应该使用 PictureBox 以及该怎么做:

如何从旧图纸中清除面板 [ ^ ]。



这个答案解释了如何在图像和控件中写入:

在C#中绘制一个矩形 [ ^ ]。



参见这个答案:

在图片框中附加图片 [ ^ ]。



关于如何进行渲染和失效的更多细节:

在mdi子表单之间绘制行 [ ^ ],

捕获面板上的图纸 [ ^ ],

什么样的俏皮方法是油漆? (DataGridViewImageCell.Paint(...)) [ ^ ]。



-SA
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


在以下代码中声明 gap 变量以保持 Rectangle 。该比例计算为宽度和高度的比率最大分别为 PictureBox Rectangle

要运行示例,创建一个 Windows窗体应用程序,并使用以下代码替换 Form1 代码文件的内容并运行该应用程序。 br />
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


这篇关于图片框中的DrawRectangle .....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆