Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > JavaScriptConverter for Bitmaps

Reply
Thread Tools

JavaScriptConverter for Bitmaps

 
 
MattC
Guest
Posts: n/a
 
      05-11-2007
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.Chart DataTwo(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>


 
Reply With Quote
 
 
 
 
bruce barker
Guest
Posts: n/a
 
      05-11-2007
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.Chart DataTwo(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>
>
>

 
Reply With Quote
 
 
 
 
MattC
Guest
Posts: n/a
 
      05-11-2007
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.Chart DataTwo(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>



 
Reply With Quote
 
bruce barker
Guest
Posts: n/a
 
      05-11-2007
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.Chart DataTwo(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>

>
>

 
Reply With Quote
 
m
Guest
Posts: n/a
 
      05-11-2007
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.Chart DataTwo(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>

>>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
byte[]s, images and bitmaps with ASP.NET =?Utf-8?B?bWpjYXN0?= ASP .Net 4 08-21-2008 06:35 PM
How to write array of bitmaps directly to disk Rithesh Pai ASP .Net 1 08-25-2005 03:47 PM
Bitmaps in picturebox Steven_licciardinnoossppaamm@hotmail.com ASP .Net 1 10-24-2003 01:12 PM
Render bitmaps really fast Thomas Richter Java 6 10-21-2003 07:28 AM
Why is Internet Explorer now saving images as untitled bitmaps? Nun Soap Computer Support 2 09-29-2003 03:22 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57