在WCF服务中传递图像并在传递后将其显示在WPF数据网格中的最佳方式是什么?通过WCF传递图像,并将它们显示在WPF数据网格中
回答
我并不是说这是唯一或最佳的解决方案,但我们有它的工作是这样的:
你需要做的是:
创建一个WCF方法将返回图像通过一些身份证或其他。它应该返回字节数组(byte []):
public byte[] GetImage(int id)
{
// put your logic of retrieving image on the server side here
}
在您的数据类(在网格中显示的对象)使属性的图像,其吸气剂应调用WCF方法和字节数组转换成的BitmapImage:
public BitmapImage Image
{
get
{
// here - connection is your wcf connection interface
// this.ImageId is id of the image. This parameter can be basically anything
byte[] imageData = connection.GetImage(this.ImageId);
// Load the bitmap from the received byte[] array
using (System.IO.MemoryStream stream = new System.IO.MemoryStream(imageData, 0, imageData.Length, false, true))
{
BitmapImage bmp = new BitmapImage();
bmp.BeginInit();
bmp.StreamSource = stream;
try
{
bmp.EndInit();
bmp.Freeze(); // helps for performance
return bmp;
}
catch (Exception ex)
{
// Handle exceptions here
}
return null; // return nothing (or some default image) if request fails
}
}
}
在你的模板(或地方)把一个Image控件和它的来源属性绑定到上面创建的图片属性:
<DataTemplate> <!-- Can be a ControlTemplate as well, depends on where and how you use it -->
<Image
Source={Binding Image, IsAsync=true}
/>
</DataTemplate>
不使UI自由的最简单方法当检索图像时将像我一样将IsAsync属性设置为false。但是还有很多需要改进的地方。例如。您可以在加载图像时显示一些加载动画。
使用PriorityBinding可以完成加载别的东西时显示的东西(您可以在这里阅读:http://msdn.microsoft.com/en-us/library/ms753174.aspx)。
你可以从流中加载WPF图像吗?如果是这样,那么你可以编写WCF服务来返回System.IO.Stream类型。
我不知道这就是为什么我问 – 2009-04-24 07:10:21
只要流被标记为唯一的一部分,你就可以发送一个流作为消息的一部分消息的正文,消息的其他所有字段都必须转到标题 – SaguiItay 2009-05-12 19:49:10
- 1. 通过意图传递多个字符串并显示它们
- 2. 将图像放在数组中并显示它们
- 3. 如何在视图中显示数据通过传递jsonResult?
- 4. WPF:数据将不会显示在我的数据网格中
- 5. Android:从网络下载图像并在gridview上显示它们
- 6. ASP.NET MVC:从数据库中加载图像并在视图中显示它们
- 7. 通过意图传递数据并接收它
- 8. 传递数据并在多个堆栈上显示数据网格
- 9. 用于上传图像并编辑/显示它们的PHP库?
- 10. 网格视图按钮传递数据通过点击
- 11. 在c#中上传图像并在wpf中显示消息框
- 12. 将图像ID传递给一个函数并显示/隐藏();
- 13. 如何在WPF中的数据网格标题中显示图像
- 14. 如何使用WCF服务在Silverlight数据网格中显示GIF图像?
- 15. wpf:如何在网格视图中显示数据集?
- 16. 如何将图像从sqlite显示到flex数据网格中
- 17. 通过ajax传递HTML并返回以显示它
- 18. 在网格视图中显示数据
- 19. 提取参数,并将它们传递到数据库
- 20. 在WPF中通过C#显示位图图像
- 21. 通过Javascript将图像数据传递给PHP
- 22. 通过HTML上传/加载图像,并在JS Canvas上显示
- 23. 从SQL Server中检索图像并使用C#在WPF网格中显示
- 24. 在平铺视图显示图像,并重新排列它们
- 25. 显示图像并将动态数据传递给火狐页面
- 26. WPF - 数据网格中的网格线(也通过标题)
- 27. 如何将位图从ASP.NET传递到JavaScript并显示它?
- 28. 在silverlight数据网格中显示图像
- 29. 试图通过图像循环并将它们添加到数组
- 30. 在wxPython中显示图像的网格
您正在处理的图像的平均大小是多少?您需要在一次通话中处理多少人? 对于您的问题有几个很好的解决方案,但这取决于您在每次通话时必须处理的信息量。将它作为一个字节数组返回只是一个很好的解决方案,如果你的图像相对较小,并且你不必一次返回大量的数据(我问你是因为把它放在一个数据网格中,所以我 – 2009-05-10 14:22:20