Skip to content

Commit 847da44

Browse files
author
Christian Wischenbart
committed
New example project to draw full screen image on stream deck
1 parent a8cbd04 commit 847da44

5 files changed

Lines changed: 223 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Drawing;
4+
using System.Linq;
5+
using System.Text;
6+
7+
namespace StreamDeckSharp.Examples.DrawFullScreen
8+
{
9+
class Program
10+
{
11+
static void Main(string[] args)
12+
{
13+
var testImg = @"C:\testimage.png";
14+
15+
using (var deck = StreamDeck.FromHID())
16+
using (var bmp = (Bitmap)Bitmap.FromFile(testImg))
17+
{
18+
deck.DrawFullScreenBitmap(bmp);
19+
Console.ReadKey();
20+
}
21+
}
22+
}
23+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// Allgemeine Informationen über eine Assembly werden über die folgenden
6+
// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern,
7+
// die einer Assembly zugeordnet sind.
8+
[assembly: AssemblyTitle("StreamDeckSharp.Examples.DrawFullScreen")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("StreamDeckSharp.Examples.DrawFullScreen")]
13+
[assembly: AssemblyCopyright("Copyright © 2018")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Durch Festlegen von ComVisible auf FALSE werden die Typen in dieser Assembly
18+
// für COM-Komponenten unsichtbar. Wenn Sie auf einen Typ in dieser Assembly von
19+
// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen.
20+
[assembly: ComVisible(false)]
21+
22+
// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird
23+
[assembly: Guid("1053a6d8-353e-4848-9434-5a35f32e9180")]
24+
25+
// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten:
26+
//
27+
// Hauptversion
28+
// Nebenversion
29+
// Buildnummer
30+
// Revision
31+
//
32+
// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden,
33+
// übernehmen, indem Sie "*" eingeben:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Drawing;
4+
using System.Drawing.Drawing2D;
5+
using System.Drawing.Imaging;
6+
using System.Linq;
7+
using System.Runtime.InteropServices;
8+
using System.Text;
9+
using System.Threading;
10+
11+
namespace StreamDeckSharp.Examples.DrawFullScreen
12+
{
13+
public static class StreamDeckFullScreenDrawingExtension
14+
{
15+
private const int buttonPxSize = 72;
16+
private const int buttonPxDist = 33; //measured
17+
private const int fullPxWidth = 5 * buttonPxSize + 4 * buttonPxDist;
18+
private const int fullPxHeight = 3 * buttonPxSize + 2 * buttonPxDist;
19+
private static readonly Brush black = Brushes.Black;
20+
21+
public static void DrawFullScreenBitmap(this IStreamDeck deck, Bitmap b)
22+
{
23+
byte[] imgData = null;
24+
using (var resizedImage = ResizeToFullStreamDeckImage(b))
25+
{
26+
imgData = GetRgbArray(resizedImage);
27+
}
28+
29+
for (int i = 0; i < deck.NumberOfKeys; i++)
30+
{
31+
var img = GetKeyImageFromFull(i, imgData);
32+
deck.SetKeyBitmap(i, img);
33+
}
34+
}
35+
36+
private static Bitmap ResizeToFullStreamDeckImage(Bitmap b)
37+
{
38+
var newBm = new Bitmap(fullPxWidth, fullPxHeight, PixelFormat.Format24bppRgb);
39+
double scale = Math.Min((double)fullPxWidth / b.Width, (double)fullPxHeight / b.Height);
40+
41+
using (var g = Graphics.FromImage(newBm))
42+
{
43+
g.InterpolationMode = InterpolationMode.High;
44+
g.CompositingQuality = CompositingQuality.HighQuality;
45+
g.SmoothingMode = SmoothingMode.AntiAlias;
46+
47+
var scaleWidth = (int)(b.Width * scale);
48+
var scaleHeight = (int)(b.Height * scale);
49+
50+
g.FillRectangle(black, new RectangleF(0, 0, fullPxWidth, fullPxHeight));
51+
g.DrawImage(b, new Rectangle(((int)fullPxWidth - scaleWidth) / 2, ((int)fullPxHeight - scaleHeight) / 2, scaleWidth, scaleHeight));
52+
}
53+
54+
return newBm;
55+
}
56+
57+
static byte[] GetRgbArray(Bitmap b)
58+
{
59+
var rect = new Rectangle(0, 0, b.Width, b.Height);
60+
var lockData = b.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
61+
62+
try
63+
{
64+
var data = new byte[fullPxWidth * fullPxHeight * 3];
65+
Marshal.Copy(lockData.Scan0, data, 0, data.Length);
66+
return data;
67+
}
68+
finally
69+
{
70+
b.UnlockBits(lockData);
71+
}
72+
}
73+
74+
static StreamDeckKeyBitmap GetKeyImageFromFull(int keyId, byte[] fullImageData)
75+
{
76+
var y = keyId / 5;
77+
var x = 4 - keyId % 5;
78+
return GetKeyImageFromFull(x, y, fullImageData);
79+
}
80+
81+
static StreamDeckKeyBitmap GetKeyImageFromFull(int xPos, int yPos, byte[] fullImageData)
82+
{
83+
var keyImgData = new byte[buttonPxSize * buttonPxSize * 3];
84+
var xOffset = xPos * (buttonPxSize + buttonPxDist);
85+
var yOffset = yPos * (buttonPxSize + buttonPxDist);
86+
87+
for (int y = 0; y < buttonPxSize; y++)
88+
{
89+
var numberOfPixelsInPrevRows = (y + yOffset) * fullPxWidth + xOffset;
90+
for (int x = 0; x < buttonPxSize; x++)
91+
{
92+
var p = (numberOfPixelsInPrevRows + x) * 3;
93+
var kPos = (y * buttonPxSize + x) * 3;
94+
keyImgData[kPos + 0] = fullImageData[p + 0];
95+
keyImgData[kPos + 1] = fullImageData[p + 1];
96+
keyImgData[kPos + 2] = fullImageData[p + 2];
97+
}
98+
}
99+
100+
return StreamDeckKeyBitmap.FromRawBitmap(keyImgData);
101+
}
102+
}
103+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{1053A6D8-353E-4848-9434-5A35F32E9180}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>StreamDeckSharp.Examples.DrawFullScreen</RootNamespace>
10+
<AssemblyName>StreamDeckSharp.Examples.DrawFullScreen</AssemblyName>
11+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
</PropertyGroup>
14+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
15+
<PlatformTarget>AnyCPU</PlatformTarget>
16+
<DebugSymbols>true</DebugSymbols>
17+
<DebugType>full</DebugType>
18+
<Optimize>false</Optimize>
19+
<OutputPath>bin\Debug\</OutputPath>
20+
<DefineConstants>DEBUG;TRACE</DefineConstants>
21+
<ErrorReport>prompt</ErrorReport>
22+
<WarningLevel>4</WarningLevel>
23+
</PropertyGroup>
24+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
25+
<PlatformTarget>AnyCPU</PlatformTarget>
26+
<DebugType>pdbonly</DebugType>
27+
<Optimize>true</Optimize>
28+
<OutputPath>bin\Release\</OutputPath>
29+
<DefineConstants>TRACE</DefineConstants>
30+
<ErrorReport>prompt</ErrorReport>
31+
<WarningLevel>4</WarningLevel>
32+
</PropertyGroup>
33+
<ItemGroup>
34+
<Reference Include="System" />
35+
<Reference Include="System.Core" />
36+
<Reference Include="System.Drawing" />
37+
<Reference Include="System.Xml.Linq" />
38+
<Reference Include="System.Data.DataSetExtensions" />
39+
<Reference Include="Microsoft.CSharp" />
40+
<Reference Include="System.Data" />
41+
<Reference Include="System.Xml" />
42+
</ItemGroup>
43+
<ItemGroup>
44+
<Compile Include="Program.cs" />
45+
<Compile Include="Properties\AssemblyInfo.cs" />
46+
<Compile Include="StreamDeckFullScreenDrawingExtension.cs" />
47+
</ItemGroup>
48+
<ItemGroup>
49+
<ProjectReference Include="..\StreamDeckSharp\StreamDeckSharp.csproj">
50+
<Project>{d06ab787-766e-4b28-89c4-8d948070eb1c}</Project>
51+
<Name>StreamDeckSharp</Name>
52+
</ProjectReference>
53+
</ItemGroup>
54+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
55+
</Project>

src/StreamDeckSharp.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StreamDeckSharp.Examples.Dr
1313
EndProject
1414
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HidLibrary", "HidLibrary\src\HidLibrary\HidLibrary.csproj", "{9E8F1D50-74EA-4C60-BD5C-AB2C5B53BC66}"
1515
EndProject
16+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StreamDeckSharp.Examples.DrawFullScreen", "StreamDeckSharp.Examples.DrawFullScreen\StreamDeckSharp.Examples.DrawFullScreen.csproj", "{1053A6D8-353E-4848-9434-5A35F32E9180}"
17+
EndProject
1618
Global
1719
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1820
Debug|Any CPU = Debug|Any CPU
@@ -39,6 +41,10 @@ Global
3941
{9E8F1D50-74EA-4C60-BD5C-AB2C5B53BC66}.Debug|Any CPU.Build.0 = Debug|Any CPU
4042
{9E8F1D50-74EA-4C60-BD5C-AB2C5B53BC66}.Release|Any CPU.ActiveCfg = Release|Any CPU
4143
{9E8F1D50-74EA-4C60-BD5C-AB2C5B53BC66}.Release|Any CPU.Build.0 = Release|Any CPU
44+
{1053A6D8-353E-4848-9434-5A35F32E9180}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
45+
{1053A6D8-353E-4848-9434-5A35F32E9180}.Debug|Any CPU.Build.0 = Debug|Any CPU
46+
{1053A6D8-353E-4848-9434-5A35F32E9180}.Release|Any CPU.ActiveCfg = Release|Any CPU
47+
{1053A6D8-353E-4848-9434-5A35F32E9180}.Release|Any CPU.Build.0 = Release|Any CPU
4248
EndGlobalSection
4349
GlobalSection(SolutionProperties) = preSolution
4450
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)