|
4 | 4 | using System.Xml.Linq; |
5 | 5 | using System.ComponentModel; |
6 | 6 | using System.Collections.Generic; |
7 | | -using System.Drawing; |
8 | | -using System.Drawing.Imaging; |
| 7 | +using System.Runtime.InteropServices; |
| 8 | +using SixLabors.ImageSharp; |
| 9 | +using SixLabors.ImageSharp.PixelFormats; |
9 | 10 |
|
10 | 11 | static class Helper |
11 | 12 | { |
@@ -42,24 +43,21 @@ public static T Get<T>(this XElement xelem, string attribute, T defaultT = defau |
42 | 43 |
|
43 | 44 | static class BitmapHelper |
44 | 45 | { |
45 | | - public static (int[] bitmap, int width, int height) LoadBitmap(string filename) |
| 46 | + public static (int[], int, int) LoadBitmap(string filename) |
46 | 47 | { |
47 | | - Bitmap bitmap = new(filename); |
48 | | - int width = bitmap.Width, height = bitmap.Height; |
49 | | - var bits = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); |
50 | | - int[] result = new int[bitmap.Width * bitmap.Height]; |
51 | | - System.Runtime.InteropServices.Marshal.Copy(bits.Scan0, result, 0, result.Length); |
52 | | - bitmap.UnlockBits(bits); |
53 | | - bitmap.Dispose(); |
| 48 | + using var image = Image.Load<Bgra32>(filename); |
| 49 | + int width = image.Width, height = image.Height; |
| 50 | + int[] result = new int[width * height]; |
| 51 | + image.CopyPixelDataTo(MemoryMarshal.Cast<int, Bgra32>(result)); |
54 | 52 | return (result, width, height); |
55 | 53 | } |
56 | 54 |
|
57 | | - public static void SaveBitmap(int[] data, int width, int height, string filename) |
| 55 | + unsafe public static void SaveBitmap(int[] data, int width, int height, string filename) |
58 | 56 | { |
59 | | - Bitmap result = new(width, height); |
60 | | - var bits = result.LockBits(new Rectangle(0, 0, result.Width, result.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); |
61 | | - System.Runtime.InteropServices.Marshal.Copy(data, 0, bits.Scan0, data.Length); |
62 | | - result.UnlockBits(bits); |
63 | | - result.Save(filename); |
| 57 | + fixed (int* pData = data) |
| 58 | + { |
| 59 | + using var image = Image.WrapMemory<Bgra32>(pData, width, height); |
| 60 | + image.SaveAsPng(filename); |
| 61 | + } |
64 | 62 | } |
65 | 63 | } |
0 commit comments