Mobile Store - Image Threads (part 6)

by Chris 26. February 2010 18:20

One important feature in most mobile store apps is the ability to show images of products (or services). In most cases, it's not possible to store all those images on the mobile device. It could be because products are added or removed, that the images change over time, or that they would simple take up to much space in the device's memory (or even on a storage card).

A possible solution to these problems would be to get the images on demand over the network. But even if the image size was kept to a minimum, most wireless connections (especially if you are out of 3G coverage) wouldn't be able to deliver the images fast enough to create a responsive user experience. The solution could be to show a temporary image at once, load the images on background threads, and replace the temporary image when the downloads is complete. That is what I will show you how to do here.

When the image is first shown, which could be in the list or detail dialog, code similar to the following is run:

if(this.image == null)
{
   
this.image = Common.Values.LoadBitmap("dummy.jpg", 70, 52);
    Thread loadImageThread = new Thread(new ThreadStart(loadImage));
    loadImageThread.Start();
    loadingImage =
true;
}

If the image is not set, it's the first time this image is to be shown, we start by loading a dummy image while the real image is loading. Then we start a background thread to load the image, and it's code look like this:

private void loadImage()
{
   
Bitmap image = Common.Values.LoadBitmap(
        new Uri(Common.Values.ImageUrl + car.ImageFileName), 70, 52);
   
lock(this.image)
       
this.image = image;
    loadingImage =
false;
}

The download of the image is started, and when it's done, the image is set and the background thread ends. The actual loading of the image from an URL looks like this:

public Bitmap LoadBitmap(Uri uri, int width, int height)
{
   
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
   
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
   
Bitmap image = LoadBitmap(resp.GetResponseStream(), width, height);
    resp.GetResponseStream().Close();
   
return image;
}

Just a simple web request with the resulting stream directly used to create a bitmap image.

While the image is loading, the Paint event will do something similar to the following:

lock(this.image)
    g.DrawImage(image, position.Left, position.Top);
if(loadingImage)
    g.DrawString(
"Loading...", SmallFont, FontBrush,
        position.Left + 4, position.Top + 2);

 

 

 

The locking of the image variable is necessary to avoid thread concurrency problems, and we are also adding a text to the dummy image while the real one is loading.

This simple technique can be used in many situations to increase the user experience.

Currently rated 4.5 by 2 people

  • Currently 4.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Chris | Compact Framework | User Interface | Windows Mobile

Mobile Store - Service Client (part 5)

by Chris 19. February 2010 11:02

Now we have come to the client implementation part of the mobile store architecture, and the first thing to do is to create a connection (proxy) to the service. To do this, make sure you have the Power Toys for CF or, if you are running Vista or Windows 7, you should get your copy of the NETCFSvcUtil utility here. Take a look at this blog post on the details of doing this. All the code can be found in the CodePlex project for this series.

As we now have a connection from the mobile client to the server, the next step is to do the actual implementation of the user interface of the mobile client. I therefore recommend you to read through the various blog posts related to the ToucHUI framework. Most of what is implemented in the mobile client of the mobile store is covered in that series, and there are just a few additional features that I will talk about in the rest of this series.

Currently rated 4.0 by 1 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Architecture | Chris | Compact Framework | Windows Mobile

Mobile Store - Service (part 4)

by chris 12. February 2010 06:39

Let's continue with the implementation of the mobile store architecture, and now it's time for the middle tier with the business domain logic as well as the actual WCF service. Here is a screencast of me doing that:

Get Microsoft Silverlight

If you prefer, you can also download the screencast and watch it offline. All the code can be found in the CodePlex project for this series.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Architecture | Chris | Compact Framework | Windows Mobile

Mobile Store - Data Service (part 3)

by chris 5. February 2010 06:28

It's time to start the implementation of the mobile store architecture, and I will start with the server side. More specifically, I will start with the data services tier, and here is a screencast of me doing that:

Get Microsoft Silverlight

If you prefer, you can also download the screencast and watch it offline. All the code can be found in the CodePlex project for this series.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Architecture | Chris | Compact Framework | Windows Mobile

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen