Error converting webp to jpg

using (var client = myHttpClient.CreateClient())
{
var destinationPathWebP = Path.Combine(LOCAL_DIRECTORY, i.SKU, IMAGE_WEB_EXTENSION);
destinationPathWebP = destinationPathWebP.Replace("\", “/”);
var destinationPathJpg = Path.Combine(LOCAL_DIRECTORY, i.SKU + IMAGE_EXTENSION); destinationPathJpg = destinationPathJpg.Replace("\", “/”);

var responseImage = await client.GetAsync(image.URL);
if (responseImage.Content.Headers.ContentType.MediaType.Equals(“image/webp”))
{
var byteArray = await responseImage.Content.ReadAsByteArrayAsync();
await File.WriteAllBytesAsync(destinationPathWebP, byteArray);
using (Converter converter = new Converter(destinationPathWebP))
{
ImageConvertOptions options = new ImageConvertOptions
{ // Set the conversion format to JPG
Format = ImageFileType.Jpg
};
converter.Convert(destinationPathJpg, options);
}

I am currently trying to convert a webp image to a jpg.
I am downloading the webp image and then creating it into a directory in my project.
When trying to convert it into jpg I get the following error: “Cannot convert. The file is corrupt or damaged.”.

Note: It works fine when I run it locally on my machine but in Production environment it gives this error. Besides that the images are valid.
Any help?

@goncalo.rodrigues

Please share following details and we’ll look into this scenario:

  • API version that you are using (e.g. 22.8, 22.9))
  • Source/problematic file
  • Production environment details
  • Sample/console application using that issue could be reproduced

Hello @Atir_Tahir

  • I’m using the latest version 22.9.0

  • Any .webp url should be fine (In the sample there is one if you want to use it)

  • Its a unix based system running a docker image in a AWS container

  • ConsoleApp4.zip (106.1 KB)

This issue is under investigation. Your investigation ticket ID is CONVERSIONNET-5558.

@goncalo.rodrigues

Please try the following code:

var httpClient = new HttpClientFactory();
using (var client = httpClient.CreateClient())
{

    var responseImage = await client.GetAsync(url);
    if (responseImage.Content.Headers.ContentType.MediaType.Equals("image/webp"))
    {
        await using (var imageStream = await responseImage.Content.ReadAsStreamAsync())
        {
            using (Converter converter = new Converter(() => imageStream))
            {
                ImageConvertOptions options = new ImageConvertOptions
                {
                    // Set the conversion format to JPG
                    Format = ImageFileType.Jpg
                };
                converter.Convert(() => new MemoryStream(), (convertedStream, converterName) =>
                {
                    // copy converted in file stream or store it

                    using (var fs = new FileStream("converted.jpg", FileMode.Create))
                    {
                        convertedStream.CopyTo(fs);
                    }
                }, options);
            }
        }
    }
}

The example above store the converted result in a memory stream and after that in ConvertedDocument delegate store this memory stream in file stream. Of course you can store directly the converted result like this:

var httpClient = new HttpClientFactory();
using (var client = httpClient.CreateClient())
{

    var responseImage = await client.GetAsync(url);
    if (responseImage.Content.Headers.ContentType.MediaType.Equals("image/webp"))
    {
        await using (var imageStream = await responseImage.Content.ReadAsStreamAsync())
        {
            using (Converter converter = new Converter(() => imageStream))
            {
                ImageConvertOptions options = new ImageConvertOptions
                {
                    // Set the conversion format to JPG
                    Format = ImageFileType.Jpg
                };
                converter.Convert(() => new FileStream("converted.jpg", FileMode.Create), options);
            }
        }
    }
}

Let us know if issue persists.

The issues you have found earlier (filed as CONVERSIONNET-5558) have been fixed in this update. This message was posted using Bugs notification tool by nikola.yankov