views:

495

answers:

4

Hi!

I trying to display image in picture box. The application have two part. First part is windows application, and second part is web service (asmx).

This is the code for windows application:

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

And this is the code for web service:

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/")&gt; _
<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

The problem is that function in web service does not return System.IO.MemoryStream data type so I getting error message can not convert: Error 1 Value of type 'WindowsApplication1.localhost.MemoryStream' cannot be converted to 'System.IO.Stream'.

How I can resolve this?

Many thanks!

+2  A: 

Try to return byte array instead of Stream.

Anuraj
+1, ... "by using `.ToArray()` method".
Rubens Farias
A: 

The MemoryStream resides on the server and can't be serialized to the client.

You should return a bytearray from your webservice then transform this to a image.

rdkleine
A: 

The error message says that your method returns the type WindowsApplication1.localhost.MemoryStream and not a System.IO.MemoryStream.

I suspect that your web service is set up to return a MemoryStream object. Try to change it to return a byte[] array instead. On your receiving side retrieve the byte array and create a System.IO.MemoryStream from that array.

Rune Grimstad
Ok. I changed web service to return byte: <WebMethod()> _ Public Function PrikazSlike() As Byte 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 Convert.ToByte(stream1) End FunctionEnd ClassBut now how i can convert bytes to System.IO.MemoryStream in windows application.
Comii
Use the contstructor for MemoryStream that takes a byte array. Dim stream1 As New System.IO.MemoryStream(data)
Rune Grimstad
Now I am getting in windows application error:Error 1 Value of type 'Byte' cannot be converted to 'System.IO.Stream'.
Comii
Can you update your example code? It probably is the return value of one of your methods that is wrong. You return a Byte instead of a MemoryStream.
Rune Grimstad
web service code: <WebMethod()> _ Public Function ShowPicture() 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 FunctionEnd Classwindows applicartion code:Public Sub PrikazSlike()Dim p As localhost.Service1 = New localhost.Service1()PictureBox1.Image = Image.FromStream(p.PictureShow()) End Sub
Comii
Your ShowPicture method returns a sigle byte instead of a byte array. Change it to Public Function ShowPicture() As Byte().
Rune Grimstad
I change this:<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 FunctionEnd ClassNow I getting error:Error 1 Value of type 'Byte' cannot be converted to '1-dimensional array of Byte'
Comii
Did you update your web reference? Also, it would be easier to read your code if you update it in your question. Somewhere you return a byte instead of a byte array and that causes your compile error.
Rune Grimstad
A: 

This is the update code for web service:

   <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

This is the windows application code:

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

Now I getting error:

Error 1 Value of type 'Byte' cannot be converted to 'System.IO.Stream'.

Comii