private static Bitmap GetBitmapFromBlob(Bitmap source, Blob blob) { int rX = blob.Rectangle.Width / 2; int rY = blob.Rectangle.Height / 2; Bitmap result = new Bitmap(blob.Rectangle.Width, blob.Rectangle.Height); IColorQuantizer quantizer = new MedianCutQuantizer(); for (int x = 0; x < blob.Rectangle.Width - 1; x++) { for (int y = 0; y < blob.Rectangle.Height - 1; y++) { Color color = source.GetPixel(blob.Rectangle.Location.X + x, blob.Rectangle.Location.Y + y); if (x > (rX / 2) && x < blob.Rectangle.Width - (rX / 2) && y > (rY / 2) && y < blob.Rectangle.Height - (rY / 2)) { quantizer.AddColor(color); } result.SetPixel(x, y, color); } } Color[] palette = quantizer.GetPalette(1); return(result); }
private static ObjectStruct GetDominantColorFromBlob(Bitmap source, Blob blob) { int rX = blob.Rectangle.Width / 2; int rY = blob.Rectangle.Height / 2; // int r = blob.Rectangle.Width > 200 ? 40 : 2; int r = blob.Rectangle.Width / 4; IColorQuantizer quantizer = new MedianCutQuantizer(); for (int x = 0; x < blob.Rectangle.Width - 1; x++) { for (int y = 0; y < blob.Rectangle.Height - 1; y++) { Color color = source.GetPixel(blob.Rectangle.Location.X + x, blob.Rectangle.Location.Y + y); if (x > rX - r && x < rX + r && y > rY - r && y < rY + r) { double hue; double saturation; double value; ColorToHSV(color, out hue, out saturation, out value); Color alignedСolor = ColorFromHSV(hue, saturation, value); quantizer.AddColor(alignedСolor); } } } // int paletteLenght = blob.Rectangle.Width > 200 ? 24 : 24; 36 64 int paletteLenght = 256; Color[] color1 = quantizer.GetPalette(paletteLenght); ObjectStruct obj = new ObjectStruct(); int lenght = paletteLenght; obj.Tone = new HSVColor[lenght]; int index = 0; foreach (Color color in color1) { double hue; double saturation; double value; ColorToHSV(color, out hue, out saturation, out value); obj.Tone[index] = new HSVColor(hue, saturation, value); index++; } obj.Radius = rX; return(obj); }
private void Timer_Tick(object sender, EventArgs e) { using (var bitmap = new Bitmap((int)SystemParameters.PrimaryScreenWidth, (int)SystemParameters.PrimaryScreenHeight, System.Drawing.Imaging.PixelFormat.Format32bppRgb)) { using (var bmpGraphics = Graphics.FromImage(bitmap)) { bmpGraphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size); //var bitmapPtr = screenBmp.GetHbitmap(); //var bitmapSource = Imaging.CreateBitmapSourceFromHBitmap( // bitmapPtr, // IntPtr.Zero, // Int32Rect.Empty, // BitmapSizeOptions.FromEmptyOptions()); //Bitmap bitmap; //using (var outStream = new MemoryStream()) //{ // BitmapEncoder enc = new BmpBitmapEncoder(); // enc.Frames.Add(BitmapFrame.Create(bitmapSource)); // enc.Save(outStream); // bitmap = new Bitmap(outStream); //} //DeleteObject(bitmapPtr); var bounds = new Rectangle(0, 0, bitmap.Width, bitmap.Height); var bitmapData = bitmap.LockBits(bounds, System.Drawing.Imaging.ImageLockMode.ReadOnly, bitmap.PixelFormat); IntPtr bitmapDataPointer = bitmapData.Scan0; // initalizes the pixel read buffer Int32[] sourceBuffer = new Int32[bitmap.Width]; // sets the offset to the first pixel in the image Int64 sourceOffset = bitmapData.Scan0.ToInt64(); for (Int32 row = 0; row < bitmap.Height; row++) { // copies the whole row of pixels to the buffer Marshal.Copy(new IntPtr(sourceOffset), sourceBuffer, 0, bitmap.Width); // scans all the colors in the buffer foreach (Color color in sourceBuffer.Select(argb => Color.FromArgb(argb))) { _quantizer.AddColor(color); } // increases a source offset by a row sourceOffset += bitmapData.Stride; } bitmap.UnlockBits(bitmapData); var mainColor = _quantizer.GetPalette(2).OrderBy(c => GetBrightness(c)).FirstOrDefault(); _quantizer.Clear(); //GC.Collect(); Background.Fill = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromArgb(mainColor.A, mainColor.R, mainColor.G, mainColor.B)); deviceIO.SetColor(mainColor.R, mainColor.G, mainColor.B, 500); } } }