22using System ;
33using System . Net ;
44using SkiaSharp ;
5+ using SixLabors . ImageSharp ;
56using Magicodes . IE . EPPlus ;
67
78namespace Magicodes . IE . Excel . Images
@@ -15,22 +16,23 @@ public struct ImageInfo
1516 public string ContentType ;
1617 }
1718
18- /// <summary>
19- /// 通过 SKCodec 读取图片宽高和格式,不解码像素
20- /// </summary>
2119 public static ImageInfo IdentifyImage ( byte [ ] imageBytes )
2220 {
23- using var ms = new MemoryStream ( imageBytes ) ;
24- using var codec = SKCodec . Create ( ms ) ;
25- if ( codec == null )
26- throw new InvalidOperationException ( "无法识别图片格式" ) ;
27-
28- return new ImageInfo
21+ using ( var ms = new MemoryStream ( imageBytes ) )
2922 {
30- Width = codec . Info . Width ,
31- Height = codec . Info . Height ,
32- ContentType = GetContentType ( codec . EncodedFormat )
33- } ;
23+ using ( var codec = SKCodec . Create ( ms ) )
24+ {
25+ if ( codec == null )
26+ throw new InvalidOperationException ( "无法识别图片格式" ) ;
27+
28+ return new ImageInfo
29+ {
30+ Width = codec . Info . Width ,
31+ Height = codec . Info . Height ,
32+ ContentType = GetContentType ( codec . EncodedFormat )
33+ } ;
34+ }
35+ }
3436 }
3537
3638 public static byte [ ] DownloadImageBytes ( this string url )
@@ -68,7 +70,7 @@ private static string GetContentType(SKEncodedImageFormat format)
6870 }
6971 }
7072
71- #region 模板导出和图片导入仍在使用,后续统一迁移到 SKCodec
73+ #region 模板导出和图片导入仍在使用
7274
7375 public static SixLabors . ImageSharp . Image GetImageByUrl ( this string url , out SixLabors . ImageSharp . Formats . IImageFormat format )
7476 {
@@ -113,23 +115,15 @@ public static SixLabors.ImageSharp.Image Base64StringToImage(this string base64S
113115
114116 public static string SaveTo ( this SixLabors . ImageSharp . Image image , string path )
115117 {
116- using ( var ms = new MemoryStream ( ) )
117- {
118- var pngFormat = SixLabors . ImageSharp . Formats . Png . PngFormat . Instance ;
119- var encoder = image . Configuration . ImageFormatsManager . GetEncoder ( pngFormat ) ;
120- image . Save ( ms , encoder ) ;
121- ms . Position = 0 ;
122- File . WriteAllBytes ( path , ms . ToArray ( ) ) ;
123- }
118+ image . Save ( path ) ;
124119 return path ;
125120 }
126121
127122 public static string ToBase64String ( this SixLabors . ImageSharp . Image image , SixLabors . ImageSharp . Formats . IImageFormat format )
128123 {
129124 using ( var ms = new MemoryStream ( ) )
130125 {
131- var encoder = image . Configuration . ImageFormatsManager . GetEncoder ( format ) ;
132- image . Save ( ms , encoder ) ;
126+ image . Save ( ms , format ) ;
133127 ms . Position = 0 ;
134128 return Convert . ToBase64String ( ms . ToArray ( ) ) ;
135129 }
0 commit comments