|
1 |
| -using System.Drawing; |
2 |
| -using System.Drawing.Imaging; |
| 1 | +using SixLabors.ImageSharp; |
| 2 | +using SixLabors.ImageSharp.PixelFormats; |
3 | 3 |
|
4 | 4 | namespace Devolutions.IronRdp.ConnectExample
|
5 | 5 | {
|
@@ -81,33 +81,27 @@ private static void saveImage(DecodedImage decodedImage, string v)
|
81 | 81 | var bytes = new byte[data.GetSize()];
|
82 | 82 | data.Fill(bytes);
|
83 | 83 |
|
84 |
| - for (int i = 0; i < bytes.Length; i += 4) |
85 |
| - { |
86 |
| - byte temp = bytes[i]; // Store the original Blue value |
87 |
| - bytes[i] = bytes[i + 2]; // Move Red to Blue's position |
88 |
| - bytes[i + 2] = temp; // Move original Blue to Red's position |
89 |
| - // Green (bytes[i+1]) and Alpha (bytes[i+3]) remain unchanged |
90 |
| - } |
| 84 | + using Image<Rgba32> image = new Image<Rgba32>(width, height); |
| 85 | + |
| 86 | + // We’ll mutate this struct instead of creating a new one for performance reasons. |
| 87 | + Rgba32 color = new Rgba32(0, 0, 0); |
91 | 88 |
|
92 |
| -#if WINDOWS // Bitmap is only available on Windows |
93 |
| - using (var bmp = new Bitmap(width, height)) |
| 89 | + for (int col = 0; col < width; ++col) |
94 | 90 | {
|
95 |
| - // Lock the bits of the bitmap. |
96 |
| - var bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), |
97 |
| - ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); |
98 |
| - |
99 |
| - // Get the address of the first line. |
100 |
| - IntPtr ptr = bmpData.Scan0; |
101 |
| - // Copy the RGBA values back to the bitmap |
102 |
| - System.Runtime.InteropServices.Marshal.Copy(bytes, 0, ptr, bytes.Length); |
103 |
| - // Unlock the bits. |
104 |
| - bmp.UnlockBits(bmpData); |
105 |
| - |
106 |
| - // Save the bitmap to the specified output path |
107 |
| - bmp.Save("./output.bmp", ImageFormat.Bmp); |
| 91 | + for (int row = 0; row < height; ++row) |
| 92 | + { |
| 93 | + // Image buffer is in BGRA format. |
| 94 | + var idx = (row * width + col) * 4; |
| 95 | + color.R = bytes[idx]; |
| 96 | + color.G = bytes[idx + 1]; |
| 97 | + color.B = bytes[idx + 2]; |
| 98 | + |
| 99 | + image[col, row] = color; |
| 100 | + } |
108 | 101 | }
|
109 |
| -#endif |
110 | 102 |
|
| 103 | + // Save the image as bitmap. |
| 104 | + image.Save("./output.bmp"); |
111 | 105 | }
|
112 | 106 |
|
113 | 107 | static Dictionary<string, string>? ParseArguments(string[] args)
|
|
0 commit comments