.net - 函数返回System.IO.MemoryStream?

标签 .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

这是网络服务的代码:

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/

相关文章:

c# - 开发一个简单的 Windows 系统托盘桌面应用程序以使用 .NET Web 服务

c# - 如何在 .NET 中一步裁剪和调整图像大小

.net - MS SQL 数据库架构更改而不丢失数据

asp.net - 具有客户端证书身份验证的 .Net Core Web API

c# - 没有模型和 Ajax 的 POST Json

.net - 在 vb.net 中创建并写入文本文件

.net - F# 和 op_GreaterThan

c# - ASP.Net LinkBut​​ton 在本地工作但不能在服务器上工作

vb.net - 处置与无处置之间的区别

vb.net - MS Access 网络共享缓慢