如何从Base64图片字符串提取EXIF数据以判断图片方向?
Solution: Extract EXIF Orientation from Base64 Image in C#
First, you’ll need to convert your Base64 image string into a stream, then parse the embedded EXIF metadata to pull the orientation value. Below are two practical approaches tailored to different project needs.
Approach 1: Using System.Drawing (Windows/System.Drawing.Common NuGet)
This works natively for Windows apps, or cross-platform if you install the System.Drawing.Common NuGet package (note: it’s deprecated for non-Windows in .NET 6+ but still functional for legacy projects).
Integrated Code for Your Loop:
using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; // ... your existing code ... busyMessage.Text = "Loading Tools"; InvToolSync toolSync = new InvToolSync(); toolData = await toolSync.GetTools(viewModel.CompanyData.company_id); foreach (Tool tool in toolData) { if (tool.archived == "True") continue; // Assume your Tool object has a property like ImageBase64 storing the string if (string.IsNullOrEmpty(tool.ImageBase64)) continue; try { // Convert Base64 to MemoryStream byte[] imageBytes = Convert.FromBase64String(tool.ImageBase64); using (MemoryStream ms = new MemoryStream(imageBytes)) { using (Image image = Image.FromStream(ms)) { // Look for the EXIF Orientation tag (0x0112 is the standard ID) PropertyItem orientationProp = image.PropertyItems.FirstOrDefault(p => p.Id == 0x0112); if (orientationProp != null) { // Orientation value is stored as a 2-byte short int orientationValue = BitConverter.ToInt16(orientationProp.Value, 0); // Map value to readable orientation string orientation = orientationValue switch { 1 => "Normal (0°)", 2 => "Flipped horizontally", 3 => "Rotated 180°", 4 => "Flipped vertically", 5 => "Flipped horizontal + Rotated 90° CCW", 6 => "Rotated 90° CCW", 7 => "Flipped horizontal + Rotated 270° CCW", 8 => "Rotated 270° CCW", _ => "Unknown" }; // Use the orientation value as needed (e.g., adjust display) Console.WriteLine($"Tool {tool.Id} Image Orientation: {orientation}"); } else { Console.WriteLine($"Tool {tool.Id} has no EXIF orientation data."); } } } } catch (Exception ex) { // Handle invalid Base64 or corrupted image Console.WriteLine($"Error processing tool {tool.Id} image: {ex.Message}"); } }
Approach 2: Using ImageSharp (Cross-Platform, Modern)
For .NET Core/.NET 5+ cross-platform apps, ImageSharp is the recommended maintained alternative. Install the SixLabors.ImageSharp NuGet package first.
Integrated Code for Your Loop:
using SixLabors.ImageSharp; using SixLabors.ImageSharp.Metadata; using System.IO; // ... your existing code ... busyMessage.Text = "Loading Tools"; InvToolSync toolSync = new InvToolSync(); toolData = await toolSync.GetTools(viewModel.CompanyData.company_id); foreach (Tool tool in toolData) { if (tool.archived == "True") continue; if (string.IsNullOrEmpty(tool.ImageBase64)) continue; try { byte[] imageBytes = Convert.FromBase64String(tool.ImageBase64); using (MemoryStream ms = new MemoryStream(imageBytes)) { // Load image with metadata using (Image image = Image.Load(ms)) { ExifOrientation orientation = image.Metadata.Exif.Orientation; if (orientation != ExifOrientation.Unknown) { Console.WriteLine($"Tool {tool.Id} Image Orientation: {orientation}"); // Optional: Auto-correct the image orientation if needed // image.Mutate(x => x.AutoOrient()); } else { Console.WriteLine($"Tool {tool.Id} has no EXIF orientation data."); } } } } catch (Exception ex) { Console.WriteLine($"Error processing tool {tool.Id} image: {ex.Message}"); } }
Key Notes:
- Orientation Values: The EXIF orientation tag uses standard values that define how the image should be rotated/flipped for correct display. Both approaches map these values to human-readable terms.
- Error Handling: The try-catch blocks handle common issues like invalid Base64 strings or corrupted image data.
- Auto-Orientation: If you need to adjust the image to its correct orientation, both libraries offer built-in methods (like
AutoOrient()in ImageSharp) to simplify this step.
内容的提问来源于stack exchange,提问作者Hayden Passmore
相关产品推荐
相关产品推荐

