Recently at work, we had to find a way to examine Gif images to find out if they were animated or not. It’s a good thing to know when dealing with banner advertising. The solution was to create a method that would return a struct containing the data about the Gif we needed. This is the struct:

public struct ImageInfo

{

  public int Width;

  public int Height;

  public bool IsAnimated;

  public bool IsLooped;

  public int AnimationLength; // In milliseconds

} >

And this is the method that returns the struct when given a file name:

using System.Drawing.Imaging;>

public static ImageInfo GetImageInfo(string path)

{

  ImageInfo info = new ImageInfo();

 

  using (System.Drawing.Image image = System.Drawing.Image.FromFile(path))

  {     

    info.Height = image.Height;

    info.Width = image.Width;

   

    if (image.RawFormat.Equals(ImageFormat.Gif))

    {

      if (System.Drawing.ImageAnimator.CanAnimate(image))

      {

        FrameDimension frameDimension = new FrameDimension(image.FrameDimensionsList[0]);

       

        int frameCount = image.GetFrameCount(frameDimension);

        int delay = 0;         

        int this_delay = 0;

        int index = 0;

 

        for (int f = 0; f < frameCount; f++)

        {

          this_delay = BitConverter.ToInt32(image.GetPropertyItem(20736).Value, index) * 10;

          delay += (this_delay < 100 ? 100 : this_delay);  // Minimum delay is 100 ms

          index += 4;

        }

 

        info.AnimationLength = delay;

        info.IsAnimated = true;

        info.IsLooped = BitConverter.ToInt16(image.GetPropertyItem(20737).Value, 0) != 1;

      }

    }

  }

 

  return info;
}

Example of use

Here is an example of a web page that writes the Gif info out in the response stream:

ImageInfo info = GetImageInfo(@"c:\artists.gif");

 

System.Text.StringBuilder sb = new System.Text.StringBuilder();

sb.AppendLine("animation length: " + info.AnimationLength);

sb.AppendLine("height: " + info.Height);

sb.AppendLine("width: " + info.Width);

sb.AppendLine("is animated: " + info.IsAnimated);

sb.AppendLine("is looped: " + info.IsLooped);

 

Response.Write(sb.ToString().Replace(Environment.NewLine, "<br />"));

This is the outcome of those lines of code on a web page:

animation length: 4340
height: 81
width: 63
is animated: True
is looped: True

I recently played with building a C# media player with built-in music library and wanted it to show album covers like Windows Media Player 11 does. It proved quite difficult because there is no free service for downloads of album covers as far as I know. Then I stumbled upon a Coding4Fun article that used the Amazon web service for retrieval of book covers and thought it might also work for album covers. Sure enough, it did.

You can start to download album covers in three simple steps that only take 5 minutes.

  • Register for free at Amazon to get your devtag (or use mine, see the code below)
  • Add a web reference to your Visual Studio project
  • Use a simple method to download the album art

After you’ve registered to get your devtag, you can add the web service to your Visual Studio project. The address of the Amazon web service is http://soap.amazon.com/schemas3/AmazonWebServices.wsdl. Be sure to rename the Web reference name to "Amazon" in the dialog below.

 

That’s all the groundwork; now just use this simple method to start downloading album covers.

using System;

using System.Web;

using System.Web.Services.Protocols;

using System.Net;

using System.IO;

using Amazon;

 

public bool DownloadAlbumArt(string albumArtist, string albumTitle, string dirName)

{

  using (WebClient webClient = new WebClient())

  {

    KeywordRequest req = new KeywordRequest();

    req.keyword = albumArtist + " " + albumTitle;

    // Remember to register to get your own devtag.

    req.devtag = "D1QFTS4VAA6C72";

    req.mode = "music";

    req.type = "lite";

    req.page = "1";

 

    using (AmazonSearchService search = new AmazonSearchService())

    {

      try

      {

        ProductInfo productInfo = search.KeywordSearchRequest(req);

 

        if (productInfo.Details.Length > 0)

        {

          string url = productInfo.Details[0].ImageUrlLarge;

          webClient.DownloadFile(url, dirName + "/" + albumTitle + ".jpg");

        }

 

        return productInfo.Details.Length > 0;

      }

      catch (SoapException)

      {

        return false;

      }

    }

  }
}

Then call the method with this line of code:

DownloadAlbumArt("metallica", "load", @"c:\");

That will download this album cover to C:\load.jpg.

This is another example of something that seems difficult at first, but actually is pretty easy.