简体   繁体   English

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

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

I need to send an image to a SOAP web service implemented in PHP. 我需要将图像发送到用PHP实现的SOAP Web服务。

The WSDL for the service looks like this... 该服务的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>

When I reference this service in my C# application the data type used for Image01 is String . 当我在C#应用程序中引用此服务时,用于Image01的数据类型为String

How can I get an image from disk and send encode it in the correct way to send it via this complex type? 如何从磁盘获取图像并以正确的方式发送编码以通过这种复杂类型发送图像?

Would appreciate sample code. 将不胜感激示例代码。

You can use this code to load the Image, transform to Byte[] and convert to Base64 您可以使用此代码加载图像,转换为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();

Load the image up into a byte[] type then run it through a Convert.ToBase64String() 将图像加载为byte[]类型,然后通过Convert.ToBase64String()运行它

There's a nice sample of code on this question to load a file from disk into a byte[] 有代码的一个很好的示例在这个问题上 ,从磁盘上的文件加载到一个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;
}

So you'd just do 所以你会做

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

And pass that back via the web service call. 并通过Web服务调用将其传递回来。 I've avoided using the Image.FromFile call so you can re-use this example with other non image calls to send binary information over a webservice. 我避免使用Image.FromFile调用,因此您可以将该示例与其他非图像调用一起使用,以通过Web服务发送二进制信息。 But if you wish to only ever use an Image then substitute this block of code for an Image.FromFile() command. 但是,如果您只希望使用Image,则可以将此代码块替换为Image.FromFile()命令。

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

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