By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,667 Members | 2,073 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,667 IT Pros & Developers. It's quick & easy.

How do you load a local file as a source for a bitmapImage stored in an array

P: 16
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

Expand|Select|Wrap|Line Numbers
  1. using System.Windows;
  2. using System.Windows.Controls;
  3. using System.Windows.Data;
  4. using System.Windows.Documents;
  5. using System.Windows.Input;
  6. using System.Windows.Media;
  7. using System.Windows.Media.Imaging;
  8. using System.Windows.Navigation;
  9. using System.Windows.Shapes;
  10.  
  11. namespace PlayingAbout
  12. {
  13.     /// <summary>
  14.     /// Interaction logic for Window1.xaml
  15.     /// </summary>
  16.     public partial class Window1 : Window
  17.     {
  18.         BitmapImage myBitMap = new BitmapImage();
  19.         BitmapImage myOtherBitMap = new BitmapImage();
  20.         BitmapImage[] myObjects = new BitmapImage[2];
  21.         String[] myFiles = new string[2];       
  22.         int mouseX, mouseY, offset;
  23.  
  24.         public Window1()
  25.         {
  26.             InitializeComponent();
  27.             myBitMap.BeginInit();
  28.             myBitMap.UriSource = new Uri(@"C:\Users\Martin\Documents\uniWork\C#programming\PlayingAbout\PlayingAbout\Images\FloorImage.png");
  29.             myBitMap.EndInit();            
  30.         }
  31.  
  32.         private void levelCanvas_MouseDown(object sender, MouseButtonEventArgs e)
  33.         {
  34.             mouseX = (int)e.GetPosition(levelCanvas).X;
  35.             mouseY = (int)e.GetPosition(levelCanvas).Y;
  36.             Image temp = new Image();
  37.             temp.Source = myBitMap;
  38.             levelCanvas.Children.Add(temp);
  39.             offset = levelCanvas.Children.Count -1;
  40.             temp.SetValue(Canvas.LeftProperty, (double)(mouseX/20)*20);
  41.             temp.SetValue(Canvas.TopProperty, (double)(mouseY/20)*20);  
  42.         }
  43.  
  44.         private void levelCanvas_MouseMove(object sender, MouseEventArgs e)
  45.         {
  46.             LabelX.Content = e.GetPosition(levelCanvas).X;
  47.             LabelY.Content = e.GetPosition(levelCanvas).Y;
  48.         }        
  49.     }
  50. }

Program 2

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14.  
  15. namespace PlayingAbout
  16. {
  17.     /// <summary>
  18.     /// Interaction logic for Window1.xaml
  19.     /// </summary>
  20.     public partial class Window1 : Window
  21.     {
  22.  
  23.         BitmapImage myOtherBitMap = new BitmapImage();
  24.         BitmapImage[] myObjects = new BitmapImage[2];
  25.         String[] myFiles = new string[2];    
  26.         int mouseX, mouseY, offset;
  27.  
  28.         public Window1()
  29.         {
  30.             InitializeComponent();   
  31.             myFiles[0] = "floorImage.png";
  32.             myFiles[1] = "threerings.png";
  33.             for (int i = 0; i < myObjects.Length; i++)
  34.             {
  35.                 myObjects[i].BeginInit();
  36.                 myObjects[i].UriSource = new Uri(@"C:\Users\Martin\Documents\uniWork\C#programming\PlayingAbout\PlayingAbout\Images\" + myFiles[i]);
  37.                 myObjects[i].EndInit();
  38.             }           
  39.         }
  40.  
  41.         private void levelCanvas_MouseDown(object sender, MouseButtonEventArgs e)
  42.         {
  43.             mouseX = (int)e.GetPosition(levelCanvas).X;
  44.             mouseY = (int)e.GetPosition(levelCanvas).Y;
  45.             Image temp = new Image();
  46.             temp.Source = myObjects[Objects.SelectedIndex];
  47.             levelCanvas.Children.Add(temp);
  48.             offset = levelCanvas.Children.Count -1;
  49.             temp.SetValue(Canvas.LeftProperty, (double)(mouseX/20)*20);
  50.             temp.SetValue(Canvas.TopProperty, (double)(mouseY/20)*20);  
  51.         }
  52.  
  53.         private void levelCanvas_MouseMove(object sender, MouseEventArgs e)
  54.         {
  55.             LabelX.Content = e.GetPosition(levelCanvas).X;
  56.             LabelY.Content = e.GetPosition(levelCanvas).Y;
  57.         }        
  58.     }
  59. }
Jan 9 '10 #1
Share this Question
Share on Google+
8 Replies


tlhintoq
Expert 2.5K+
P: 3,525
TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
Jan 9 '10 #2

tlhintoq
Expert 2.5K+
P: 3,525
local file it always creates a program crash.
Give the volunteers here something more to go on, please. What is the error you get? What line of code produces the error?
Jan 9 '10 #3

tlhintoq
Expert 2.5K+
P: 3,525
local file it always creates a program crash.
Give the volunteers here something more to go on, please. What is the error you get? What line of code produces the error?
Jan 9 '10 #4

P: 16
Hi all

I will use the code tags from now on sorry. The main problem is my program at its heart creates images from bitmaps at runtime. So for example when the user clicks on the canvas the image is created and drawn to the screen, this works with one image at the minute. What i want to do now is create an array of bitmapImages() each of which are loaded from local png files on my computer. The main problem I have is if i try to load local files into the bitmapImages in each array element the program throws an exception and the program crashes. Im puzzled because the code to load one local file into a bitmapImage works but as soon as i store it in an array it causes problems.
The program 1 code is the program that only uses one image and program 2 is my attemp to store them in an array.
Thanks for the response.
Martin
Jan 11 '10 #5

P: 92
The main problem I have is if i try to load local files into the bitmapImages in each array element the program throws an exception and the program crashes.
What exactly the exception is?
Jan 12 '10 #6

P: 16
The exception that is thrown is this:

An unhandled exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll

Additional information: Cannot create instance of 'Window1' defined in assembly 'PlayingAbout, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Exception has been thrown by the target of an invocation. Error in markup file 'Window1.xaml' Line 1 Position 9.

The problem is im quite sure its not anything to do with the markup because when i debug the program the program crashes at the line where i load the local file into the bitmapImage. Its line 36 in program 2 above.
Jan 13 '10 #7

P: 92
i m not expert in WPF but if its same as WinForms, then on Line 32 you are not loading you are just adding a value to a array, and if that gives error.. its strange... i dont know what is problem, you could try using List class in System.Collections.Generic and add values then use its ToArray() and insert the values in your myFiles[] string array, again i dont know if List class is available in WPF.

Code would be something like this:
Expand|Select|Wrap|Line Numbers
  1. public Window1()
  2. {
  3.     InitializeComponent();   
  4.     System.Collections.Generic.List<string> arr = new System.Collections.Generic.List<string>();
  5.     arr.Add("floorImage.png");
  6.     arr.Add("threerings.png");
  7.     myFiles = arr.ToArray();
  8.     for (int i = 0; i < myObjects.Length; i++)
  9.     {
  10.        myObjects[i].BeginInit();
  11.        myObjects[i].UriSource = new Uri(@"C:\Users\Martin\Documents\uniWork\C#programming\PlayingAbout\PlayingAbout\Images\" + myFiles[i]);
  12.        myObjects[i].EndInit();
  13.     }           
  14. }
Jan 13 '10 #8

P: 16
Thank you all for your responses. I have just solved my problem and it was a rookie mistake. I created an array of bitmapimages but i hadn't populated them so they were all initialized to null. I feel a little stupid now.

All the best,
Martin
Jan 13 '10 #9

Post your reply

Sign in to post your reply or Sign up for a free account.