|
4 | 4 | using SixLabors.ImageSharp; |
5 | 5 | using SixLabors.ImageSharp.Formats; |
6 | 6 | using SixLabors.ImageSharp.Metadata; |
| 7 | +using SkiaSharp; |
| 8 | +using SixLabors.ImageSharp.PixelFormats; |
| 9 | +using SixLabors.ImageSharp.Advanced; |
| 10 | +using SixLabors.ImageSharp.Metadata.Profiles.Exif; |
| 11 | +using Magicodes.IE.EPPlus; |
| 12 | +using SixLabors.ImageSharp.Memory; |
| 13 | +using System.Text.RegularExpressions; |
7 | 14 |
|
8 | 15 | namespace Magicodes.IE.Excel.Images |
9 | 16 | { |
10 | | - internal static class ImageExtensions |
| 17 | + internal static partial class ImageExtensions |
11 | 18 | { |
12 | 19 | public static string SaveTo(this Image image, string path) |
13 | 20 | { |
@@ -39,34 +46,52 @@ public static Image GetImageByUrl(this string url, out IImageFormat format) |
39 | 46 | using (var wc = new WebClient()) |
40 | 47 | { |
41 | 48 | wc.Proxy = null; |
42 | | - var image = Image.Load(wc.OpenRead(url), out format); |
43 | | - if (image.Metadata.HorizontalResolution == 0 && image.Metadata.VerticalResolution == 0) |
| 49 | + using (Stream webStream = wc.OpenRead(url)) |
44 | 50 | { |
45 | | - image.Metadata.HorizontalResolution = ImageMetadata.DefaultHorizontalResolution; |
46 | | - image.Metadata.VerticalResolution = ImageMetadata.DefaultVerticalResolution; |
| 51 | + using (MemoryStream memoryStream = new MemoryStream()) |
| 52 | + { |
| 53 | + webStream.CopyTo(memoryStream); |
| 54 | + memoryStream.Position = 0; |
| 55 | + |
| 56 | + var image = Image.Load(memoryStream); |
| 57 | + format = image.GetImageFormat(memoryStream); |
| 58 | + |
| 59 | + if (image.Metadata.HorizontalResolution == 0 && image.Metadata.VerticalResolution == 0) |
| 60 | + { |
| 61 | + image.Metadata.HorizontalResolution = ImageMetadata.DefaultHorizontalResolution; |
| 62 | + image.Metadata.VerticalResolution = ImageMetadata.DefaultVerticalResolution; |
| 63 | + } |
| 64 | + return image; |
| 65 | + } |
47 | 66 | } |
48 | | - return image; |
49 | 67 | } |
50 | 68 | } |
51 | 69 |
|
52 | 70 | /// <summary> |
53 | | - /// base64转Bitmap |
| 71 | + /// Converts a base64 string to an Image |
54 | 72 | /// </summary> |
55 | | - /// <param name="base64String"></param> |
56 | | - /// <param name="format"></param> |
57 | | - /// <returns></returns> |
| 73 | + /// <param name="base64String">The base64 string representing the image</param> |
| 74 | + /// <param name="format">The image format</param> |
| 75 | + /// <returns>An Image object representing the base64 string</returns> |
58 | 76 | public static Image Base64StringToImage(this string base64String, out IImageFormat format) |
59 | 77 | { |
60 | | - var bytes = Convert.FromBase64String(FixBase64ForImage(base64String)); |
61 | | - return Image.Load(bytes, out format); |
| 78 | + byte[] bytes = Convert.FromBase64String(CleanupBase64String(base64String)); |
| 79 | + using (MemoryStream stream = new MemoryStream(bytes)) |
| 80 | + { |
| 81 | + Image image = Image.Load(stream); |
| 82 | + format = image.GetImageFormat(stream); |
| 83 | + return image; |
| 84 | + } |
62 | 85 | } |
63 | 86 |
|
64 | | - private static string FixBase64ForImage(string image) |
| 87 | + /// <summary> |
| 88 | + /// Cleans up the base64 string by removing unnecessary characters |
| 89 | + /// </summary> |
| 90 | + /// <param name="base64String">The base64 string to clean up</param> |
| 91 | + /// <returns>A cleaned-up base64 string</returns> |
| 92 | + private static string CleanupBase64String(string base64String) |
65 | 93 | { |
66 | | - var sbText = new System.Text.StringBuilder(image, image.Length); |
67 | | - sbText.Replace("\r\n", string.Empty); |
68 | | - sbText.Replace(" ", string.Empty); |
69 | | - return sbText.ToString(); |
| 94 | + return Regex.Replace(base64String, @"\s+", string.Empty); |
70 | 95 | } |
71 | 96 | } |
72 | 97 | } |
0 commit comments