Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

Displaying Gifs in silverlight RSS

13 replies

Last post Jun 13, 2009 08:04 AM by neopocott

(0)
  • EricDunn

    EricDunn

    Member

    18 Points

    11 Posts

    Displaying Gifs in silverlight

    Aug 21, 2007 10:10 PM | LINK

    Good day !

     I've searched the web a bit and havn't found any specific solutions to this problem (other than renaming them to jpg :P). Is there a way to either display a gif image, of export it to another format on the fly? My application will eventually use images from a server that I don't have write permissions on that uses gifs. I thought maybe using a downloader, downloading the image first then changing the format before displaying, but silverlight is missing namespaces that could be usefull for this.

    Anybody out there have an idea ?? I'm sure I'm not the 1st to have come accross a similar problem lol.

    Thank you very much,
    Eric.

     

  • luisabreu

    luisabreu

    Participant

    1676 Points

    612 Posts

    Re: Displaying Gifs in silverlight

    Aug 22, 2007 12:45 AM | LINK

    hello.

    well, i believe that's one of the scenarios that isnt't supported in the current version. as you've seen, currently you don't have support for working with images in the mini-clr

  • hufeng

    hufeng

    Member

    2 Points

    1 Post

    Re: Displaying Gifs in silverlight

    Aug 22, 2007 04:07 AM | LINK

    Hi, 

    May be you can setup a web page to convert image format. The page gets image url from request parameter, fetchs the real image from remoting server, converts the image's format to JPG or PNG and outputs the resulting data.

     

  • EricDunn

    EricDunn

    Member

    18 Points

    11 Posts

    Re: Displaying Gifs in silverlight

    Aug 22, 2007 02:32 PM | LINK

    Actually that's not a bad idea! My silverlight app is actually already calling out to a webservice to get the link of the address. I'll keep you posted.

     

    Eric

  • luisabreu

    luisabreu

    Participant

    1676 Points

    612 Posts

    Re: Displaying Gifs in silverlight

    Aug 22, 2007 04:39 PM | LINK

    hello.,

    that is, in fact, a valid scenario. if you follow it, you'll be better served by a custom handler. however, it still looks like a lot of work to me...

  • EricDunn

    EricDunn

    Member

    18 Points

    11 Posts

    Re: Displaying Gifs in silverlight

    Aug 22, 2007 04:44 PM | LINK

    I looked into it and .. It does seem like alot of work. Especially because the way things are set up, the only thing I can return for the picture is actually a string. So I need to transform the picture in a stream, transform the stream in to a string, send the string out threw the webservice, then transform it back to a stream, then try to figure out, within silverlight, how to transform from stream to image, all this without saving to a local file (for performance of course). Complicated and long task, especially since this won't be used in production anytime soon. Ohh well, I guess I'll keep my fingers crossed for MS to eventually support gifs !

     Thanks alot people !

    Eric.

  • luisabreu

    luisabreu

    Participant

    1676 Points

    612 Posts

    Re: Displaying Gifs in silverlight

    Aug 22, 2007 04:54 PM | LINK

    hello.

    hum, not sure about this one. i'm under the assumption that your handler should return the stream wiht the image bytes and you only need to pass an uri that points to your handler to the source of the image element....

     

     

  • EricDunn

    EricDunn

    Member

    18 Points

    11 Posts

    Re: Displaying Gifs in silverlight

    Aug 22, 2007 05:05 PM | LINK

    luisabreu

    hello.

    hum, not sure about this one. i'm under the assumption that your handler should return the stream wiht the image bytes and you only need to pass an uri that points to your handler to the source of the image element....

    Ok so ... I create a uri, and instead of a link, I put in the stream directly ?? How would it work ?? Can you elaborate please ?

    Eric.

  • luisabreu

    luisabreu

    Participant

    1676 Points

    612 Posts

    Re: Displaying Gifs in silverlight

    Aug 22, 2007 05:10 PM | LINK

    hello again.

    nop. i only see it working by doing something like this:

    Uri uri = new Uri( "localhandler.asxh?url=http://blabla.com/image.gif", urikind.relative);

    image.source = uri;

    then your handler would have to retrieve the gif, change it and write the stream back to the client (don't forget to set the correct mime type from the handler)...

  • EricDunn

    EricDunn

    Member

    18 Points

    11 Posts

    Re: Displaying Gifs in silverlight

    Aug 22, 2007 06:30 PM | LINK

    luisabreu

    hello again.

    nop. i only see it working by doing something like this:

    Uri uri = new Uri( "localhandler.asxh?url=http://blabla.com/image.gif", urikind.relative);

    image.source = uri;

    then your handler would have to retrieve the gif, change it and write the stream back to the client (don't forget to set the correct mime type from the handler)...

     Hello !
    Good news !
    it actually worked perfectly lol ... I don't know how "resource-heavy" it is, but I doubt that to be a major issue. Here's what the handler looks like (FYI):

    context.Response.ContentType = "image/jpeg";
    context.Response.BinaryWrite(GetBufferFromImage(imageLnk));

    private byte[] GetBufferFromImage(string imageLnk)
    {
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(imageLnk);
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Image myImage = Image.FromStream(response.GetResponseStream());
    MemoryStream ImageStream = new MemoryStream();
    myImage.Save(ImageStream,
    ImageFormat.Jpeg);
    ImageStream.Position = 0;
    byte[] Buffer = new byte[(int)ImageStream.Length];
    ImageStream.Read(Buffer, 0, (
    int)ImageStream.Length);
    return Buffer;
    }

    This is all in a ashx file so usage is pretty simple : http://(whatever)/myHandler.ashx?link=http://www.GifsIntoSL.net/myGifImage.gif

    Hope this helps !
    Eric.