Skip to content

Instantly share code, notes, and snippets.

@saibimajdi
Last active May 24, 2019 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saibimajdi/bf8bb4d9abd59df48a1c53b1cefcebc3 to your computer and use it in GitHub Desktop.
Save saibimajdi/bf8bb4d9abd59df48a1c53b1cefcebc3 to your computer and use it in GitHub Desktop.
using System;
using System.Drawing;
using System.IO;
using System.Net.Mime;
namespace imageProcess
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
var filePath = @"D:\code\imageProcess\picture.JPG";
var bitmap = GetBitmap(filePath);
var newBitmap = new Bitmap(bitmap.Width/2, bitmap.Height / 2);
var indexI = 0;
var indexJ = 0;
for (int i = 0; i < newBitmap.Width; i++)
{
indexJ = 0;
for (int j = 0; j < newBitmap.Height; j++)
{
var pixel00 = bitmap.GetPixel(indexI, indexJ);
var pixel01 = bitmap.GetPixel(indexI, indexJ + 1);
var pixel10 = bitmap.GetPixel(indexI + 1, indexJ);
var pixel11 = bitmap.GetPixel(indexI + 1, indexJ + 1);
var A = (pixel00.A + pixel01.A + pixel10.A + pixel11.A) / 4;
var R = (pixel00.R + pixel01.R + pixel10.R + pixel11.R) / 4;
var G = (pixel00.G + pixel01.G + pixel10.G + pixel11.G) / 4;
var B = (pixel00.B + pixel01.B + pixel10.B + pixel11.B) / 4;
//newBitmap.SetPixel(i, j, Color.FromArgb(A,R,G,B));
if (((R + G + B) / 3) > 127)
newBitmap.SetPixel(i, j, Color.FromArgb(A, 255, 0, 0));
else
newBitmap.SetPixel(i, j, Color.FromArgb(A, 0, 0, 0));
indexJ += 2;
}
indexI += 2;
}
newBitmap.Save("new.bmp");
}
static System.Drawing.Bitmap GetBitmap(string filePath)
{
Bitmap bitmap;
using(Stream stream = File.Open(filePath, System.IO.FileMode.Open))
{
Image image = Image.FromStream(stream);
bitmap = new Bitmap(image);
}
return bitmap;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment