如何将 JPEG 图像转换为字节格式
本文关键字:字节 格式 转换 图像 JPEG | 更新日期: 2023-09-27 18:36:44
我目前正在使用Windows Phone应用程序单击图片,我想使用HTTP post请求将该图像上传到Web服务。我不想使用Windows Phone silverlight。
如何将该图像发送到 Web 服务 URL?
在http
上发布图像就像发布任何其他文件类型一样。 使用以下代码片段
public string PostFileUsingApi()
{
string result = "";
string param1 = "value1";
using (var handler = new HttpClientHandler()) {
using (var client = new HttpClient(handler) { BaseAddress = new Uri("http://localhost:8008") }) {
client.Timeout = new TimeSpan(0, 20, 0);
StorageFile storageFile = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri);
Stream stream = await storageFile.OpenStreamForReadAsync();
var requestContent = new MultipartFormDataContent();
StreamContent fileContent = new StreamContent(stream);
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") {
Name = "imagekey", //content key goes here
FileName = "myimage"
};
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/bmp");
requestContent.Add(fileContent);
client.DefaultRequestHeaders.Add("ClientSecretKey", "ClientSecretValue");
HttpResponseMessage response = await client.PostAsync("api/controller/UploadData?param1=" + HttpUtility.UrlEncode(param1), requestContent).Result;
if (response.StatusCode == System.Net.HttpStatusCode.OK) {
result = await response.Content.ReadAsStringAsync().Result
} else {
result = "";
}
}
}
return result;
}
安装此包以解析依赖项https://www.nuget.org/packages/microsoft.aspnet.webapi.client/