如何WCF异步调用如何返回位图类型的值的问题. [英] How to WCF Asynchronous calls problem with how to return value of Bitmap type.
问题描述
这是我的第一个WCF,
我已经阅读了过去一天的内容,在很大程度上,我对此有很好的理解(我认为),但似乎我只是缺少了一些东西.请允许我解释.
我目前有一个项目可以绘制分形并且是多线程的&并行化此项目的核心是函数"CalcFractel()",该项目的当前版本正在运行和调试.现在,对于该项目的下一个版本,我需要将处理部分卸载到WCF所在的Web服务上.通过阅读这两本优秀的教程,我已经了解到以下IServices.cs是您需要的接口在services.svc.cs中实施.因此,这就是我使用VS 2010 Ultimate添加到自动生成的IServices.cs中的内容.而且我已经选中了服务引用"列表下的生成异步操作"复选框.
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);
在实现方面(即Services.cs),我执行了以下操作.
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;
}
在客户端,我添加了以下代码...因此在事件处理程序中
"client_GetDataCompleted(对象发送者,ServiceReference1.GetDataCompletedEventArgs e)"我不知道如何获取位图.细绳.这是我最后需要我自己解决的问题,如果您能提供帮助,将不胜感激.
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:
thanks,
:-O :confused:
推荐答案
将签名更改为
Change the signature to
[OperationContract]
byte[] CalcFractel(int width, int height, float complexX, float complexY);
并使用类似
C#图像到字节数组和字节数组到图像转换器类 [
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
回答以下后续问题之一:
到base64:
http://msdn.microsoft.com/en-us/library/system.convert. tobase64string.aspx [ ^ ],请参见每种重载方法下的示例.
来自base64:
http://msdn.microsoft.com/en-us/library/system.convert. frombase64string.aspx [ ^ ].
这应该足够清楚.
—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
我的评论有误.位图不是抽象的,因此不会出现序列化问题.当我从内存中消失时,我对Image类感到困惑,
您需要在事件处理程序中做的是
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;
}
这将获取图像,然后将其放入WPF中的图像
This gets the image and then puts it into an Image in WPF
这篇关于如何WCF异步调用如何返回位图类型的值的问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!