我正在尝试使用依赖服务将 HEIC 图像从下面的代码转换为 Jpg,并尝试使用图像显示并上传到 Web API。但两者都不起作用,这是将 HEIC 转换为 Jpg 的正确方法吗?如果不是,请建议我如何实现这一目标。
依赖服务方法:
public byte[] GetJpgFromHEIC(string path)
{
byte[] imageAsBytes = File.ReadAllBytes(path);
UIImage images = new UIImage(NSData.FromArray(imageAsBytes));
byte[] bytes = images.AsJPEG().ToArray();
// Stream imgStream = new MemoryStream(bytes);
return bytes;
}
在显示图像后面的 Xaml 代码中:
Image image = new Image(){};
byte[] bytes = DependencyService.Get<ICommonHelper>
().GetJpgFromHEIC(fileData.FileName);
image.Source = ImageSource.FromStream(() => new MemoryStream(bytes));
将代码上传到 Web API:在 StreamContent 和 ByteArrayContent 中都作为 HttpContent 进行了尝试,如下所示
HttpResponseMessage response = null;
string filename = data.FileName; // here data object is a FileData, picked from using FilePicker.
byte[] fileBytes = DependencyService.Get<ICommonHelper>().GetJpgFromHEIC(data.FilePath);
HttpContent fileContent = new ByteArrayContent(fileBytes);
// Stream fileStream = new MemoryStream(fileBytes);
// HttpContent fileContent = new StreamContent(fileStream);
fileContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data") { Name =
"file", FileName = data.FileName };
fileContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
using (var formData = new MultipartFormDataContent())
{
formData.Add(fileContent);
response = await client.PostAsync(uri, formData);
if (response.IsSuccessStatusCode)
{
Debug.WriteLine(@" Upload CommentsAttachments SUCCESS>> " + response.Content.ToString());
}
}
请建议我我做错了什么以及如何实现这种转换和上传的可能方法。
谢谢,