Find image format using Bitmap object in C
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In C#, the System.Drawing.Bitmap class provides the RawFormat property to determine an image's format (JPEG, PNG, GIF, BMP, TIFF, etc.) after loading it from a file or stream. This is useful when processing user-uploaded images where the file extension may be incorrect or missing, or when handling images from streams and byte arrays where no filename exists. The RawFormat property returns an ImageFormat object that you compare against known format constants like ImageFormat.Jpeg and ImageFormat.Png.
Basic Format Detection
RawFormat detects the actual binary format of the image data, regardless of the file extension. A file named photo.png that is actually a JPEG will correctly report as JPEG.
Helper Method for Format Detection
Detecting Format from a Stream
When loading from streams, Bitmap reads the binary header to detect the format. The stream must remain open for the lifetime of the Bitmap object.
Using ImageCodecInfo for MIME Type
ImageCodecInfo.GetImageDecoders() returns all registered image codecs. Matching by FormatID gives you the MIME type (image/jpeg, image/png, etc.), which is useful for HTTP content-type headers.
Cross-Platform Alternative: ImageSharp
ImageSharp is the recommended replacement for System.Drawing in cross-platform .NET applications. It detects format directly during load and provides a clean API.
Reading Magic Bytes (Without Loading the Image)
Magic bytes detection reads only the first few bytes of the file, making it much faster than loading the entire image into a Bitmap object. This is ideal for validating uploads before processing.
Common Pitfalls
- Using
==instead of.Equals()to compare ImageFormat:bitmap.RawFormat == ImageFormat.Jpegcompares references, not values, and may returnfalseeven for JPEG images. Always usebitmap.RawFormat.Equals(ImageFormat.Jpeg)or compare theGuidproperty. - Using
System.Drawingon Linux or macOS:System.Drawing.Bitmapdepends on GDI+ (Windows-only). On .NET Core/.NET 5+ non-Windows platforms, it throwsPlatformNotSupportedException. UseSixLabors.ImageSharporSkiaSharpfor cross-platform applications. - Not disposing the Bitmap:
Bitmapholds unmanaged GDI+ resources. Failing to callDispose()(or use ausingblock) causes memory leaks, especially when processing many images in a loop. - Closing the source stream before the Bitmap: When loading from a
Stream, the stream must remain open for the Bitmap's lifetime. Closing the stream prematurely causesGDI+ generic errorexceptions on subsequent operations. - Trusting file extensions for format detection: File extensions can be wrong or spoofed. A file named
.pngmay contain JPEG data. Always useRawFormator magic bytes to detect the actual format, especially for user-uploaded files in security-sensitive contexts.
Summary
- Use
bitmap.RawFormat.Equals(ImageFormat.Jpeg)to check the image format RawFormatdetects the actual binary format, not the file extension- Use
ImageCodecInfoto get the MIME type from the format GUID - For cross-platform .NET, use
SixLabors.ImageSharpinstead ofSystem.Drawing - Magic bytes detection is fastest when you only need the format without loading the full image
- Always use
usingblocks withBitmapto prevent GDI+ resource leaks
Related reading
- Find the highest occuring words in a string C
- Find the next TCP port in .NET
- Find type of nullable properties via reflection
- Find Types in All Assemblies
- Finding all positions of substring in a larger string in C
- Finding an item in a List using C
- Finding property differences between two C objects
- Firing off multiple Tasks asynchronously and waiting for them to complete

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.