Skip to content

Commit

Permalink
Add support for screen selection
Browse files Browse the repository at this point in the history
The screenshot was taken from the primary screen.
Add an option for the client to get a list of screens on the server.
The screenshot will be taken from the selected screen.
  • Loading branch information
ckorn committed Jan 30, 2021
1 parent 587765b commit bfe87d2
Show file tree
Hide file tree
Showing 32 changed files with 582 additions and 71 deletions.
4 changes: 3 additions & 1 deletion CrossCutting/DataClasses/ConnectionSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace CrossCutting.DataClasses
{
public static class ConnectionSettings
{
public const string PipeName = "GimmeAScreenshotPlease.SendScreenshot";
public const string PrimaryScreenPipeName = "GimmeAScreenshotPlease.SendScreenshotPrimaryScreen";
public const string ScreenPipeName = "GimmeAScreenshotPlease.SendScreenshotForScreen";
public const string ScreenListPipeName = "GimmeAScreenshotPlease.SendScreenList";
}
}
11 changes: 11 additions & 0 deletions CrossCutting/DataClasses/ScreenInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,16 @@ public class ScreenInformation
{
public int Index { get; set; }
public string Name { get; set; }

public ScreenInformation()
{

}

public ScreenInformation(int index, string name)
{
Index = index;
Name = name;
}
}
}
14 changes: 14 additions & 0 deletions GimmeAScreenshotPlease.sln
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Encodings.Contract", "Logic
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Encodings", "Logic\Foundation\Encodings\Encodings.csproj", "{A0865541-5E27-4177-95B4-C2DDC9ABDEA7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Serialization", "Logic\Foundation\Serialization\Serialization.csproj", "{6B38A64D-27B0-4F98-BA53-4204B4394C58}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Serialization.Contract", "Logic\Foundation\Serialization.Contract\Serialization.Contract.csproj", "{E428141B-971F-495E-BF23-277433EB8E32}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -117,6 +121,14 @@ Global
{A0865541-5E27-4177-95B4-C2DDC9ABDEA7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A0865541-5E27-4177-95B4-C2DDC9ABDEA7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A0865541-5E27-4177-95B4-C2DDC9ABDEA7}.Release|Any CPU.Build.0 = Release|Any CPU
{6B38A64D-27B0-4F98-BA53-4204B4394C58}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6B38A64D-27B0-4F98-BA53-4204B4394C58}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6B38A64D-27B0-4F98-BA53-4204B4394C58}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6B38A64D-27B0-4F98-BA53-4204B4394C58}.Release|Any CPU.Build.0 = Release|Any CPU
{E428141B-971F-495E-BF23-277433EB8E32}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E428141B-971F-495E-BF23-277433EB8E32}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E428141B-971F-495E-BF23-277433EB8E32}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E428141B-971F-495E-BF23-277433EB8E32}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -141,6 +153,8 @@ Global
{22FEE8C4-3711-45E8-8085-02754E1EB77F} = {184D9DA9-1150-4646-976E-45E5193AD28A}
{4A594EBA-482E-4A2D-8CE0-7AEC059615EA} = {83982FCC-0016-417C-A1C5-39D069B5DAFC}
{A0865541-5E27-4177-95B4-C2DDC9ABDEA7} = {83982FCC-0016-417C-A1C5-39D069B5DAFC}
{6B38A64D-27B0-4F98-BA53-4204B4394C58} = {83982FCC-0016-417C-A1C5-39D069B5DAFC}
{E428141B-971F-495E-BF23-277433EB8E32} = {83982FCC-0016-417C-A1C5-39D069B5DAFC}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {1B43E731-2B89-4F38-80F7-19D0917FFDAE}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using CrossCutting.DataClasses;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
Expand All @@ -9,6 +10,8 @@ namespace Logic.Business.ScreenshotClientWorkflow.Contract
{
public interface IClientWorkflow
{
Bitmap GetScreenshot(string target);
Bitmap GetScreenshotPrimaryScreen(string target);
Bitmap GetScreenshotForScreen(string target, ScreenInformation screenInformation);
IReadOnlyList<ScreenInformation> GetScreenInformationList(string target);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,11 @@
<Compile Include="IClientWorkflow.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\CrossCutting\DataClasses\DataClasses.csproj">
<Project>{22fee8c4-3711-45e8-8085-02754e1eb77f}</Project>
<Name>DataClasses</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
17 changes: 14 additions & 3 deletions Logic/Business/ScreenshotClientWorkflow/ClientWorkflow.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Logic.Business.ScreenshotClientWorkflow.Contract;
using CrossCutting.DataClasses;
using Logic.Business.ScreenshotClientWorkflow.Contract;
using Logic.Foundation.Client.Contract;
using System;
using System.Collections.Generic;
Expand All @@ -18,9 +19,19 @@ public ClientWorkflow(IScreenshotClient client)
this.client = client;
}

public Bitmap GetScreenshot(string target)
public Bitmap GetScreenshotPrimaryScreen(string target)
{
return this.client.GetScreenshot(target, CrossCutting.DataClasses.ConnectionSettings.PipeName);
return this.client.GetScreenshot(target);
}

public Bitmap GetScreenshotForScreen(string target, ScreenInformation screenInformation)
{
return this.client.GetScreenshot(target, screenInformation);
}

public IReadOnlyList<ScreenInformation> GetScreenInformationList(string target)
{
return this.client.GetScreenInformationList(target);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace Logic.Business.ScreenshotServerWorkflow.Contract
public interface IServerWorkflow
{
event EventHandler<Bitmap> ScreenshotSent;
void Start();
void StartSendPrimaryScreen();
void StartSendScreen();
void StartSendScreenList();
}
}
16 changes: 13 additions & 3 deletions Logic/Business/ScreenshotServerWorkflow/ServerWorkflow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,21 @@ private void ScreenshotServer_ScreenshotSent(object sender, Bitmap e)
this.ScreenshotSent?.Invoke(sender, e);
}

public void Start()
public void StartSendPrimaryScreen()
{
this.screenshotServer.Start(CrossCutting.DataClasses.ConnectionSettings.PipeName,
CrossCutting.DataClasses.ScreenshotOptions.MaxWidth,
this.screenshotServer.StartSendPrimaryScreen(CrossCutting.DataClasses.ScreenshotOptions.MaxWidth,
CrossCutting.DataClasses.ScreenshotOptions.MaxHeight);
}

public void StartSendScreen()
{
this.screenshotServer.StartSendScreen(CrossCutting.DataClasses.ScreenshotOptions.MaxWidth,
CrossCutting.DataClasses.ScreenshotOptions.MaxHeight);
}

public void StartSendScreenList()
{
this.screenshotServer.StartSendScreenList();
}
}
}
6 changes: 6 additions & 0 deletions Logic/Domain/Client.Contract/Client.Contract.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,11 @@
<Compile Include="IScreenshotClient.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\CrossCutting\DataClasses\DataClasses.csproj">
<Project>{22fee8c4-3711-45e8-8085-02754e1eb77f}</Project>
<Name>DataClasses</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
7 changes: 5 additions & 2 deletions Logic/Domain/Client.Contract/IScreenshotClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using CrossCutting.DataClasses;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
Expand All @@ -9,6 +10,8 @@ namespace Logic.Foundation.Client.Contract
{
public interface IScreenshotClient
{
Bitmap GetScreenshot(string target, string name);
Bitmap GetScreenshot(string target);
Bitmap GetScreenshot(string target, ScreenInformation screenInformation);
IReadOnlyList<ScreenInformation> GetScreenInformationList(string target);
}
}
8 changes: 8 additions & 0 deletions Logic/Domain/Client/Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
<Compile Include="ScreenshotClient.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\CrossCutting\DataClasses\DataClasses.csproj">
<Project>{22FEE8C4-3711-45E8-8085-02754E1EB77F}</Project>
<Name>DataClasses</Name>
</ProjectReference>
<ProjectReference Include="..\..\Foundation\Encodings.Contract\Encodings.Contract.csproj">
<Project>{4a594eba-482e-4a2d-8ce0-7aec059615ea}</Project>
<Name>Encodings.Contract</Name>
Expand All @@ -54,6 +58,10 @@
<Project>{67d18de8-9eb0-4746-a3ea-ec1bc9e433b7}</Project>
<Name>Io.Contract</Name>
</ProjectReference>
<ProjectReference Include="..\..\Foundation\Serialization.Contract\Serialization.Contract.csproj">
<Project>{e428141b-971f-495e-bf23-277433eb8e32}</Project>
<Name>Serialization.Contract</Name>
</ProjectReference>
<ProjectReference Include="..\Client.Contract\Client.Contract.csproj">
<Project>{f7ec64a7-f348-41f7-ab6d-6efb6f2054c0}</Project>
<Name>Client.Contract</Name>
Expand Down
45 changes: 40 additions & 5 deletions Logic/Domain/Client/ScreenshotClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Logic.Foundation.Client.Contract;
using CrossCutting.DataClasses;
using Logic.Foundation.Client.Contract;
using Logic.Foundation.Encodings.Contract;
using Logic.Foundation.Io.Contract;
using Logic.Foundation.Serialization.Contract;
using System;
using System.Collections.Generic;
using System.Drawing;
Expand All @@ -15,16 +17,49 @@ public class ScreenshotClient : IScreenshotClient
{
private readonly ISender sender;
private readonly IBinaryDecoder binaryDecoder;
public ScreenshotClient(ISender sender, IBinaryDecoder binaryDecoder)
private readonly ISerializer serializer;
private readonly IDeserializer deserializer;

public ScreenshotClient(ISender sender, IBinaryDecoder binaryDecoder,
ISerializer serializer, IDeserializer deserializer)
{
this.sender = sender;
this.binaryDecoder = binaryDecoder;
this.serializer = serializer;
this.deserializer = deserializer;
}

public IReadOnlyList<ScreenInformation> GetScreenInformationList(string target)
{
string result = this.sender.Send(target,
ConnectionSettings.ScreenListPipeName,
"GimmeAScreenshotPlease");
List<ScreenInformation> screenInformationList
= this.deserializer.Deserialize<List<ScreenInformation>>(result);
return screenInformationList;
}

public Bitmap GetScreenshot(string target)
{
string result = this.sender.Send(target,
ConnectionSettings.PrimaryScreenPipeName,
"GimmeAScreenshotPlease");
Bitmap bitmap = GetBitmapFromResponse(result);
return bitmap;
}

public Bitmap GetScreenshot(string target, ScreenInformation screenInformation)
{
string text = this.serializer.Serialize(screenInformation);
string result = this.sender.Send(target,
ConnectionSettings.ScreenPipeName, text);
Bitmap bitmap = GetBitmapFromResponse(result);
return bitmap;
}

public Bitmap GetScreenshot(string target, string name)
private Bitmap GetBitmapFromResponse(string response)
{
string result = this.sender.Send(target, name, "GimmeAScreenshotPlease");
byte[] arr = this.binaryDecoder.DecodePlainText(result);
byte[] arr = this.binaryDecoder.DecodePlainText(response);
MemoryStream ms = new MemoryStream(arr);
Bitmap bitmap = new Bitmap(ms);

Expand Down
4 changes: 3 additions & 1 deletion Logic/Domain/Server.Contract/IScreenshotServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace Logic.Foundation.Server.Contract
public interface IScreenshotServer
{
event EventHandler<Bitmap> ScreenshotSent;
void Start(string name, int maxWidth, int maxHeight);
void StartSendPrimaryScreen(int maxWidth, int maxHeight);
void StartSendScreen(int maxWidth, int maxHeight);
void StartSendScreenList();
}
}
62 changes: 49 additions & 13 deletions Logic/Domain/Server/ScreenshotServer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Logic.Foundation.Encodings.Contract;
using CrossCutting.DataClasses;
using Logic.Foundation.Encodings.Contract;
using Logic.Foundation.Graphics.Contract;
using Logic.Foundation.Io.Contract;
using Logic.Foundation.Serialization.Contract;
using Logic.Foundation.Server.Contract;
using System;
using System.Collections.Generic;
Expand All @@ -10,6 +12,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Logic.Foundation.Server
{
Expand All @@ -19,37 +22,70 @@ public class ScreenshotServer : IScreenshotServer
private readonly IReceiver receiver;
private readonly IResize resize;
private readonly IBinaryEncoder binaryEncoder;
private readonly ISerializer serializer;
private readonly IDeserializer deserializer;

public event EventHandler<Bitmap> ScreenshotSent;

public ScreenshotServer(IScreenshot screenshot, IReceiver receiver, IResize resize,
IBinaryEncoder binaryEncoder)
IBinaryEncoder binaryEncoder, ISerializer serializer, IDeserializer deserializer)
{
this.screenshot = screenshot;
this.receiver = receiver;
this.resize = resize;
this.binaryEncoder = binaryEncoder;
this.serializer = serializer;
this.deserializer = deserializer;
}

public void Start(string name, int maxWidth, int maxHeight)
public void StartSendPrimaryScreen(int maxWidth, int maxHeight)
{
string Send(string text)
{
using (Bitmap bitmap = this.screenshot.GetFullScreen())
string base64 = GetScreenshotAsText(this.screenshot.GetPrimaryScreen(), maxWidth, maxHeight);
return base64;
}
this.receiver.Start(ConnectionSettings.PrimaryScreenPipeName, Send);
}

public void StartSendScreen(int maxWidth, int maxHeight)
{
string Send(string text)
{
ScreenInformation screenInformation = this.deserializer.Deserialize<ScreenInformation>(text);
string base64 = GetScreenshotAsText(this.screenshot.GetScreen(screenInformation.Index), maxWidth, maxHeight);
return base64;
}
this.receiver.Start(ConnectionSettings.ScreenPipeName, Send);
}

public void StartSendScreenList()
{
string Send(string text)
{
IReadOnlyList<Screen> screenList = this.screenshot.GetScreenList();
List<ScreenInformation> screenInformationList = screenList.Select((x, i) => new ScreenInformation(i, x.DeviceName)).ToList();
string returnValue = this.serializer.Serialize(screenInformationList);
return returnValue;
}
this.receiver.Start(ConnectionSettings.ScreenListPipeName, Send);
}

private string GetScreenshotAsText(Bitmap screenshot, int maxWidth, int maxHeight)
{
using (Bitmap bitmap = screenshot)
{
using (MemoryStream ms = new MemoryStream())
{
using (MemoryStream ms = new MemoryStream())
using (Bitmap newImage = this.resize.ResizeImage(bitmap, maxWidth, maxHeight))
{
using (Bitmap newImage = this.resize.ResizeImage(bitmap, maxWidth, maxHeight))
{
ScreenshotSent?.Invoke(this, new Bitmap(newImage));
newImage.Save(ms, ImageFormat.Jpeg);
string base64 = this.binaryEncoder.GetAsPlainText(ms.GetBuffer());
return base64;
}
ScreenshotSent?.Invoke(this, new Bitmap(newImage));
newImage.Save(ms, ImageFormat.Jpeg);
string base64 = this.binaryEncoder.GetAsPlainText(ms.GetBuffer());
return base64;
}
}
}
this.receiver.Start(name, Send);
}
}
}
Loading

0 comments on commit bfe87d2

Please sign in to comment.