JavaScriptConverter for Bitmaps

Discussion in 'ASP .Net' started by MattC, May 11, 2007.

  1. MattC

    MattC Guest

    Hi,

    I've got a webservice that returns a System.Drawing.Image. I'm calling the
    webservice via AJAX all goes well but my JSON converter seralize method
    gets called repeatedly and causes a
    An unhandled exception of type 'System.StackOverflowException' occurred in
    mscorlib.dll

    Hope you can help.
    TIA

    MattC - code bellow

    Service:

    [WebMethod]
    [ScriptMethod]
    public System.Drawing.Image ChartDataTwo(){...}


    AJAX Call:

    TestingGround.AJAXServices.LocalChartService.ChartDataTwo(SuccessCallBack,
    FailedCallback);

    And the callback on success

    function OnCompleteImageService(results)
    {
    var obj = Sys.Serialization.JavaScriptSerializer.deserialize(results);
    document.getElementById('ImageService').src = obj;
    }



    And my JSON Converter

    class AjaxImageConvert : JavaScriptConverter
    {

    public override IDictionary<string, object> Serialize(object obj,
    JavaScriptSerializer serializer)
    {
    Bitmap i;

    //obj comes in as a Bitmap check why later
    if (obj is Bitmap)
    {
    i = (Bitmap)obj;

    Dictionary<string, object> result = new Dictionary<string,
    object>();

    result["CustomImage"] = i;

    return result;
    }
    return new Dictionary<string, object>();
    }

    public override IEnumerable<Type> SupportedTypes
    {
    get { return new ReadOnlyCollection<Type>(new List<Type>(new
    Type[] { typeof(Bitmap) })); }
    }

    public override object Deserialize(IDictionary<string, object>
    dictionary, Type type, JavaScriptSerializer serializer)
    {
    object obj = (object)dictionary["CustomImage"];
    return obj;
    }

    }


    And registered

    <jsonSerialization maxJsonLength="500000">
    <converters>
    <add name="AjaxImageConvert"
    type="CustomControls.AjaxImageConvert"/>
    </converters>
    </jsonSerialization>
    MattC, May 11, 2007
    #1
    1. Advertising

  2. MattC

    bruce barker Guest

    javascript does not have a bitmap datatype (nor binary) so there is
    nothing to serialize to. the normal way would be to base64 encode the
    bitmap to string, then send the string.

    why are you passing an image thru ajax? javascript can not display a
    bitmap directly anyway. javascript can create a new Image() and set its
    url to fetch from the server, or set the src of an <img> to display an
    image. no ajax required.

    -- bruce (sqlwork.com)

    MattC wrote:
    > Hi,
    >
    > I've got a webservice that returns a System.Drawing.Image. I'm calling the
    > webservice via AJAX all goes well but my JSON converter seralize method
    > gets called repeatedly and causes a
    > An unhandled exception of type 'System.StackOverflowException' occurred in
    > mscorlib.dll
    >
    > Hope you can help.
    > TIA
    >
    > MattC - code bellow
    >
    > Service:
    >
    > [WebMethod]
    > [ScriptMethod]
    > public System.Drawing.Image ChartDataTwo(){...}
    >
    >
    > AJAX Call:
    >
    > TestingGround.AJAXServices.LocalChartService.ChartDataTwo(SuccessCallBack,
    > FailedCallback);
    >
    > And the callback on success
    >
    > function OnCompleteImageService(results)
    > {
    > var obj = Sys.Serialization.JavaScriptSerializer.deserialize(results);
    > document.getElementById('ImageService').src = obj;
    > }
    >
    >
    >
    > And my JSON Converter
    >
    > class AjaxImageConvert : JavaScriptConverter
    > {
    >
    > public override IDictionary<string, object> Serialize(object obj,
    > JavaScriptSerializer serializer)
    > {
    > Bitmap i;
    >
    > //obj comes in as a Bitmap check why later
    > if (obj is Bitmap)
    > {
    > i = (Bitmap)obj;
    >
    > Dictionary<string, object> result = new Dictionary<string,
    > object>();
    >
    > result["CustomImage"] = i;
    >
    > return result;
    > }
    > return new Dictionary<string, object>();
    > }
    >
    > public override IEnumerable<Type> SupportedTypes
    > {
    > get { return new ReadOnlyCollection<Type>(new List<Type>(new
    > Type[] { typeof(Bitmap) })); }
    > }
    >
    > public override object Deserialize(IDictionary<string, object>
    > dictionary, Type type, JavaScriptSerializer serializer)
    > {
    > object obj = (object)dictionary["CustomImage"];
    > return obj;
    > }
    >
    > }
    >
    >
    > And registered
    >
    > <jsonSerialization maxJsonLength="500000">
    > <converters>
    > <add name="AjaxImageConvert"
    > type="CustomControls.AjaxImageConvert"/>
    > </converters>
    > </jsonSerialization>
    >
    >
    bruce barker, May 11, 2007
    #2
    1. Advertising

  3. MattC

    MattC Guest

    Yeah I tried the base64 route but IE ignores the data:image/gif;base64 part
    and is limted to 1024bytes.

    I can do it using using the src set to the webservice but IE only makes 2
    HTTP requests at the same time. I thought possibly that the AJAX
    webrequests would not be bound by IE's request limit and allow, say all 20
    requests to fire off to the webservice.

    MattC


    "bruce barker" <> wrote in message
    news:eF5H0E%...
    > javascript does not have a bitmap datatype (nor binary) so there is
    > nothing to serialize to. the normal way would be to base64 encode the
    > bitmap to string, then send the string.
    >
    > why are you passing an image thru ajax? javascript can not display a
    > bitmap directly anyway. javascript can create a new Image() and set its
    > url to fetch from the server, or set the src of an <img> to display an
    > image. no ajax required.
    >
    > -- bruce (sqlwork.com)
    >
    > MattC wrote:
    >> Hi,
    >>
    >> I've got a webservice that returns a System.Drawing.Image. I'm calling
    >> the webservice via AJAX all goes well but my JSON converter seralize
    >> method gets called repeatedly and causes a
    >> An unhandled exception of type 'System.StackOverflowException' occurred
    >> in mscorlib.dll
    >>
    >> Hope you can help.
    >> TIA
    >>
    >> MattC - code bellow
    >>
    >> Service:
    >>
    >> [WebMethod]
    >> [ScriptMethod]
    >> public System.Drawing.Image ChartDataTwo(){...}
    >>
    >>
    >> AJAX Call:
    >>
    >> TestingGround.AJAXServices.LocalChartService.ChartDataTwo(SuccessCallBack,
    >> FailedCallback);
    >>
    >> And the callback on success
    >>
    >> function OnCompleteImageService(results)
    >> {
    >> var obj =
    >> Sys.Serialization.JavaScriptSerializer.deserialize(results);
    >> document.getElementById('ImageService').src = obj;
    >> }
    >>
    >>
    >>
    >> And my JSON Converter
    >>
    >> class AjaxImageConvert : JavaScriptConverter
    >> {
    >>
    >> public override IDictionary<string, object> Serialize(object obj,
    >> JavaScriptSerializer serializer)
    >> {
    >> Bitmap i;
    >>
    >> //obj comes in as a Bitmap check why later
    >> if (obj is Bitmap)
    >> {
    >> i = (Bitmap)obj;
    >>
    >> Dictionary<string, object> result = new
    >> Dictionary<string, object>();
    >>
    >> result["CustomImage"] = i;
    >>
    >> return result;
    >> }
    >> return new Dictionary<string, object>();
    >> }
    >>
    >> public override IEnumerable<Type> SupportedTypes
    >> {
    >> get { return new ReadOnlyCollection<Type>(new List<Type>(new
    >> Type[] { typeof(Bitmap) })); }
    >> }
    >>
    >> public override object Deserialize(IDictionary<string, object>
    >> dictionary, Type type, JavaScriptSerializer serializer)
    >> {
    >> object obj = (object)dictionary["CustomImage"];
    >> return obj;
    >> }
    >>
    >> }
    >>
    >>
    >> And registered
    >>
    >> <jsonSerialization maxJsonLength="500000">
    >> <converters>
    >> <add name="AjaxImageConvert"
    >> type="CustomControls.AjaxImageConvert"/>
    >> </converters>
    >> </jsonSerialization>
    MattC, May 11, 2007
    #3
  4. MattC

    bruce barker Guest

    ajax is limted to 2 concurrent requests also (IE treats it as another
    http request). this is to be friendly to web servers.

    note: if you use session with your webservice you will be limited to one
    request at a time as requests for the same session are queued on the
    server side.

    if you want performance, iis serves up images a lot faster then asp.net
    can.

    -- bruce (sqlwork.com)

    MattC wrote:
    > Yeah I tried the base64 route but IE ignores the data:image/gif;base64 part
    > and is limted to 1024bytes.
    >
    > I can do it using using the src set to the webservice but IE only makes 2
    > HTTP requests at the same time. I thought possibly that the AJAX
    > webrequests would not be bound by IE's request limit and allow, say all 20
    > requests to fire off to the webservice.
    >
    > MattC
    >
    >
    > "bruce barker" <> wrote in message
    > news:eF5H0E%...
    >> javascript does not have a bitmap datatype (nor binary) so there is
    >> nothing to serialize to. the normal way would be to base64 encode the
    >> bitmap to string, then send the string.
    >>
    >> why are you passing an image thru ajax? javascript can not display a
    >> bitmap directly anyway. javascript can create a new Image() and set its
    >> url to fetch from the server, or set the src of an <img> to display an
    >> image. no ajax required.
    >>
    >> -- bruce (sqlwork.com)
    >>
    >> MattC wrote:
    >>> Hi,
    >>>
    >>> I've got a webservice that returns a System.Drawing.Image. I'm calling
    >>> the webservice via AJAX all goes well but my JSON converter seralize
    >>> method gets called repeatedly and causes a
    >>> An unhandled exception of type 'System.StackOverflowException' occurred
    >>> in mscorlib.dll
    >>>
    >>> Hope you can help.
    >>> TIA
    >>>
    >>> MattC - code bellow
    >>>
    >>> Service:
    >>>
    >>> [WebMethod]
    >>> [ScriptMethod]
    >>> public System.Drawing.Image ChartDataTwo(){...}
    >>>
    >>>
    >>> AJAX Call:
    >>>
    >>> TestingGround.AJAXServices.LocalChartService.ChartDataTwo(SuccessCallBack,
    >>> FailedCallback);
    >>>
    >>> And the callback on success
    >>>
    >>> function OnCompleteImageService(results)
    >>> {
    >>> var obj =
    >>> Sys.Serialization.JavaScriptSerializer.deserialize(results);
    >>> document.getElementById('ImageService').src = obj;
    >>> }
    >>>
    >>>
    >>>
    >>> And my JSON Converter
    >>>
    >>> class AjaxImageConvert : JavaScriptConverter
    >>> {
    >>>
    >>> public override IDictionary<string, object> Serialize(object obj,
    >>> JavaScriptSerializer serializer)
    >>> {
    >>> Bitmap i;
    >>>
    >>> //obj comes in as a Bitmap check why later
    >>> if (obj is Bitmap)
    >>> {
    >>> i = (Bitmap)obj;
    >>>
    >>> Dictionary<string, object> result = new
    >>> Dictionary<string, object>();
    >>>
    >>> result["CustomImage"] = i;
    >>>
    >>> return result;
    >>> }
    >>> return new Dictionary<string, object>();
    >>> }
    >>>
    >>> public override IEnumerable<Type> SupportedTypes
    >>> {
    >>> get { return new ReadOnlyCollection<Type>(new List<Type>(new
    >>> Type[] { typeof(Bitmap) })); }
    >>> }
    >>>
    >>> public override object Deserialize(IDictionary<string, object>
    >>> dictionary, Type type, JavaScriptSerializer serializer)
    >>> {
    >>> object obj = (object)dictionary["CustomImage"];
    >>> return obj;
    >>> }
    >>>
    >>> }
    >>>
    >>>
    >>> And registered
    >>>
    >>> <jsonSerialization maxJsonLength="500000">
    >>> <converters>
    >>> <add name="AjaxImageConvert"
    >>> type="CustomControls.AjaxImageConvert"/>
    >>> </converters>
    >>> </jsonSerialization>

    >
    >
    bruce barker, May 11, 2007
    #4
  5. MattC

    m Guest

    Problem is the images don't exist until requested, i.e they are generated
    from some backend stats analysis.
    I thought of maybe using a custom handler that implements IHttpAsyncHandler
    and set the maxconnections section of the web.config to a higher number.

    Would that do it. I really want to try and get the page (which has any where
    up to 10 of these images) to be as quick to download.

    Is there anyway to break to 2 request barrier. I have full control over
    settings on the web app and webservice.

    Thanks for your help so far.

    MattC

    "bruce barker" <> wrote in message
    news:%23CRaiN$...
    > ajax is limted to 2 concurrent requests also (IE treats it as another http
    > request). this is to be friendly to web servers.
    >
    > note: if you use session with your webservice you will be limited to one
    > request at a time as requests for the same session are queued on the
    > server side.
    >
    > if you want performance, iis serves up images a lot faster then asp.net
    > can.
    >
    > -- bruce (sqlwork.com)
    >
    > MattC wrote:
    >> Yeah I tried the base64 route but IE ignores the data:image/gif;base64
    >> part and is limted to 1024bytes.
    >>
    >> I can do it using using the src set to the webservice but IE only makes 2
    >> HTTP requests at the same time. I thought possibly that the AJAX
    >> webrequests would not be bound by IE's request limit and allow, say all
    >> 20 requests to fire off to the webservice.
    >>
    >> MattC
    >>
    >>
    >> "bruce barker" <> wrote in message
    >> news:eF5H0E%...
    >>> javascript does not have a bitmap datatype (nor binary) so there is
    >>> nothing to serialize to. the normal way would be to base64 encode the
    >>> bitmap to string, then send the string.
    >>>
    >>> why are you passing an image thru ajax? javascript can not display a
    >>> bitmap directly anyway. javascript can create a new Image() and set its
    >>> url to fetch from the server, or set the src of an <img> to display an
    >>> image. no ajax required.
    >>>
    >>> -- bruce (sqlwork.com)
    >>>
    >>> MattC wrote:
    >>>> Hi,
    >>>>
    >>>> I've got a webservice that returns a System.Drawing.Image. I'm calling
    >>>> the webservice via AJAX all goes well but my JSON converter seralize
    >>>> method gets called repeatedly and causes a
    >>>> An unhandled exception of type 'System.StackOverflowException' occurred
    >>>> in mscorlib.dll
    >>>>
    >>>> Hope you can help.
    >>>> TIA
    >>>>
    >>>> MattC - code bellow
    >>>>
    >>>> Service:
    >>>>
    >>>> [WebMethod]
    >>>> [ScriptMethod]
    >>>> public System.Drawing.Image ChartDataTwo(){...}
    >>>>
    >>>>
    >>>> AJAX Call:
    >>>>
    >>>> TestingGround.AJAXServices.LocalChartService.ChartDataTwo(SuccessCallBack,
    >>>> FailedCallback);
    >>>>
    >>>> And the callback on success
    >>>>
    >>>> function OnCompleteImageService(results)
    >>>> {
    >>>> var obj =
    >>>> Sys.Serialization.JavaScriptSerializer.deserialize(results);
    >>>> document.getElementById('ImageService').src = obj;
    >>>> }
    >>>>
    >>>>
    >>>>
    >>>> And my JSON Converter
    >>>>
    >>>> class AjaxImageConvert : JavaScriptConverter
    >>>> {
    >>>>
    >>>> public override IDictionary<string, object> Serialize(object
    >>>> obj, JavaScriptSerializer serializer)
    >>>> {
    >>>> Bitmap i;
    >>>>
    >>>> //obj comes in as a Bitmap check why later
    >>>> if (obj is Bitmap)
    >>>> {
    >>>> i = (Bitmap)obj;
    >>>>
    >>>> Dictionary<string, object> result = new
    >>>> Dictionary<string, object>();
    >>>>
    >>>> result["CustomImage"] = i;
    >>>>
    >>>> return result;
    >>>> }
    >>>> return new Dictionary<string, object>();
    >>>> }
    >>>>
    >>>> public override IEnumerable<Type> SupportedTypes
    >>>> {
    >>>> get { return new ReadOnlyCollection<Type>(new
    >>>> List<Type>(new Type[] { typeof(Bitmap) })); }
    >>>> }
    >>>>
    >>>> public override object Deserialize(IDictionary<string, object>
    >>>> dictionary, Type type, JavaScriptSerializer serializer)
    >>>> {
    >>>> object obj = (object)dictionary["CustomImage"];
    >>>> return obj;
    >>>> }
    >>>>
    >>>> }
    >>>>
    >>>>
    >>>> And registered
    >>>>
    >>>> <jsonSerialization maxJsonLength="500000">
    >>>> <converters>
    >>>> <add name="AjaxImageConvert"
    >>>> type="CustomControls.AjaxImageConvert"/>
    >>>> </converters>
    >>>> </jsonSerialization>

    >>
    m, May 11, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rithesh Pai
    Replies:
    1
    Views:
    446
  2. Bitmaps in picturebox

    , Oct 24, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    433
    Kevin Spencer
    Oct 24, 2003
  3. =?Utf-8?B?bWpjYXN0?=

    byte[]s, images and bitmaps with ASP.NET

    =?Utf-8?B?bWpjYXN0?=, Jun 7, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    12,645
    Pedro Lopez
    Aug 21, 2008
  4. Thomas Richter

    Render bitmaps really fast

    Thomas Richter, Oct 20, 2003, in forum: Java
    Replies:
    6
    Views:
    597
    Thomas Weidenfeller
    Oct 21, 2003
  5. Michael Schwab

    Artist needs help with bitmaps

    Michael Schwab, Nov 17, 2003, in forum: C Programming
    Replies:
    5
    Views:
    343
    Michael Schwab
    Nov 18, 2003
Loading...

Share This Page