Hi everyone
I am trying to create a simple wpf program that allows a user to select an image from a combo box and then where they click on th screen draw that image at the mouse co-ordinates. The prolem im having is i can load a single local file (a png file) into a bitmapImage source it works perfectly but if i try and store a number of bitmapImages in an array and try and load a local file it always creates a program crash. The first portion of code works perfectly except it can only use one image.
the second trys to store a number of bitmapImages in an array and load the local files in a for loop. Any help would be greatly appreciated.
All the best,
Martin
Program 1
Program 2
I am trying to create a simple wpf program that allows a user to select an image from a combo box and then where they click on th screen draw that image at the mouse co-ordinates. The prolem im having is i can load a single local file (a png file) into a bitmapImage source it works perfectly but if i try and store a number of bitmapImages in an array and try and load a local file it always creates a program crash. The first portion of code works perfectly except it can only use one image.
the second trys to store a number of bitmapImages in an array and load the local files in a for loop. Any help would be greatly appreciated.
All the best,
Martin
Program 1
Code:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace PlayingAbout
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
BitmapImage myBitMap = new BitmapImage();
BitmapImage myOtherBitMap = new BitmapImage();
BitmapImage[] myObjects = new BitmapImage[2];
String[] myFiles = new string[2];
int mouseX, mouseY, offset;
public Window1()
{
InitializeComponent();
myBitMap.BeginInit();
myBitMap.UriSource = new Uri(@"C:\Users\Martin\Documents\uniWork\C#programming\PlayingAbout\PlayingAbout\Images\FloorImage.png");
myBitMap.EndInit();
}
private void levelCanvas_MouseDown(object sender, MouseButtonEventArgs e)
{
mouseX = (int)e.GetPosition(levelCanvas).X;
mouseY = (int)e.GetPosition(levelCanvas).Y;
Image temp = new Image();
temp.Source = myBitMap;
levelCanvas.Children.Add(temp);
offset = levelCanvas.Children.Count -1;
temp.SetValue(Canvas.LeftProperty, (double)(mouseX/20)*20);
temp.SetValue(Canvas.TopProperty, (double)(mouseY/20)*20);
}
private void levelCanvas_MouseMove(object sender, MouseEventArgs e)
{
LabelX.Content = e.GetPosition(levelCanvas).X;
LabelY.Content = e.GetPosition(levelCanvas).Y;
}
}
}
Program 2
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace PlayingAbout
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
BitmapImage myOtherBitMap = new BitmapImage();
BitmapImage[] myObjects = new BitmapImage[2];
String[] myFiles = new string[2];
int mouseX, mouseY, offset;
public Window1()
{
InitializeComponent();
myFiles[0] = "floorImage.png";
myFiles[1] = "threerings.png";
for (int i = 0; i < myObjects.Length; i++)
{
myObjects[i].BeginInit();
myObjects[i].UriSource = new Uri(@"C:\Users\Martin\Documents\uniWork\C#programming\PlayingAbout\PlayingAbout\Images\" + myFiles[i]);
myObjects[i].EndInit();
}
}
private void levelCanvas_MouseDown(object sender, MouseButtonEventArgs e)
{
mouseX = (int)e.GetPosition(levelCanvas).X;
mouseY = (int)e.GetPosition(levelCanvas).Y;
Image temp = new Image();
temp.Source = myObjects[Objects.SelectedIndex];
levelCanvas.Children.Add(temp);
offset = levelCanvas.Children.Count -1;
temp.SetValue(Canvas.LeftProperty, (double)(mouseX/20)*20);
temp.SetValue(Canvas.TopProperty, (double)(mouseY/20)*20);
}
private void levelCanvas_MouseMove(object sender, MouseEventArgs e)
{
LabelX.Content = e.GetPosition(levelCanvas).X;
LabelY.Content = e.GetPosition(levelCanvas).Y;
}
}
}
Comment