Click here to Skip to main content
15,881,787 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.
[OperationContract]
       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;
            }
            else            
                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.
        }

thanks,
:-O :confused:
Posted
Comments
Sergey Alexandrovich Kryukov 8-Mar-11 5:02am    
Does GetFractel mean fractal? Is this just a problem of bitmap serialization of different types of bitmaps, or what?
--SA
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.
Sergey Alexandrovich Kryukov 8-Mar-11 16:01pm    
I did not say "fractel", I said "fractal" See the difference?.. :-)
--SA
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.

thanks,

-SR.
Sergey Alexandrovich Kryukov 8-Mar-11 20:58pm    
The samples are provided by Microsoft in help to the base64 methods of Convert, see my references.
--SA

Change the signature to
[OperationContract]
 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.

Regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-Mar-11 23:55pm    
Sure, this is was a missing piece, my 5.
--SA
To answer one of the follow-up questions:

To base64:
http://msdn.microsoft.com/en-us/library/system.convert.tobase64string.aspx[^], see samples under each overloaded method.

From base64:
http://msdn.microsoft.com/en-us/library/system.convert.frombase64string.aspx[^].

This should be clear enough.

—SA
 
Share this answer
 
Hi,

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.BeginInit();
            bi.StreamSource = ms;
            bi.EndInit();
            
            DisplayImage.Source = bi; 
            
        }


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

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