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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • martinsmith160
    New Member
    • Dec 2009
    • 16

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

    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

    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;
            }        
        }
    }
    Last edited by tlhintoq; Jan 9 '10, 04:50 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    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.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      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?

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        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?

        Comment

        • martinsmith160
          New Member
          • Dec 2009
          • 16

          #5
          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

          Comment

          • vishal1082
            New Member
            • Apr 2009
            • 92

            #6
            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?

            Comment

            • martinsmith160
              New Member
              • Dec 2009
              • 16

              #7
              The exception that is thrown is this:

              An unhandled exception of type 'System.Windows .Markup.XamlPar seException' occurred in PresentationFra mework.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.

              Comment

              • vishal1082
                New Member
                • Apr 2009
                • 92

                #8
                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.Collecti ons.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:
                Code:
                public Window1()
                {
                    InitializeComponent();   
                    System.Collections.Generic.List<string> arr = new System.Collections.Generic.List<string>();
                    arr.Add("floorImage.png");
                    arr.Add("threerings.png");
                    myFiles = arr.ToArray();
                    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();
                    }           
                }

                Comment

                • martinsmith160
                  New Member
                  • Dec 2009
                  • 16

                  #9
                  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

                  Comment

                  Working...