Converting .HEIC to JPEG using imagick in C#

后端 未结 2 1704
长发绾君心
长发绾君心 2021-01-14 09:41

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 co

2条回答
  •  无人及你
    2021-01-14 10:38

    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();
    }
    

提交回复
热议问题