Click here to Skip to main content
15,039,714 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hi this is my first WCF,

I have read the past day on how to this, and for the most part I have a good understanding of it ( I think ), but it seems I'm just missing something a little bit. allow me to explain.

I have a project currently that draws fractals and is multi-threaded & parallelized the heart of this project is The function "CalcFractel()" the current version of the project is running and debugged. Now for the next version of this project I need to offload the processing portion to a webservice thats where WCF comes in. From what I have read both here with the great tutorials I have understood the following the IServices.cs is the interface which you need to implement in services.svc.cs. So here is what I have added to the auto generated IServices.cs I'm using VS 2010 Ultimate I don't if that matters thought I mention it. And I have already checked the "Generate asynchronous operations" check box under the Service references list.
       Bitmap CalcFractel(int width, int height, float complexX, float complexY);

on the implementation side (ie Services.cs)I did the following.
public Bitmap CalcFractel(int width, int height, float complexX, float complexY)
            Bitmap bmp = new Bitmap(width, height);
            if ((width != 0) && (height != 0))
                Graphics g = Graphics.FromImage(bmp);
                SolidBrush b = new SolidBrush(Color.Aqua);
                Rectangle r = new Rectangle(0, 0, width, height);
                g.FillRectangle(b, r);
                return bmp;
                return null;

On the client side I added the following code... so in the Event Handler
"client_GetDataCompleted(object sender, ServiceReference1.GetDataCompletedEventArgs e)" I don't know how to get at the bitmap.. I thought e.result would have what i was returning in the function on the server side but I just get a type string. This is the the last piece I need I figured the rest out on my own, and if you can help well any help would be greatly appreciated.
<pre lang="msil">private void Test_Click(object sender, EventArgs e)

            ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
            client.GetDataCompleted += new EventHandler<ServiceReference1.GetDataCompletedEventArgs>(client_GetDataCompleted);

and the Event handler
oid client_GetDataCompleted(object sender, ServiceReference1.GetDataCompletedEventArgs e)        
            //e.result does not give me the bitmap
            //returned by the CalcFractel function on the server side
            //How do I get to it?
            Bitmap bmp = e.Result.

:-O :confused:
Does GetFractel mean fractal? Is this just a problem of bitmap serialization of different types of bitmaps, or what?
onyxbird 8-Mar-11 15:38pm
I have CalcFractel() and yes it means fractel. from what Michael said it seems serialization is a problem, but I don't know yet how to implement his solution.
I did not say "fractel", I said "fractal" See the difference?.. :-)
onyxbird 8-Mar-11 20:48pm
sorry yes it is fractal. BTW if you have any idea of how to implement the BASE64 stream or a coding sample I would greatly appreciate it.


The samples are provided by Microsoft in help to the base64 methods of Convert, see my references.
onyxbird 8-Mar-11 21:38pm
thanks, I found what i was looking for but then I realized something.... since the client side is multi-thread and the Async call will not block (I'm just kind of envisioning a future problem here) wouldn't returning such a huge string over a network connection be bad practice especially since packets are suppose to be only so big I don't recall the specific size but I know 1-2MB is way bigger then a single packet... and it seems like I'm bursting this data to the client is that bad practice?

Michael Bookatz 8-Mar-11 6:43am
Your problem is going to be serialiization of the bit map. You can't serialize Abstract classes unless you tell WCF what the actual classes it can expect are. The other way to do it is to use a BASE64 stream and write the image to that
Michael, I suggest you work this into Answer, would be a good one.
onyxbird 8-Mar-11 15:40pm
Michael thanks for the idea if you could please provide a quick little example of how to write it to a BASE64 stream, and how to convert it back to a bitmap I will also do some research on it too to see if I could find something. thanks.
onyxbird 9-Mar-11 0:17am
okay I have the bitmap to base64 conversion and vise versa down, but the problem is now that the GetDataCompletedEventArgs handler client_GetDataCompleted() never gets called I followed the CalcFractel() on the server side and it does hit & return on the server side now I don't know why the client side never gets a invocation of the GetDataCompletedEventArgs
onyxbird 9-Mar-11 5:02am
Okay I figured that portion out, but now when I draw the bitmap it throws and out of memory exception. I checked the bitmap it is not null, and looked at the data in the bitmap it seems to be there... so I don't know why this is happening. any help would be appreciated. thanks,

Change the signature to
 byte[] CalcFractel(int width, int height, float complexX, float complexY);

and use something like
C# Image to Byte Array and Byte Array to Image Converter Class[^]

to convert your image to and from a byte array - WCF handles serialization of byte[] nicely.

Espen Harlinn
Sure, this is was a missing piece, my 5.
To answer one of the follow-up questions:

To base64:[^], see samples under each overloaded method.

From base64:[^].

This should be clear enough.


I made a mistake in my comment. Bitmap isn't abstract so you don't get serializtion Issues. I got confused with Image class as I was going from memory,

What you need to do in the event handler is

private void getBitmap_Click(object sender, RoutedEventArgs e)
            ServiceReference1.IService1 client = new ServiceReference1.Service1Client();
            Bitmap bm = client.GetBitmap();

            MemoryStream ms = new MemoryStream();
            bm.Save(ms, ImageFormat.Png);
            ms.Position = 0;
            BitmapImage bi = new BitmapImage();
            bi.StreamSource = ms;
            DisplayImage.Source = bi; 

This gets the image and then puts it into an Image in WPF

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900