如何转换流到图像?

时间:2019-06-03 09:47:15

标签: vb.net wcf

我有一个WCF服务,我在Stream中通过参数发送img,并且我想将在文件中发送的图像保存在服务器中,在这种情况下为localhost。

你能帮我吗?

Public Function UploadStream(ByVal stream As Stream) As Byte() Implements IService1.UploadStream

    Dim buffer As Byte() = New Byte(9999) {}
    Dim bytesRead As Integer, totalBytesRead As Integer = 0

    Do
        bytesRead = stream.Read(buffer, 0, buffer.Length)
        totalBytesRead += bytesRead
    Loop While bytesRead > 0

    Dim imagememorystream As MemoryStream = New MemoryStream(buffer)
    Dim imgFromStream As Image = Image.FromStream(imagememorystream)

    Return buffer

End Function

如果我删除以下代码:Dim imagememorystream As MemoryStream = New MemoryStream(buffer) Dim imgFromStream As Image = Image.FromStream(imagememorystream), 该函数返回字节数组,当我添加他时,WCF服务不起作用。

  

佩罗(Erro do Pedido)
  维修人员或维修人员的错误处理。一个令人惊叹的“ Oparâmetronãoéválido”。咨询服务提供者详细信息。 O rastreio da pilha deexceçõesé:
  em System.Drawing.Image.FromStream(流流,布尔值useEmbeddedColorManagement,布尔值validateImageData)   em WcfService2.Service1.UploadStream(Stream stream)
  em C:\ Users \ d-sil \ source \ repos \ WcfService2 \ WcfService2 \ Service1.svc.vb:第40行
  em SyncInvokeUploadStream(Object,Object [],Object [])   em System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(对象实例,对象[]输入,对象[]&输出)
  em System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&rpc)
  em System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&rpc)
  em System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc&rpc)
  em System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

1 个答案:

答案 0 :(得分:0)

我对vb不太了解,但是我在c#中做了同样的事情。首先,您需要返回Stream而不是字节数组,并且只需返回如下图像:

var Image = new MemoryStream(image) { Position = 0 };
WebOperationContext.Current.OutgoingResponse.ContentType = "image/*";
return Image;