JavaScriptConverter for Bitmaps


M

MattC

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>
 
Ad

Advertisements

B

bruce barker

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)
 
M

MattC

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 said:
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)
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>
 
B

bruce barker

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)
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 said:
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)
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>
 
Ad

Advertisements

M

m

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 said:
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)
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 said:
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>
 

Top