从另一个表格的按钮在图片框上绘制矩形

时间:2015-07-17 09:20:46

标签: vb.net visual-studio-2010 visual-studio-2012

我的表单上有一个namespace Test1 { /// <summary> /// Interaktionslogik für MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void button1_Click(object sender, RoutedEventArgs e) { TabItem item = new TabItem(); item.Header = "Some header"; //item.Content = < your content> TabControl.Items.Add(item); } } } ,另一个表单上有一个按钮。一旦我按下另一个表单上的按钮,我将如何picturebox在第一个表单上?

picturebox1_paint

这就是我所拥有的,为了让它发挥作用我需要改变什么?

1 个答案:

答案 0 :(得分:0)

在Form1中,添加

Sub PaintImage(rectangle As Rectangle)
    ' create image in size of PictureBox1
    Dim image As New Bitmap(PictureBox1.Width, PictureBox1.Height)

    ' paint on created image
    Using gr As Graphics = Graphics.FromImage(image)
        gr.DrawRectangle(New Pen(Color.Red), rectangle)            
    End Using

    ' display finished image in PictureBox1
    PictureBox1.Image = image
End Sub

并使用按钮从表单中调用它:

Sub Button1_Click(sender as Object, e as EventArgs) Handles Button1.Click        
    Form1.PaintImage(New Rectangle(x, y, width, height)) ' assume we already have x, y, w, h
End Sub

您可以删除问题中显示的Paint()事件处理程序。其目的是重新标记PictureBox1.Image,如果它的大小或可见性发生变化,在大多数情况下,您可以将其保留原样。