Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fileformat-heic-gists/c3d9285a0d122d4af08997ab116a0250 to your computer and use it in GitHub Desktop.
Save fileformat-heic-gists/c3d9285a0d122d4af08997ab116a0250 to your computer and use it in GitHub Desktop.
Convert HEIC to PNG programatically using System.Drawing.Common.Bitmap
// Open the HEIC file named "filename.heic" in read mode using FileStream.
using (var fs = new FileStream("filename.heic", FileMode.Open))
{
// Load the HEIC image from the file stream into a HeicImage object.
HeicImage image = HeicImage.Load(fs);
// Extract the pixel data from the HEIC image in the ARGB32 format, storing it in an int array.
var pixels = image.GetInt32Array(Heic.Decoder.PixelFormat.Argb32);
// Retrieve the width and height of the HEIC image.
var width = (int)image.Width;
var height = (int)image.Height;
var i = 0;
// Create a Bitmap object with the specified width, and height.
Bitmap myBitmap = new Bitmap(width, height);
// Set pixel data for each pixel of the bitmap.
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
myBitmap.SetPixel(x, y, Color.FromArgb(pixels[i++]));
// Save the encoded image to the file stream.
myBitmap.Save("output.png");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment