我试图在图片框中显示图像。该应用程序有两个部分。 第一部分是windows应用程序,第二部分是web服务(asmx)。
这是windows应用程序的代码:
Public Sub PrikazSlike()
Dim p As localhost.Service1 = New localhost.Service1()
PictureBox1.Image = Image.FromStream(p.PictureShow())
End Sub
这是网络服务的代码:
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”。
最佳答案
尝试返回字节数组而不是 Stream。
关于.net - 函数返回System.IO.MemoryStream?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2027217/