Browse Source
Merge pull request #1519 from bmlpg/patch-1
Account for image resolution units other than pixels per inch
pull/1547/head
Tony Qu
3 months ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with
25 additions and
3 deletions
-
main/SS/Util/ImageUtils.cs
|
|
@ -22,7 +22,8 @@ namespace NPOI.SS.Util |
|
|
|
using NPOI.SS.UserModel; |
|
|
|
using NPOI.Util; |
|
|
|
using SixLabors.ImageSharp; |
|
|
|
|
|
|
|
using SixLabors.ImageSharp.Metadata; |
|
|
|
|
|
|
|
/** |
|
|
|
* @author Yegor Kozlov |
|
|
|
*/ |
|
|
@ -103,7 +104,28 @@ namespace NPOI.SS.Util |
|
|
|
*/ |
|
|
|
public static int[] GetResolution(Image r) |
|
|
|
{ |
|
|
|
return new int[] { (int)r.Metadata.HorizontalResolution, (int)r.Metadata.VerticalResolution }; |
|
|
|
ImageMetadata imageMetadata = r.Metadata; |
|
|
|
|
|
|
|
double horizontalResolution = 0; |
|
|
|
double verticalResolution = 0; |
|
|
|
|
|
|
|
if (imageMetadata.ResolutionUnits == PixelResolutionUnit.PixelsPerMeter) |
|
|
|
{ |
|
|
|
horizontalResolution = imageMetadata.HorizontalResolution * 0.0254D; |
|
|
|
verticalResolution = imageMetadata.VerticalResolution * 0.0254D; |
|
|
|
} |
|
|
|
else if (imageMetadata.ResolutionUnits == PixelResolutionUnit.PixelsPerCentimeter) |
|
|
|
{ |
|
|
|
horizontalResolution = imageMetadata.HorizontalResolution * 2.54D; |
|
|
|
verticalResolution = imageMetadata.VerticalResolution * 2.54D; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
horizontalResolution = imageMetadata.HorizontalResolution; |
|
|
|
verticalResolution = imageMetadata.VerticalResolution; |
|
|
|
} |
|
|
|
|
|
|
|
return new int[] { (int)Math.Round(horizontalResolution), (int)Math.Round(verticalResolution) }; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@ -296,4 +318,4 @@ namespace NPOI.SS.Util |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |