[英]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.