如何从WCF REST读取二进制数据/图像

时间:2010-07-06 20:39:24

标签: wcf wcf-rest

我有以下内容:

公共接口INorthwindSvc

<OperationContract()>
<WebGet(UriTemplate:="EmployeePictureBytes?id={EmpId}")>
Function GetEmployeePictureBytesById(ByVal EmpId As String) As Byte()

结束界面

我按如下方式实现了(使用EF 4.0)方法:

Public Function GetEmployeePictureBytesById(ByVal EmpId As String) As Byte() Implements INorthwindSvc.GetEmployeePictureBytesById
    Dim ctxt As New NorthwindEntities
    Dim q = From c In ctxt.Employees
            Where c.EmployeeID = EmpId
            Select c.Photo

    Return q.FirstOrDefault
End Function

当我从浏览器访问操作时,我能够接收字节。如果我尝试使用Win Client访问相同的内容(如内联所示发生错误):

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim o As New WebClient
    Dim b() As Byte = o.DownloadData(New Uri("http://localhost:8732/WcfWebHttpSvcLib/rest/EmployeePictureBytes?id=2"))

    Dim ms As New MemoryStream()
    ms.Write(b, 0, b.Length)

    Dim original As New System.Drawing.Bitmap(ms) 'error: parameter is not valid

End Sub

我也尝试使用Image.FromStream。但是,仍然没有运气。

有人可以帮我吗?

感谢

1 个答案:

答案 0 :(得分:0)

写入后你没有倒回内存流,所以尝试读取它会失败那就是说,即使这是不必要的,你可以写一下:

im ms As New MemoryStream(b)
' now call FromStream