-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New example project to draw full screen image on stream deck
- Loading branch information
Christian Wischenbart
committed
Jan 29, 2018
1 parent
a8cbd04
commit 847da44
Showing
5 changed files
with
223 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Drawing; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace StreamDeckSharp.Examples.DrawFullScreen | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
var testImg = @"C:\testimage.png"; | ||
|
||
using (var deck = StreamDeck.FromHID()) | ||
using (var bmp = (Bitmap)Bitmap.FromFile(testImg)) | ||
{ | ||
deck.DrawFullScreenBitmap(bmp); | ||
Console.ReadKey(); | ||
} | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/StreamDeckSharp.Examples.DrawFullScreen/Properties/AssemblyInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
// Allgemeine Informationen über eine Assembly werden über die folgenden | ||
// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern, | ||
// die einer Assembly zugeordnet sind. | ||
[assembly: AssemblyTitle("StreamDeckSharp.Examples.DrawFullScreen")] | ||
[assembly: AssemblyDescription("")] | ||
[assembly: AssemblyConfiguration("")] | ||
[assembly: AssemblyCompany("")] | ||
[assembly: AssemblyProduct("StreamDeckSharp.Examples.DrawFullScreen")] | ||
[assembly: AssemblyCopyright("Copyright © 2018")] | ||
[assembly: AssemblyTrademark("")] | ||
[assembly: AssemblyCulture("")] | ||
|
||
// Durch Festlegen von ComVisible auf FALSE werden die Typen in dieser Assembly | ||
// für COM-Komponenten unsichtbar. Wenn Sie auf einen Typ in dieser Assembly von | ||
// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen. | ||
[assembly: ComVisible(false)] | ||
|
||
// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird | ||
[assembly: Guid("1053a6d8-353e-4848-9434-5a35f32e9180")] | ||
|
||
// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten: | ||
// | ||
// Hauptversion | ||
// Nebenversion | ||
// Buildnummer | ||
// Revision | ||
// | ||
// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden, | ||
// übernehmen, indem Sie "*" eingeben: | ||
// [assembly: AssemblyVersion("1.0.*")] | ||
[assembly: AssemblyVersion("1.0.0.0")] | ||
[assembly: AssemblyFileVersion("1.0.0.0")] |
103 changes: 103 additions & 0 deletions
103
src/StreamDeckSharp.Examples.DrawFullScreen/StreamDeckFullScreenDrawingExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Drawing; | ||
using System.Drawing.Drawing2D; | ||
using System.Drawing.Imaging; | ||
using System.Linq; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
using System.Threading; | ||
|
||
namespace StreamDeckSharp.Examples.DrawFullScreen | ||
{ | ||
public static class StreamDeckFullScreenDrawingExtension | ||
{ | ||
private const int buttonPxSize = 72; | ||
private const int buttonPxDist = 33; //measured | ||
private const int fullPxWidth = 5 * buttonPxSize + 4 * buttonPxDist; | ||
private const int fullPxHeight = 3 * buttonPxSize + 2 * buttonPxDist; | ||
private static readonly Brush black = Brushes.Black; | ||
|
||
public static void DrawFullScreenBitmap(this IStreamDeck deck, Bitmap b) | ||
{ | ||
byte[] imgData = null; | ||
using (var resizedImage = ResizeToFullStreamDeckImage(b)) | ||
{ | ||
imgData = GetRgbArray(resizedImage); | ||
} | ||
|
||
for (int i = 0; i < deck.NumberOfKeys; i++) | ||
{ | ||
var img = GetKeyImageFromFull(i, imgData); | ||
deck.SetKeyBitmap(i, img); | ||
} | ||
} | ||
|
||
private static Bitmap ResizeToFullStreamDeckImage(Bitmap b) | ||
{ | ||
var newBm = new Bitmap(fullPxWidth, fullPxHeight, PixelFormat.Format24bppRgb); | ||
double scale = Math.Min((double)fullPxWidth / b.Width, (double)fullPxHeight / b.Height); | ||
|
||
using (var g = Graphics.FromImage(newBm)) | ||
{ | ||
g.InterpolationMode = InterpolationMode.High; | ||
g.CompositingQuality = CompositingQuality.HighQuality; | ||
g.SmoothingMode = SmoothingMode.AntiAlias; | ||
|
||
var scaleWidth = (int)(b.Width * scale); | ||
var scaleHeight = (int)(b.Height * scale); | ||
|
||
g.FillRectangle(black, new RectangleF(0, 0, fullPxWidth, fullPxHeight)); | ||
g.DrawImage(b, new Rectangle(((int)fullPxWidth - scaleWidth) / 2, ((int)fullPxHeight - scaleHeight) / 2, scaleWidth, scaleHeight)); | ||
} | ||
|
||
return newBm; | ||
} | ||
|
||
static byte[] GetRgbArray(Bitmap b) | ||
{ | ||
var rect = new Rectangle(0, 0, b.Width, b.Height); | ||
var lockData = b.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); | ||
|
||
try | ||
{ | ||
var data = new byte[fullPxWidth * fullPxHeight * 3]; | ||
Marshal.Copy(lockData.Scan0, data, 0, data.Length); | ||
return data; | ||
} | ||
finally | ||
{ | ||
b.UnlockBits(lockData); | ||
} | ||
} | ||
|
||
static StreamDeckKeyBitmap GetKeyImageFromFull(int keyId, byte[] fullImageData) | ||
{ | ||
var y = keyId / 5; | ||
var x = 4 - keyId % 5; | ||
return GetKeyImageFromFull(x, y, fullImageData); | ||
} | ||
|
||
static StreamDeckKeyBitmap GetKeyImageFromFull(int xPos, int yPos, byte[] fullImageData) | ||
{ | ||
var keyImgData = new byte[buttonPxSize * buttonPxSize * 3]; | ||
var xOffset = xPos * (buttonPxSize + buttonPxDist); | ||
var yOffset = yPos * (buttonPxSize + buttonPxDist); | ||
|
||
for (int y = 0; y < buttonPxSize; y++) | ||
{ | ||
var numberOfPixelsInPrevRows = (y + yOffset) * fullPxWidth + xOffset; | ||
for (int x = 0; x < buttonPxSize; x++) | ||
{ | ||
var p = (numberOfPixelsInPrevRows + x) * 3; | ||
var kPos = (y * buttonPxSize + x) * 3; | ||
keyImgData[kPos + 0] = fullImageData[p + 0]; | ||
keyImgData[kPos + 1] = fullImageData[p + 1]; | ||
keyImgData[kPos + 2] = fullImageData[p + 2]; | ||
} | ||
} | ||
|
||
return StreamDeckKeyBitmap.FromRawBitmap(keyImgData); | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/StreamDeckSharp.Examples.DrawFullScreen/StreamDeckSharp.Examples.DrawFullScreen.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProjectGuid>{1053A6D8-353E-4848-9434-5A35F32E9180}</ProjectGuid> | ||
<OutputType>Exe</OutputType> | ||
<RootNamespace>StreamDeckSharp.Examples.DrawFullScreen</RootNamespace> | ||
<AssemblyName>StreamDeckSharp.Examples.DrawFullScreen</AssemblyName> | ||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion> | ||
<FileAlignment>512</FileAlignment> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<PlatformTarget>AnyCPU</PlatformTarget> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
<Optimize>false</Optimize> | ||
<OutputPath>bin\Debug\</OutputPath> | ||
<DefineConstants>DEBUG;TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<PlatformTarget>AnyCPU</PlatformTarget> | ||
<DebugType>pdbonly</DebugType> | ||
<Optimize>true</Optimize> | ||
<OutputPath>bin\Release\</OutputPath> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Reference Include="System" /> | ||
<Reference Include="System.Core" /> | ||
<Reference Include="System.Drawing" /> | ||
<Reference Include="System.Xml.Linq" /> | ||
<Reference Include="System.Data.DataSetExtensions" /> | ||
<Reference Include="Microsoft.CSharp" /> | ||
<Reference Include="System.Data" /> | ||
<Reference Include="System.Xml" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="Program.cs" /> | ||
<Compile Include="Properties\AssemblyInfo.cs" /> | ||
<Compile Include="StreamDeckFullScreenDrawingExtension.cs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\StreamDeckSharp\StreamDeckSharp.csproj"> | ||
<Project>{d06ab787-766e-4b28-89c4-8d948070eb1c}</Project> | ||
<Name>StreamDeckSharp</Name> | ||
</ProjectReference> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters