Skip to content

Commit

Permalink
New example project to draw full screen image on stream deck
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Wischenbart committed Jan 29, 2018
1 parent a8cbd04 commit 847da44
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/StreamDeckSharp.Examples.DrawFullScreen/Program.cs
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();
}
}
}
}
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")]
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);
}
}
}
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>
6 changes: 6 additions & 0 deletions src/StreamDeckSharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StreamDeckSharp.Examples.Dr
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HidLibrary", "HidLibrary\src\HidLibrary\HidLibrary.csproj", "{9E8F1D50-74EA-4C60-BD5C-AB2C5B53BC66}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StreamDeckSharp.Examples.DrawFullScreen", "StreamDeckSharp.Examples.DrawFullScreen\StreamDeckSharp.Examples.DrawFullScreen.csproj", "{1053A6D8-353E-4848-9434-5A35F32E9180}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -39,6 +41,10 @@ Global
{9E8F1D50-74EA-4C60-BD5C-AB2C5B53BC66}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9E8F1D50-74EA-4C60-BD5C-AB2C5B53BC66}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9E8F1D50-74EA-4C60-BD5C-AB2C5B53BC66}.Release|Any CPU.Build.0 = Release|Any CPU
{1053A6D8-353E-4848-9434-5A35F32E9180}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1053A6D8-353E-4848-9434-5A35F32E9180}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1053A6D8-353E-4848-9434-5A35F32E9180}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1053A6D8-353E-4848-9434-5A35F32E9180}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit 847da44

Please sign in to comment.