From 7a6c2aca013a25ac06e60609b17ce1c2e88bff9a Mon Sep 17 00:00:00 2001 From: Jorg Tretter Date: Wed, 14 Jul 2021 09:00:54 -0500 Subject: [PATCH] Add gray scale conversion --- SShot/Program.cs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/SShot/Program.cs b/SShot/Program.cs index f142b0f..6d8e9dc 100644 --- a/SShot/Program.cs +++ b/SShot/Program.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; +using System.Runtime.InteropServices; namespace SShot { static class Program { @@ -25,16 +26,26 @@ namespace SShot { Bitmap captureBitmap = new Bitmap(captureRectangle.Width, captureRectangle.Height, PixelFormat.Format32bppArgb); Graphics captureGraphics = Graphics.FromImage(captureBitmap); captureGraphics.CopyFromScreen(captureRectangle.Left, captureRectangle.Top, 0, 0, captureRectangle.Size); + toGrayscale(captureBitmap); captureBitmap.Save(filename, ImageFormat.Png); - } - catch (Exception ex) { + } catch (Exception ex) { if (args.Length > 0) { - if (args[0] != "-QuietErrors") { + if (args[0] != "-QuietErrors") { MessageBox.Show(ex.Message); } } } } + private static void toGrayscale(Bitmap bmp) { + for (int y = 0; y < bmp.Height; y++) { + for (int x = 0; x < bmp.Width; x++) { + var c = bmp.GetPixel(x, y); + int rgb = (c.R + c.G + c.B) / 3; + + bmp.SetPixel (x,y,Color.FromArgb(c.A, rgb,rgb,rgb)); + } + } + } } }