Skip to content

Commit c0031d3

Browse files
committed
fix(dotnet): use ImageSharp to also support Linux
1 parent 26421c7 commit c0031d3

File tree

3 files changed

+31
-30
lines changed

3 files changed

+31
-30
lines changed

ffi/dotnet/Devolutions.IronRdp.ConnectExample/Devolutions.IronRdp.ConnectExample.csproj

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@
99

1010
<ItemGroup>
1111
<ProjectReference Include="../Devolutions.IronRdp/Devolutions.IronRdp.csproj" />
12-
</ItemGroup>
1312

14-
<ItemGroup>
15-
<PackageReference Include="System.Drawing.Common" Version="8.0.3" />
13+
<!--
14+
System.Drawing.Common NuGet package is only supported on Windows operating systems.
15+
One of the recommended alternatives include ImageSharp.
16+
17+
https://learn.microsoft.com/en-us/dotnet/api/system.drawing?view=net-8.0
18+
-->
19+
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.3" />
1620
</ItemGroup>
1721

1822
</Project>

ffi/dotnet/Devolutions.IronRdp.ConnectExample/Program.cs

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using System.Drawing;
2-
using System.Drawing.Imaging;
1+
using SixLabors.ImageSharp;
2+
using SixLabors.ImageSharp.PixelFormats;
33

44
namespace Devolutions.IronRdp.ConnectExample
55
{
@@ -81,33 +81,27 @@ private static void saveImage(DecodedImage decodedImage, string v)
8181
var bytes = new byte[data.GetSize()];
8282
data.Fill(bytes);
8383

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);
9185

92-
#if WINDOWS // Bitmap is only available on Windows
93-
using (var bmp = new Bitmap(width, height))
86+
// We’ll mutate this struct instead of creating a new one for performance reasons.
87+
Rgba32 color = new Rgba32(0, 0, 0);
88+
89+
for (int col = 0; col < width; ++col)
9490
{
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+
var idx = (row * width + col) * 4;
94+
95+
color.R = bytes[idx];
96+
color.G = bytes[idx + 1];
97+
color.B = bytes[idx + 2];
98+
99+
image[col, row] = color;
100+
}
108101
}
109-
#endif
110102

103+
// Save the image as bitmap.
104+
image.Save("./output.bmp");
111105
}
112106

113107
static Dictionary<string, string>? ParseArguments(string[] args)

ffi/dotnet/Devolutions.IronRdp/src/Connection.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,12 @@ static async Task<NetworkStream> CreateTcpConnection(String servername, int port
199199
{
200200
IPAddress ipAddress;
201201

202-
try {
202+
try
203+
{
203204
ipAddress = IPAddress.Parse(servername);
204-
} catch (FormatException) {
205+
}
206+
catch (FormatException)
207+
{
205208
IPHostEntry ipHostInfo = await Dns.GetHostEntryAsync(servername);
206209
ipAddress = ipHostInfo.AddressList[0];
207210
}

0 commit comments

Comments
 (0)