函数返回System.IO.MemoryStream?

时间:2010-01-08 11:26:55

标签: .net asp.net vb.net

我试图在图片框中显示图像。该应用程序有两部分。 第一部分是Windows应用程序,第二部分是Web服务(asmx)。

这是Windows应用程序的代码:

Public Sub PrikazSlike()
             Dim p As localhost.Service1 = New localhost.Service1()
        PictureBox1.Image = Image.FromStream(p.PictureShow()) 
End Sub

这是Web服务的代码:

Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.IO

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class Service1
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function PictureShow() As System.IO.MemoryStream
        Dim client As New System.Net.WebClient()
        Dim stream1 As New System.IO.MemoryStream()
        Dim data As Byte() = client.DownloadData("http://www.psp-themes.net/.../assassins%20creed%203.jpg")
        client.Dispose()
        stream1.Write(data, 0, data.Length)
        Return stream1
    End Function
End Class

问题是Web服务中的函数没有返回System.IO.MemoryStream数据类型,所以我得到的错误信息无法转换: 错误1“WindowsApplication1.localhost.MemoryStream”类型的值无法转换为“System.IO.Stream”。

我如何解决这个问题?

非常感谢!

更新(最初发布为“回答”)

这是网络服务的更新代码:

   <WebMethod()> _
    Public Function PrikazSlike() As Byte
        Dim client As New System.Net.WebClient()
        'Dim data As Byte

        Dim data As Byte() = client.DownloadData("http://www.psp-themes.net/.../assassins%20creed%203.jpg")
        Dim stream1 As New System.IO.MemoryStream(data)
        client.Dispose()
        stream1.Write(data, 0, data.Length)
        Return Convert.ToByte(stream1)
    End Function
End Class

这是Windows应用程序代码:

Public Sub Show.Picture()
         Dim p As localhost.Service1 = New localhost.Service1()
         PictureBox1.Image = Image.FromStream(p.PictureShow()) 
         End Sub

现在我收到错误:

错误1“字节”类型的值无法转换为“System.IO.Stream”。

3 个答案:

答案 0 :(得分:6)

尝试返回字节数组而不是Stream。

答案 1 :(得分:1)

错误消息显示您的方法返回类型WindowsApplication1.localhost.MemoryStream而不是System.IO.MemoryStream

我怀疑您的网络服务已设置为返回MemoryStream个对象。尝试将其更改为返回byte[]数组。在接收端检索字节数组并从该数组创建System.IO.MemoryStream

答案 2 :(得分:0)

MemoryStream驻留在服务器上,无法序列化到客户端。

您应该从Web服务返回一个bytearray,然后将其转换为图像。