如何从C#中的代码后面显示PictureBox [英] How to display a PictureBox from behind code in C#

查看:260
本文介绍了如何从C#中的代码后面显示PictureBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我的问题听起来很基本,但是我到处搜寻,却一无所获. 这是我的代码:

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);

}

这是附属功能:

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

squareImage是对应于Drawing.Image的属性. pos_x和pos_y是自定义的int32属性.

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

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

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

推荐答案

由于您使用的PictureBox是Winforms控件,因此您需要添加

Since the PictureBox that you are using is a Winforms Control you will need to add a WindowsFormsHost Control to 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.

即像这样的东西.

MainWindow.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

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);
        }
    }
}

这篇关于如何从C#中的代码后面显示PictureBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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