C# 如何在C中从代码后面显示PictureBox#
我知道我的问题听起来很基本,但我到处找,什么也没找到。。 这是我的代码:C# 如何在C中从代码后面显示PictureBox#,c#,wpf,picturebox,system.drawing,drawimage,C#,Wpf,Picturebox,System.drawing,Drawimage,我知道我的问题听起来很基本,但我到处找,什么也没找到。。 这是我的代码: public MainWindow() { InitializeComponent(); Map newMap = new Map(); newMap.setMapStrategy(new SmallMapStrategy()); newMap.createMap(); System.Windows.Forms.PictureBox pictureBox1 = new Syst
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);
}
这是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属性
我希望在运行我的应用程序时看到图像…因为您使用的PictureBox是Winforms控件,您需要向Wpf表单添加一个,然后将PictureBox添加到该表单中。任何时候动态创建控件时,都需要将其添加到表单或容器对象中,否则它将不会显示 i、 像这样的 main window.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);
}
}
}
命名空间WpfApplication1
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
System.Windows.Forms.PictureBox picturebox1=新的System.Windows.Forms.PictureBox();
windowsFormsHost1.Child=picturebox1;
picturebox1.Paint+=新系统.Windows.Forms.PaintEventHandler(picturebox1\u Paint);
}
无效picturebox1_Paint(对象发送器,System.Windows.Forms.PaintEventArgs e)
{
System.Drawing.Bitmap bmp=新的System.Drawing.Bitmap(@“C:\Temp\test.jpg”);
System.Drawing.Point ulPoint=新的System.Drawing.Point(0,0);
e、 Graphics.DrawImage(bmp,ulPoint);
}
}
}
newMap.grid是什么类型的?这也是winforms还是wpf,您将其标记为wpf,但正在使用winforms控件,并且正在使用winforms系统。绘图方法newMap.grid是一个自定义类,与此无关。但是它包含上面显示的“afficher”方法,可能是,但是我的第二个问题是相关的。哦,对不起,没有看到它。该项目以WPF为重点,但我打算使用实际使用PictureBoxs和winforms的DrawImage(我猜是吧?),它说找不到WindowsFormsHost类型。我正在进行研究,以找到缺少的内容。@AdrienBrunelat它应该是Wpf工具箱中的一个控件,您可以将其添加到Wpf表单中。是的,它会自动添加我误读的引用。谢谢