Skip to content

Commit

Permalink
handle errors again
Browse files Browse the repository at this point in the history
  • Loading branch information
dnmalenke committed Nov 4, 2021
1 parent 131a012 commit 675a8f3
Showing 5 changed files with 35 additions and 38 deletions.
2 changes: 1 addition & 1 deletion Calc2KeyCE.Calc/makefile
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
NAME ?= CALC2KEY
COMPRESSED ?= YES
ICON ?= iconc.png
DESCRIPTION ?= "Calc2KEYCE"
DESCRIPTION ?= "Calc2KEYCE 1.4.0"
ARCHIVED ?= NO

CFLAGS ?= -Wall -Wextra -Oz
4 changes: 3 additions & 1 deletion Calc2KeyCE.Core/Calc2KeyCE.Core.csproj
Original file line number Diff line number Diff line change
@@ -2,7 +2,9 @@

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Version>1.3.0</Version>
<Version>1.4.0</Version>
<Authors>David Malenke</Authors>
<Company />
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
9 changes: 0 additions & 9 deletions Calc2KeyCE.Core/ScreenMirroring/ScreenMirror.cs
Original file line number Diff line number Diff line change
@@ -14,7 +14,6 @@ public class ScreenMirror
private UsbEndpointWriter _calcWriter;
private Thread _sendThread;
private Thread _screenThread;
// private Thread _captureThread;

private byte[] _compressedImage;
private byte[] _uncompressedImage;
@@ -55,14 +54,6 @@ public void StopMirroring()
}
}

private void CaptureScreen()
{
while (_connected)
{
_uncompressedImage = _captureFunc.Invoke();
}
}

private unsafe void CompressScreenArray()
{
#if DEBUG
3 changes: 2 additions & 1 deletion Calc2KeyCE.Windows/Calc2KeyCE.Windows.csproj
Original file line number Diff line number Diff line change
@@ -7,8 +7,9 @@
<Platforms>AnyCPU</Platforms>
<AssemblyName>Calc2KeyCE</AssemblyName>
<RootNamespace>Calc2KeyCE</RootNamespace>
<Version>1.3.1</Version>
<Version>1.4.0</Version>
<Authors>David Malenke</Authors>
<Company />
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
55 changes: 29 additions & 26 deletions Calc2KeyCE.Windows/ScreenMirroring/CaptureMonitor.cs
Original file line number Diff line number Diff line change
@@ -14,12 +14,13 @@ namespace Calc2KeyCE.ScreenMirroring
{
public static class CaptureMonitor
{
private static int errorCount = 0;
private static int _errorCount = 0;

private static byte[] CaptureErr(Screen monitor, Exception ex)
{
if(errorCount > 2)
if (_errorCount > 3)
{
MessageBox.Show(ex.Message, "Error");
throw ex;
}

@@ -28,9 +29,11 @@ private static byte[] CaptureErr(Screen monitor, Exception ex)

public static byte[] Capture(Screen monitor)
{
Bitmap resultBmp = null;

byte[] imageBytes = null;
try
{
Bitmap resultBmp = null;
Rectangle monitorRect = monitor.Bounds;
HWND desktopWindow = User32.GetDesktopWindow();
HDC windowDc = User32.GetWindowDC(desktopWindow);
@@ -61,35 +64,35 @@ public static byte[] Capture(Screen monitor)
Gdi32.DeleteDC(memDc);
User32.ReleaseDC(desktopWindow, windowDc);

errorCount = 0;
}
catch (Exception ex)
{
errorCount++;
return CaptureErr(monitor,ex);
}
var shrunkImage = resultBmp.GetThumbnailImage(320, 240, null, IntPtr.Zero);
resultBmp.Dispose();

var shrunkImage = resultBmp.GetThumbnailImage(320, 240, null, IntPtr.Zero);
resultBmp.Dispose();
var cloned = shrunkImage.ConvertPixelFormatAsync(PixelFormat.Format8bppIndexed, OptimizedPaletteQuantizer.Octree(), ErrorDiffusionDitherer.FloydSteinberg, new TaskConfig()).GetAwaiter().GetResult();
shrunkImage.Dispose();

var cloned = shrunkImage.ConvertPixelFormatAsync(PixelFormat.Format8bppIndexed, OptimizedPaletteQuantizer.Octree(), ErrorDiffusionDitherer.FloydSteinberg, new TaskConfig()).GetAwaiter().GetResult();
shrunkImage.Dispose();
short[] colors = new short[256];

short[] colors = new short[256];
var pal = (Color[])cloned.Palette.Entries.Clone();
Parallel.For(0, cloned.Palette.Entries.Length, i =>
{
colors[i] = ConvertColor(pal[i].R, pal[i].G, pal[i].B);
});

var pal = (Color[])cloned.Palette.Entries.Clone();
Parallel.For(0, cloned.Palette.Entries.Length, i =>
{
colors[i] = ConvertColor(pal[i].R, pal[i].G, pal[i].B);
});
imageBytes = new byte[colors.Length * sizeof(short) + cloned.Width * cloned.Height];
Buffer.BlockCopy(colors, 0, imageBytes, 0, colors.Length * sizeof(short));

byte[] imageBytes = new byte[colors.Length * sizeof(short) + cloned.Width * cloned.Height];
Buffer.BlockCopy(colors, 0, imageBytes, 0, colors.Length * sizeof(short));
BitmapData imageData = cloned.LockBits(new Rectangle(0, 0, cloned.Width, cloned.Height), ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
Marshal.Copy(imageData.Scan0, imageBytes, colors.Length * sizeof(short), cloned.Width * cloned.Height);
cloned.UnlockBits(imageData);
cloned.Dispose();

BitmapData imageData = cloned.LockBits(new Rectangle(0, 0, cloned.Width, cloned.Height), ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
Marshal.Copy(imageData.Scan0, imageBytes, colors.Length * sizeof(short), cloned.Width * cloned.Height);
cloned.UnlockBits(imageData);
cloned.Dispose();
_errorCount = 0;
}
catch (Exception ex)
{
_errorCount++;
return CaptureErr(monitor, ex);
}

return imageBytes;
}

0 comments on commit 675a8f3

Please sign in to comment.