繁体   English   中英

从.Net客户端将图像发送到SOAP 1.0 Web服务

[英]Send image to a SOAP 1.0 webservice from a .Net client

我需要将图像发送到用PHP实现的SOAP Web服务。

该服务的WSDL看起来像这样...

<xsd:complexType name="Product">
  <xsd:all>
    <xsd:element name="ProductId" type="xsd:int"/>   
    <xsd:element name="Image01" type="xsd:base64Array"/>
  </xsd:all>
</xsd:complexType>

当我在C#应用程序中引用此服务时,用于Image01的数据类型为String

如何从磁盘获取图像并以正确的方式发送编码以通过这种复杂类型发送图像?

将不胜感激示例代码。

您可以使用此代码加载图像,转换为Byte []并转换为Base64

Image myImage = Image.FromFile("myimage.bmp");
MemoryStream stream = new MemoryStream();
myImage.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] imageByte = stream.ToArray();
string imageBase64 = Convert.ToBase64String(imageByte);
stream.Dispose();
myImage.Dispose();

将图像加载为byte[]类型,然后通过Convert.ToBase64String()运行它

有代码的一个很好的示例在这个问题上 ,从磁盘上的文件加载到一个byte []

public byte[] StreamToByteArray(string fileName)
{
byte[] total_stream = new byte[0];
using (Stream input = File.Open(fileName, FileMode.Open, FileAccess.Read))
{
    byte[] stream_array = new byte[0];
    // Setup whatever read size you want (small here for testing)
    byte[] buffer = new byte[32];// * 1024];
    int read = 0;

    while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
    {
        stream_array = new byte[total_stream.Length + read];
        total_stream.CopyTo(stream_array, 0);
        Array.Copy(buffer, 0, stream_array, total_stream.Length, read);
        total_stream = stream_array;
    }
}
return total_stream;
}

所以你会做

Convert.ToBase64String(this.StreamToByteArray("Filename"));

并通过Web服务调用将其传递回来。 我避免使用Image.FromFile调用,因此您可以将该示例与其他非图像调用一起使用,以通过Web服务发送二进制信息。 但是,如果您只希望使用Image,则可以将此代码块替换为Image.FromFile()命令。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM