Main Site Documentation

Load a jpeg from PersistentStorage?


#1

ok … I give up how do I load a jpeg from the file system? I know how to do from a resource but I want to load it from a SD card or USB. (I know how to do it in full wpf and in the full framework, but can’t anything in the micro framework.)

-Eric


#2

I think the only way is to read the file into RAM then construct a Bitmap object from your data


#3

I have a pretty good example that illustrates this: http://files.chrisseto.com/UXL

Look in the extended weather info file.


#4

Here is the code that use both image from resource and from SD card.
If you want to use jpeg just change type to be Jpeg

Bitmap artImage = new Bitmap(data, Bitmap.BitmapImageType.Bmp);


using System;
using System.IO;

using Microsoft.SPOT;
using Microsoft.SPOT.IO;

using Microsoft.SPOT.Input;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Media;

using Microsoft.SPOT.Presentation.Controls;

using GHIElectronics.NETMF.IO;

namespace FEZ_Cobra_Window_Application1
{
    public class Program : Microsoft.SPOT.Application
    {
        public static void Main()
        {
            // ...
            // SD Card is inserted
            // Create a new storage device
            PersistentStorage sdPS = new PersistentStorage("SD");
            // Mount the file system
            sdPS.MountFileSystem();

            Program myApplication = new Program();

            Window mainWindow = myApplication.CreateWindow();

            // Create the object that configures the GPIO pins to buttons.
            GPIOButtonInputProvider inputProvider = new GPIOButtonInputProvider(null);

            // Start the application
            myApplication.Run(mainWindow);

            // Unmount
            sdPS.UnmountFileSystem();
        }

        private Window mainWindow;

        public Window CreateWindow()
        {
            // Create a window object and set its size to the
            // size of the display.
            mainWindow = new Window();
            mainWindow.Height = SystemMetrics.ScreenHeight;
            mainWindow.Width = SystemMetrics.ScreenWidth;

            Panel panel01 = new Panel();

            Image backgnd = new Image(Resources.GetBitmap(Resources.BitmapResources.MorningSky01));
            backgnd.HorizontalAlignment = HorizontalAlignment.Center;
            backgnd.VerticalAlignment = VerticalAlignment.Center;
            //mainWindow.Child = backgnd;
            panel01.Children.Add(backgnd);

            string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;

            FileStream fileHandle = new FileStream(rootDirectory + @ "\matisse001.bmp",
                                                   FileMode.Open, FileAccess.Read);

            // Create the byte array to hold the data
            byte[] data = new byte[fileHandle.Length];

            // Copy file to byte array
            fileHandle.Read(data, 0, data.Length);
            // finished!
            fileHandle.Close();

            Bitmap artImage = new Bitmap(data, Bitmap.BitmapImageType.Bmp);
            Image testArt = new Image(artImage);
            testArt.HorizontalAlignment = HorizontalAlignment.Center;
            testArt.VerticalAlignment = VerticalAlignment.Center;

            panel01.Children.Add(testArt);

            #region Display Text on the screen
            // Create a single text control.
            Text text = new Text();

            text.Font = Resources.GetFont(Resources.FontResources.small);

            // use text that store in resource as String1
            text.ForeColor = Colors.White;
            text.TextContent = Resources.GetString(Resources.StringResources.String1);
            text.HorizontalAlignment = Microsoft.SPOT.Presentation.HorizontalAlignment.Center;
            text.VerticalAlignment = Microsoft.SPOT.Presentation.VerticalAlignment.Center;

            // Add the text control to the window.
            panel01.Children.Add(text);
            //mainWindow.Child = text;
            #endregion Display Text on the screen

            // Connect the button handler to all of the buttons.
            //mainWindow.AddHandler(Buttons.ButtonUpEvent, new RoutedEventHandler(OnButtonUp), false);

            // add the text control to the window
            mainWindow.Child = panel01;

            // Set the window visibility to visible.
            mainWindow.Visibility = Visibility.Visible;

            // Attach the button focus to the window.
            Buttons.Focus(mainWindow);

            return mainWindow;
        }

        private void OnButtonUp(object sender, RoutedEventArgs evt)
        {
            ButtonEventArgs e = (ButtonEventArgs)evt;

            // Print the button code to the Visual Studio output window.
            Debug.Print(e.Button.ToString());
        }
    }
}

enjoys,

sam


#5

Sam,

Thanks! This is what I was looking for exactly:

FileStream fileHandle = new FileStream(rootDirectory + @ "\matisse001.bmp",
                                                   FileMode.Open, FileAccess.Read);
 
            // Create the byte array to hold the data
            byte[] data = new byte[fileHandle.Length];
 
            // Copy file to byte array
            fileHandle.Read(data, 0, data.Length);
            // finished!
            fileHandle.Close();
 
            Bitmap artImage = new Bitmap(data, Bitmap.BitmapImageType.Bmp);
            Image testArt = new Image(artImage);

I had seen the constructor for Bitmap, but didn’t investigate because I was looking for something that took a filename or a stream.

Thanks again

-Eric


#6

EricM:

Glad that helped!

sam


#7

I wrote a digital picture frame app with the info and I’ll be posting it to the Wiki soon. (Along with a full framework app for sizing images to the appropriate size for display by the Cobra or the Chipworkx displays.)

Went together really quickly.

-Eric