Convert to 8-Bit Gray scale instead to save space.

This commit is contained in:
Jorg Tretter
2021-07-14 10:35:08 -05:00
parent 7a6c2aca01
commit 30d3edc9fd

View File

@@ -26,7 +26,7 @@ namespace SShot {
Bitmap captureBitmap = new Bitmap(captureRectangle.Width, captureRectangle.Height, PixelFormat.Format32bppArgb); Bitmap captureBitmap = new Bitmap(captureRectangle.Width, captureRectangle.Height, PixelFormat.Format32bppArgb);
Graphics captureGraphics = Graphics.FromImage(captureBitmap); Graphics captureGraphics = Graphics.FromImage(captureBitmap);
captureGraphics.CopyFromScreen(captureRectangle.Left, captureRectangle.Top, 0, 0, captureRectangle.Size); captureGraphics.CopyFromScreen(captureRectangle.Left, captureRectangle.Top, 0, 0, captureRectangle.Size);
toGrayscale(captureBitmap); captureBitmap = to8BitGS(captureBitmap);
captureBitmap.Save(filename, ImageFormat.Png); captureBitmap.Save(filename, ImageFormat.Png);
} catch (Exception ex) { } catch (Exception ex) {
if (args.Length > 0) { if (args.Length > 0) {
@@ -37,15 +37,36 @@ namespace SShot {
} }
} }
private static void toGrayscale(Bitmap bmp) { private static Bitmap to8BitGS(Bitmap bmp) {
var result = new Bitmap(bmp.Width, bmp.Height, PixelFormat.Format8bppIndexed);
BitmapData data = result.LockBits(new Rectangle(0, 0, result.Width, result.Height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
// Copy the bytes from the image into a byte array
byte[] bytes = new byte[data.Width * data.Height ];
Marshal.Copy(data.Scan0, bytes, 0, bytes.Length);
for (int y = 0; y < bmp.Height; y++) { for (int y = 0; y < bmp.Height; y++) {
for (int x = 0; x < bmp.Width; x++) { for (int x = 0; x < bmp.Width; x++) {
var c = bmp.GetPixel(x, y); var c = bmp.GetPixel(x, y);
int rgb = (c.R + c.G + c.B) / 3; var rgb = (byte)((c.R + c.G + c.B) / 3);
bmp.SetPixel (x,y,Color.FromArgb(c.A, rgb,rgb,rgb)); bytes[y * data.Stride + x] = rgb;
} }
} }
// Copy the bytes from the byte array into the image
Marshal.Copy(bytes, 0, data.Scan0, bytes.Length);
result.UnlockBits(data);
ColorPalette pal = result.Palette;
for (int i = 0; i < 256; i++) {
pal.Entries[i] = Color.FromArgb(255, i, i, i);
}
result.Palette = pal;
return result;
} }
} }
} }