C# How to Open HEIC Image
See the question and my original answer on StackOverflowOn Windows 10 and higher, there's a bridge between GDI and WIC (Windows Imaging Component) and WIC can open .HEIC file, or any file, as long the component that understands this file format is installed.
Unfortunately, this requires GDI+ version 3, while .NET is hardcoded for GDI+ version 1.
So to open .HEIC files with System.Drawing, you need to
- install a WIC codec that understand the .HEIC format. Microsoft provides HEIF/HEVC Video Extensions, depending on your setup, you may have to install it from the Microsoft Store.
- use a code like this (
GdiplusStartup
needs to be called just once):
...
GdiplusStartup(out _, StartupInputEx.GetDefault(), IntPtr.Zero); // call only once
using (var img = new Bitmap("somefile.heic")) // System.Drawing
{
Console.WriteLine(img.Size);
}
...
[DllImport("gdiplus")]
private static extern int GdiplusStartup(out IntPtr token, in StartupInputEx input, IntPtr output);
private struct StartupInputEx
{
public int GdiplusVersion;
public IntPtr DebugEventCallback;
public bool SuppressBackgroundThread;
public bool SuppressExternalCodecs;
public int StartupParameters;
public static StartupInputEx GetDefault() => new StartupInputEx { GdiplusVersion = 3 };
}