Freigeben über


Which API should I use? – Using the .NET Compact Framework.

A couple of days ago I published an article which talked about a session I’m presenting at ESC East on choosing an API for your embedded device – The plan is to show building a FrameIt enabled Picture Frame (RSS Feed with image enclosures).

The application has three parts.

  1. Pull down the FrameIt XML feed
  2. Parse the XML feed – pull out the enclosures (URL to an image)
  3. Pull down the images and display them on the screen

I’ve written a simple application that clearly shows each of the three phases – Here’s the application running on the desktop.

Video: Picture Frame Demo Application

Here’s the same code (no source code changes) running on the Windows CE 6.0 Emulator

Video: Device Emulator Picture Frame Demo

And now the code…

1. Get the RSS Feed from FrameIt https://demofeed.frameit.com

    1:      WebRequest request = WebRequest.Create("https://demofeed.frameit.com");
    2:      WebResponse response = request.GetResponse();
    3:      StreamReader sr = new StreamReader(response.GetResponseStream());
    4:      string str=sr.ReadToEnd();
    5:      textBox1.Text = str;
    6:      response.Close();
    7:      StreamWriter sw = new StreamWriter(strXMLFile);
    8:      sw.Write(str);
    9:      sw.Close();

 

2. Parse the XML – In this case I’m building an ArrayList of the Image URLs.

    1:  private void ParseXML(string strFilename)
    2:  {
    3:      string strURL = null;
    4:      iArrayListPointer = 0;
    5:   
    6:      Console.WriteLine("Parsing - " + strFilename);
    7:      if (File.Exists(strFilename))
    8:      {
    9:          XmlTextReader reader = new XmlTextReader(strFilename);
   10:          while (reader.Read())
   11:          {
   12:              switch (reader.NodeType)
   13:              {
   14:                  case XmlNodeType.Element: // The node is an element.
   15:                      if (reader.Name == "enclosure")
   16:                      {
   17:                          Console.WriteLine("enclosure...");
   18:                          if (reader.AttributeCount > 0)
   19:                          {
   20:                              strURL = reader.GetAttribute("url");
   21:                              if (null != strURL)
   22:                              {
   23:                                  Console.WriteLine("strURL == " + strURL);
   24:                                  ar.Add(strURL);
   25:                              }
   26:                          }
   27:                      }
   28:                      Console.Write("<" + reader.Name);
   29:                      Console.WriteLine(">");
   30:                      break;
   31:              }
   32:          }
   33:          Console.WriteLine("---------------------------------------");
   34:      }
   35:  }

3. Now get the image (store in the local file system, so we don’t need to get it every time we display the image), and display the image in a PictureBox.

    1:  private void GetAndDisplayImage(string strURL,int iIndex)
    2:  {
    3:   
    4:      string strFile= @"c:\demo\foo"+iIndex.ToString()+".jpg";
    5:      if (!File.Exists(strFile))
    6:      {
    7:          WebRequest request = WebRequest.Create(strURL);
    8:          WebResponse response = request.GetResponse();
    9:          long iFileSize = response.ContentLength;
   10:          BinaryReader br = new BinaryReader(response.GetResponseStream());
   11:          FileStream fs = new FileStream(strFile, System.IO.FileMode.CreateNew);
   12:          BinaryWriter bw = new BinaryWriter(fs);
   13:          byte[] bArray = new byte[iFileSize];
   14:          int iRead = 0;
   15:          do
   16:          {
   17:              iRead = br.Read(bArray, 0, Convert.ToInt32(iFileSize));
   18:              bw.Write(bArray, 0, iRead);
   19:          }
   20:          while (iRead > 0);
   21:          bw.Flush();
   22:          bw.Close();
   23:          fs.Close();
   24:          br.Close();
   25:          response.Close();
   26:      }
   27:      pictureBox1.Image = new Bitmap(strFile);
   28:  }

Next step is to reproduce the same three steps in Native/Win32 code (rolls up sleeves), makes sure that Coffee cup is full… Look out for the next post coming soon (at least before ESC East).

- Mike

Comments