Skip to content

Upgraded to SharpDX 4.2.0. #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions DesktopDuplication.Demo/FormDemo.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion DesktopDuplication.Demo/FormDemo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

Expand All @@ -14,6 +15,7 @@ namespace DesktopDuplication.Demo
public partial class FormDemo : Form
{
private DesktopDuplicator desktopDuplicator;
private CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();

public FormDemo()
{
Expand All @@ -31,7 +33,7 @@ public FormDemo()

private void FormDemo_Shown(object sender, EventArgs e)
{
while (true)
while (_cancellationTokenSource.IsCancellationRequested == false)
{
Application.DoEvents();

Expand Down Expand Up @@ -67,5 +69,10 @@ private void FormDemo_Shown(object sender, EventArgs e)
}
}
}

private void FormDemo_FormClosing(object sender, FormClosingEventArgs e)
{
_cancellationTokenSource.Cancel();
}
}
}
29 changes: 12 additions & 17 deletions DesktopDuplication/DesktopDuplication.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
<AssemblyName>DesktopDuplication</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<NuGetPackageImportStamp>512a20ac</NuGetPackageImportStamp>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -34,17 +35,17 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Reference Include="SharpDX, Version=2.6.3.0, Culture=neutral, PublicKeyToken=b4dcf0f35e5521f1, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>$(SharpDXPackageBinDir)\SharpDX.dll</HintPath>
<Reference Include="SharpDX, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b4dcf0f35e5521f1, processorArchitecture=MSIL">
<HintPath>..\packages\SharpDX.4.2.0\lib\net45\SharpDX.dll</HintPath>
</Reference>
<Reference Include="SharpDX.Direct3D11, Version=2.6.3.0, Culture=neutral, PublicKeyToken=b4dcf0f35e5521f1, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>$(SharpDXPackageBinDir)\SharpDX.Direct3D11.dll</HintPath>
<Reference Include="SharpDX.Direct3D11, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b4dcf0f35e5521f1, processorArchitecture=MSIL">
<HintPath>..\packages\SharpDX.Direct3D11.4.2.0\lib\net45\SharpDX.Direct3D11.dll</HintPath>
</Reference>
<Reference Include="SharpDX.DXGI, Version=2.6.3.0, Culture=neutral, PublicKeyToken=b4dcf0f35e5521f1, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>$(SharpDXPackageBinDir)\SharpDX.DXGI.dll</HintPath>
<Reference Include="SharpDX.DXGI, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b4dcf0f35e5521f1, processorArchitecture=MSIL">
<HintPath>..\packages\SharpDX.DXGI.4.2.0\lib\net45\SharpDX.DXGI.dll</HintPath>
</Reference>
<Reference Include="SharpDX.Mathematics, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b4dcf0f35e5521f1, processorArchitecture=MSIL">
<HintPath>..\packages\SharpDX.Mathematics.4.2.0\lib\net45\SharpDX.Mathematics.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand All @@ -63,18 +64,12 @@
<Compile Include="MovedRegion.cs" />
<Compile Include="PointerInfo.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SharpDXRectangleExtensions.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\SharpDX.2.6.3\build\SharpDX.targets" Condition="Exists('..\packages\SharpDX.2.6.3\build\SharpDX.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\SharpDX.2.6.3\build\SharpDX.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\SharpDX.2.6.3\build\SharpDX.targets'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
21 changes: 12 additions & 9 deletions DesktopDuplication/DesktopDuplicator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using SharpDX.DXGI;
using Device = SharpDX.Direct3D11.Device;
using MapFlags = SharpDX.Direct3D11.MapFlags;
using Rectangle = SharpDX.Rectangle;
using Rectangle = SharpDX.Mathematics.Interop.RawRectangle;
using System.Drawing;
using System.Diagnostics;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -93,8 +93,8 @@ public DesktopDuplicator(int whichGraphicsCardAdapter, int whichOutputDevice)
CpuAccessFlags = CpuAccessFlags.Read,
BindFlags = BindFlags.None,
Format = Format.B8G8R8A8_UNorm,
Width = this.mOutputDesc.DesktopBounds.Width,
Height = this.mOutputDesc.DesktopBounds.Height,
Width = this.mOutputDesc.DesktopBounds.Width(),
Height = this.mOutputDesc.DesktopBounds.Height(),
OptionFlags = ResourceOptionFlags.None,
MipLevels = 1,
ArraySize = 1,
Expand Down Expand Up @@ -187,7 +187,7 @@ private void RetrieveFrameMetadata(DesktopFrame frame)
frame.MovedRegions[i] = new MovedRegion()
{
Source = new System.Drawing.Point(movedRectangles[i].SourcePoint.X, movedRectangles[i].SourcePoint.Y),
Destination = new System.Drawing.Rectangle(movedRectangles[i].DestinationRect.X, movedRectangles[i].DestinationRect.Y, movedRectangles[i].DestinationRect.Width, movedRectangles[i].DestinationRect.Height)
Destination = new System.Drawing.Rectangle(movedRectangles[i].DestinationRect.Left, movedRectangles[i].DestinationRect.Top, movedRectangles[i].DestinationRect.Width(), movedRectangles[i].DestinationRect.Height())
};
}

Expand All @@ -198,7 +198,7 @@ private void RetrieveFrameMetadata(DesktopFrame frame)
frame.UpdatedRegions = new System.Drawing.Rectangle[dirtyRegionsLength / Marshal.SizeOf(typeof(Rectangle))];
for (int i = 0; i < frame.UpdatedRegions.Length; i++)
{
frame.UpdatedRegions[i] = new System.Drawing.Rectangle(dirtyRectangles[i].X, dirtyRectangles[i].Y, dirtyRectangles[i].Width, dirtyRectangles[i].Height);
frame.UpdatedRegions[i] = new System.Drawing.Rectangle(dirtyRectangles[i].Left, dirtyRectangles[i].Top, dirtyRectangles[i].Width(), dirtyRectangles[i].Height());
}
}
else
Expand Down Expand Up @@ -275,16 +275,19 @@ private void ProcessFrame(DesktopFrame frame)
// Get the desktop capture texture
var mapSource = mDevice.ImmediateContext.MapSubresource(desktopImageTexture, 0, MapMode.Read, MapFlags.None);

FinalImage = new System.Drawing.Bitmap(mOutputDesc.DesktopBounds.Width, mOutputDesc.DesktopBounds.Height, PixelFormat.Format32bppRgb);
var boundsRect = new System.Drawing.Rectangle(0, 0, mOutputDesc.DesktopBounds.Width, mOutputDesc.DesktopBounds.Height);
var width = mOutputDesc.DesktopBounds.Width();
var height = mOutputDesc.DesktopBounds.Height();

FinalImage = new System.Drawing.Bitmap(width, height, PixelFormat.Format32bppRgb);
var boundsRect = new System.Drawing.Rectangle(0, 0, width, height);
// Copy pixels from screen capture Texture to GDI bitmap
var mapDest = FinalImage.LockBits(boundsRect, ImageLockMode.WriteOnly, FinalImage.PixelFormat);
var sourcePtr = mapSource.DataPointer;
var destPtr = mapDest.Scan0;
for (int y = 0; y < mOutputDesc.DesktopBounds.Height; y++)
for (int y = 0; y < height; y++)
{
// Copy a single line
Utilities.CopyMemory(destPtr, sourcePtr, mOutputDesc.DesktopBounds.Width * 4);
Utilities.CopyMemory(destPtr, sourcePtr, width * 4);

// Advance pointers
sourcePtr = IntPtr.Add(sourcePtr, mapSource.RowPitch);
Expand Down
18 changes: 18 additions & 0 deletions DesktopDuplication/SharpDXRectangleExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

using Rectangle = SharpDX.Mathematics.Interop.RawRectangle;

namespace DesktopDuplication
{
public static class SharpDXRectangleExtensions
{
public static int Width(this Rectangle rectangle)
{
return rectangle.Right - rectangle.Left;
}

public static int Height(this Rectangle rectangle)
{
return rectangle.Bottom - rectangle.Top;
}
}
}
7 changes: 4 additions & 3 deletions DesktopDuplication/packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="SharpDX" version="2.6.3" targetFramework="net45" />
<package id="SharpDX.Direct3D11" version="2.6.3" targetFramework="net45" />
<package id="SharpDX.DXGI" version="2.6.3" targetFramework="net45" />
<package id="SharpDX" version="4.2.0" targetFramework="net45" />
<package id="SharpDX.Direct3D11" version="4.2.0" targetFramework="net45" />
<package id="SharpDX.DXGI" version="4.2.0" targetFramework="net45" />
<package id="SharpDX.Mathematics" version="4.2.0" targetFramework="net45" />
</packages>