【问题标题】:Sending an Image as a Byte Array to a REST Service将图像作为字节数组发送到 REST 服务
【发布时间】:2017-06-22 11:34:49
【问题描述】:

我正在开发一个 REST 服务,该服务将用于处理图像,例如更改亮度或对比度。我目前有一个 Windows 窗体应用程序,我将图像上传到该应用程序,然后再将其发送到其他应用程序。

这是我目前在 Rest 上接收字节的代码:

IRestServiceImpl.cs

[OperationContract]
[WebInvoke(Method = "POST",
    ResponseFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.Wrapped,
    UriTemplate = "json/{id}")]
string jsonData(byte[] id);

RestServiceImpl.svc.cs

public string jsonData(byte[] id) {
    return "The byte array is" + id;
}

这是我尝试从表单发送它,但它只是在 Visual Studio 的控制台中返回空白。

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[]));
}

如何让字节数组成功发送到其他服务,以便我可以在那里操作它?

提前致谢:)

【问题讨论】:

标签: c# image rest image-processing byte


【解决方案1】:

您可以将字节数组转换为 base 64 字符串,以便将其作为字符串或 JSON 对象发送到前端。

string base64Image = Convert.ToBase64String(byteArray);    

*转换属于系统库

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-31
    • 2012-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-31
    相关资源
    最近更新 更多