Reputation: 4024
I have an image upload form in ASP.NET that supports JPG/PNG/GIF and I thought it would be enough. Until Apple introduced their new HEIC image format. How do I handle that in C#?
Searching for C# and HEIC shows nothing in Google, so it seems this issue hasn't been addressed yet.
Does the .NET Framework support HEIC out-of-the-box? Probably not since it's so new. Is there any 3rd party library that supports it? I want to convert HEIC to JPG for storage.
Thanks
Upvotes: 18
Views: 14744
Reputation: 138960
On 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
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 };
}
Upvotes: 0
Reputation: 93
There are tons of answers out there on the internet about reading heic files, converting them to different other file formats:
Converting .HEIC to JPEG using imagick in C#.
and
https://medium.com/@cloudmersive/how-to-convert-heic-to-png-using-c-in-net-framework-d73ae7d4609e 's solution to. convert heic to png using
Cloudmersive.APIClient.NET.DocumentAndDataConvert
NuGet package.
//Program.cs
using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Api;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Client;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Model;
namespace Example
{
public class ConvertImageImageFormatConvertExample
{
public static void Main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
//
Configuration.Default.AddApiKeyPrefix("Apikey", "Bearer");
var apiInstance = new ConvertImageApi();
var format1 = format1_example; // string | Input file format as a 3+ letter file extension. You can also provide UNKNOWN for unknown file formats. Supported formats include AAI, ART, ARW, AVS, BPG, BMP, BMP2, BMP3, BRF, CALS, CGM, CIN, CMYK, CMYKA, CR2, CRW, CUR, CUT, DCM, DCR, DCX, DDS, DIB, DJVU, DNG, DOT, DPX, EMF, EPDF, EPI, EPS, EPS2, EPS3, EPSF, EPSI, EPT, EXR, FAX, FIG, FITS, FPX, GIF, GPLT, GRAY, HDR, HEIC, HPGL, HRZ, ICO, ISOBRL, ISBRL6, JBIG, JNG, JP2, JPT, J2C, J2K, JPEG/JPG, JXR, MAT, MONO, MNG, M2V, MRW, MTV, NEF, ORF, OTB, P7, PALM, PAM, PBM, PCD, PCDS, PCL, PCX, PDF, PEF, PES, PFA, PFB, PFM, PGM, PICON, PICT, PIX, PNG, PNG8, PNG00, PNG24, PNG32, PNG48, PNG64, PNM, PPM, PSB, PSD, PTIF, PWB, RAD, RAF, RGB, RGBA, RGF, RLA, RLE, SCT, SFW, SGI, SID, SUN, SVG, TGA, TIFF, TIM, UIL, VIFF, VICAR, VBMP, WDP, WEBP, WPG, X, XBM, XCF, XPM, XWD, X3F, YCbCr, YCbCrA, YUV
var format2 = format2_example; // string | Output (convert to this format) file format as a 3+ letter file extension. Supported formats include AAI, ART, ARW, AVS, BPG, BMP, BMP2, BMP3, BRF, CALS, CGM, CIN, CMYK, CMYKA, CR2, CRW, CUR, CUT, DCM, DCR, DCX, DDS, DIB, DJVU, DNG, DOT, DPX, EMF, EPDF, EPI, EPS, EPS2, EPS3, EPSF, EPSI, EPT, EXR, FAX, FIG, FITS, FPX, GIF, GPLT, GRAY, HDR, HEIC, HPGL, HRZ, ICO, ISOBRL, ISBRL6, JBIG, JNG, JP2, JPT, J2C, J2K, JPEG/JPG, JXR, MAT, MONO, MNG, M2V, MRW, MTV, NEF, ORF, OTB, P7, PALM, PAM, PBM, PCD, PCDS, PCL, PCX, PDF, PEF, PES, PFA, PFB, PFM, PGM, PICON, PICT, PIX, PNG, PNG8, PNG00, PNG24, PNG32, PNG48, PNG64, PNM, PPM, PSB, PSD, PTIF, PWB, RAD, RAF, RGB, RGBA, RGF, RLA, RLE, SCT, SFW, SGI, SID, SUN, SVG, TGA, TIFF, TIM, UIL, VIFF, VICAR, VBMP, WDP, WEBP, WPG, X, XBM, XCF, XPM, XWD, X3F, YCbCr, YCbCrA, YUV
var inputFile = new System.IO.Stream(); // System.IO.Stream | Input file to perform the operation on.
try
{
// Image format conversion
byte[] result = apiInstance.ConvertImageImageFormatConvert(format1, format2, inputFile);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling ConvertImageApi.ConvertImageImageFormatConvert: " + e.Message );
}
}
}
}
But as you are in asp.net I suppose you need to open the image in an html webpage, not to edit it.
The question is not really about csharp..
https://www.quora.com/Does-Google-Chrome-support-heif-images.
As in the first answer is said, as Google Chrome supports h264, so it should support heic..
But it would be a very hard solution, which would require too much work to do, so I either recommend just converting heic on the server or writing a custom script that will allow you to read the heic file and view it in pixels..
Upvotes: 3