Created
July 29, 2024 05:08
-
-
Save fileformat-heic-gists/c3d9285a0d122d4af08997ab116a0250 to your computer and use it in GitHub Desktop.
Convert HEIC to PNG programatically using System.Drawing.Common.Bitmap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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