4

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 :

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.

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

Adrien Brunelat
  • 4,492
  • 4
  • 29
  • 42
  • What type is newMap.grid ? – Mark Hall Jan 03 '13 at 16:37
  • 3
    also is this winforms or wpf, you have it tagged Wpf but are using a Winforms control and are using the Winforms System.Drawing Methods – Mark Hall Jan 03 '13 at 16:39
  • newMap.grid is a custom class, it's not really relevant here. But it contains the "afficher" method as shown above – Adrien Brunelat Jan 03 '13 at 16:43
  • That may be, but my second question is relevant. – Mark Hall Jan 03 '13 at 16:44
  • oh sorry, didn't see it. The project is WPF focused but i'm meant to use DrawImage that actually uses pictureBoxes and winforms (i guess?) – Adrien Brunelat Jan 03 '13 at 16:48
  • The type of newMap.grid is defined in another project that is not using WPF. It's a .lib C# project. That means i cannot use Controls into afficher's definition. – Adrien Brunelat Jan 03 '13 at 16:58
  • is afficher being called? or is the problem that it is being called but you don't see anything in the picture box? do you even see the picture box on the form? Are you missing this.controls.add(pictureBox1), or is that just not in the code you are showing? – Dan OConnell Jan 03 '13 at 17:01

1 Answers1

12

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.

But first, add these references:

System.Windows.Forms
WindowsFormsIntegration

Now write code something like this.

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);
        }
    }
}
Omri Attiya
  • 3,917
  • 3
  • 19
  • 35
Mark Hall
  • 53,938
  • 9
  • 94
  • 111