Forum Cross Platform with Xamarin

HEIC to JPG conversion in Xamarin IOS

Hi All,

I am trying to convert the HEIC image to Jpg from below code in using Dependency Service and trying to display with Image and uploading to Web API..
But both are not working, Is it a correct way of doing HEIC conversion to Jpg? if not please suggest me how to achieve this.

Dependency Service method:

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;

In Xaml Code Behind Displaying Image:

    Image image = new Image(){
    byte[] bytes = DependencyService.Get<ICommonHelper>().GetJpgFromHEIC(fileData.FileName);
            image.Source = ImageSource.FromStream(() => new MemoryStream(bytes));

Uploading code to web API:

Tried in both StreamContent and ByteArrayContent as HttpContent like below

       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())
                    response = await client.PostAsync(uri, formData);
                    if (response.IsSuccessStatusCode)
                        Debug.WriteLine(@" Upload CommentsAttachments SUCCESS>> " + response.Content.ToString());

Please suggest me What I am doing wrong and how to achieve and possible ways for this conversion and upload.

Thank you,

Sign In or Register to comment.