Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting .HEIC to JPEG using imagick in C#

I'm having trouble in converting heic file to jpeg

I have already tried searching it online, i can find how to write to a folder but not how to get a byte[] of a converted file so that i can save it

       byte[] file = null;
        file = Convert.FromBase64String(dto.File);

        //Convert HEIC/HEIF to JPF
        if (extension == "HEIC" || extension == "HEIF")
        {
          try
          {
           using (MagickImageCollection images = new MagickImageCollection())
            {
              images.Read(file);
              using (IMagickImage vertical = images.AppendVertically())
              {
                var imgname = filename + ".jpeg";
                vertical.Format = MagickFormat.Jpeg;
                vertical.Density = new Density(300);
                vertical.Write(imgname);
                extension = "jpeg";
            }
            }
          }
          catch (Exception ex)
          {
            Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
          }
        }
            documentId = Service.AddSupportingDocument(file, extension , userName);

I'm not able to get the output file, it's just a string

like image 347
Comrade404 Avatar asked Jul 08 '19 16:07

Comrade404


People also ask

Can ImageMagick convert HEIC to JPG?

Convert with ImageMagick If you have the free command-line tool ImageMagick installed on your computer, you can convert an HEIC file to any other format using the convert tool.

How do I convert HEIC to JPG?

Since Apple accepts HEIC as standard, using a Mac computer greatly simplifies the process. Individual photos can be opened in the Preview app and saved as a JPEG file. Once an HEIC photo is opened, simply select File>Export, select JPEG in the Format option, and hit Save.

Does FFmpeg support HEIC?

FFmpeg does not currently support HEIF decoding (as of 2021-01-11).


2 Answers

According to the documentation, and just like @SLaks suggested, you need to do it via a MemoryStream. Check this example straight from the docs:

// Read first frame of gif image
using (MagickImage image = new MagickImage("Snakeware.gif"))
{
    // Save frame as jpg
    image.Write("Snakeware.jpg");
}

// Write to stream
MagickReadSettings settings = new MagickReadSettings();
// Tells the xc: reader the image to create should be 800x600
settings.Width = 800;
settings.Height = 600;

using (MemoryStream memStream = new MemoryStream())
{
    // Create image that is completely purple and 800x600
    using (MagickImage image = new MagickImage("xc:purple", settings))
    {
        // Sets the output format to png
        image.Format = MagickFormat.Png;
        // Write the image to the memorystream
        image.Write(memStream);
    }
}

// Read image from file
using (MagickImage image = new MagickImage("Snakeware.png"))
{
    // Sets the output format to jpeg
    image.Format = MagickFormat.Jpeg;
    // Create byte array that contains a jpeg file
    byte[] data = image.ToByteArray();
}
like image 163
Javier García Manzano Avatar answered Sep 19 '22 05:09

Javier García Manzano


Other solution.

works with dotnet 6 & Nuget Package Magick.NET-Q16-AnyCPU

    string[] allfiles = Directory.GetFiles(@"__PATH__", "*.heic", SearchOption.AllDirectories);

foreach (var file in allfiles)
{
    FileInfo info = new FileInfo(file);
    using (MagickImage image = new MagickImage(info.FullName))
    {
        // Save frame as jpg
        image.Write(@$"__OUTPATH__{info.Name}.jpg");
    }


}
like image 21
Sios Avatar answered Sep 19 '22 05:09

Sios