如何从 C# 中的代码后面显示图片框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14142977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:50:14  来源:igfitidea点击:

How to display a PictureBox from behind code in C#

c#wpfpictureboxsystem.drawingdrawimage

提问by Adrien Brunelat

I know my question sounds basic, but i searched all over the place and found nothing.. this is my code :

我知道我的问题听起来很基本,但我搜索了所有地方,一无所获..这是我的代码:

public MainWindow()
{
    InitializeComponent();

    Map newMap = new Map();
    newMap.setMapStrategy(new SmallMapStrategy());
    newMap.createMap();


    System.Windows.Forms.PictureBox pictureBox1 = new System.Windows.Forms.PictureBox();
    pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(newMap.grid[3].afficher);

}

this is the afficher function :

这是afficher函数:

public override void afficher(object sender, PaintEventArgs e)
{
    e.Graphics.DrawImage(squareImage, pos_x, pos_y, 50, 50);
}

squareImage is an attribute corresponding to a Drawing.Image. pos_x and pos_y are custom int32 attributes.

squareImage 是一个与 Drawing.Image 对应的属性。pos_x 和 pos_y 是自定义的 int32 属性。

What i'd like is to SEE the image while running my application...

我想要的是在运行我的应用程序时查看图像...

采纳答案by Mark Hall

Since the PictureBox that you are using is a Winforms Control you will need to add a WindowsFormsHost Controlto your Wpf Form and add the PictureBox to that. Any time you dynamically create a control you need to add it to the Form or Container object otherwise it will not be shown.

由于您使用的 PictureBox 是 Winforms 控件,因此您需要将WindowsFormsHost 控件添加到您的 Wpf 表单并将 PictureBox 添加到其中。任何时候动态创建控件时都需要将其添加到 Form 或 Container 对象,否则将不会显示。

i.e. something like this.

即这样的事情。

MainWindow.xaml

主窗口.xaml

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <WindowsFormsHost Height="175" HorizontalAlignment="Left" Margin="10,10,0,0" Name="windowsFormsHost1" VerticalAlignment="Top" Width="255" />
    </Grid>
</Window>

MainWindow.xaml.cs

主窗口.xaml.cs

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            System.Windows.Forms.PictureBox picturebox1 = new System.Windows.Forms.PictureBox();
            windowsFormsHost1.Child = picturebox1;
            picturebox1.Paint += new System.Windows.Forms.PaintEventHandler(picturebox1_Paint);
        }

        void picturebox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(@"C:\Temp\test.jpg");
            System.Drawing.Point ulPoint = new System.Drawing.Point(0, 0);
            e.Graphics.DrawImage(bmp,ulPoint);
        }
    }
}