2017-06-22 43 views
0

我正在使用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[])); 
} 

我如何將字節數組被成功發送到其他服務,所以我可以在那裏操縱呢?

感謝提前:)

+0

[AWAIT](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/index) –

回答

0

可以將字節數組轉換成底座64的字符串,以便將其發送到前端爲一個字符串或JSON對象的內部。

string base64Image = Convert.ToBase64String(byteArray);  

*轉換屬於系統庫

相關問題