C# 将图像作为字节数组发送到REST服务
我正在开发一个REST服务,该服务将用于处理图像,例如,更改亮度或对比度。我目前有一个Windows窗体应用程序,在将图像发送给其他人之前,我会将图像上载到该应用程序 以下是我到目前为止在其余部分上接收字节的代码: IRestServiceImpl.csC# 将图像作为字节数组发送到REST服务,c#,image,rest,image-processing,byte,C#,Image,Rest,Image Processing,Byte,我正在开发一个REST服务,该服务将用于处理图像,例如,更改亮度或对比度。我目前有一个Windows窗体应用程序,在将图像发送给其他人之前,我会将图像上载到该应用程序 以下是我到目前为止在其余部分上接收字节的代码: IRestServiceImpl.cs [OperationContract] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBody
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "json/{id}")]
string jsonData(byte[] id);
public string jsonData(byte[] id) {
return "The byte array is" + id;
}
RestServiceImpl.svc.cs
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "json/{id}")]
string jsonData(byte[] id);
public string jsonData(byte[] id) {
return "The byte array is" + id;
}
这是我试图从表单发送它的尝试,但它在VisualStudio的控制台中返回空白
private async void btnRestSend_Click(object sender, EventArgs e) {
byte[] byteArray = ImageManipulation.ImgToByte(pictureBox1.Image);
ByteArrayContent byteContent = new ByteArrayContent(byteArray);
var url = "http://localhost:52278/RestServiceImpl.svc/json/";
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.PostAsync(new Uri(url), byteContent);
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
ImgToByte
public static byte[] ImgToByte(Image img) {
//Converts an Image to a Byte
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(img, typeof(byte[]));
}
如何将字节数组成功发送到rest服务,以便在其中操作它
提前感谢:)您可以将字节数组转换为base 64字符串,以便将其作为字符串或JSON对象内部发送到前端
string base64Image = Convert.ToBase64String(byteArray);
*Convert属于系统库您可以将字节数组转换为base 64字符串,以便将其作为字符串或JSON对象内部发送到前端
string base64Image = Convert.ToBase64String(byteArray);
*转换属于系统库