将图像加载到byte[]
类型中,然后通过Convert.ToBase64String()
这个问题有一个很好的代码示例,可以将文件从磁盘加载到字节[]
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"));
And pass that back via the web service call. 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. But if you wish to only ever use an Image then substitute this block of code for an Image.FromFile()
command.