Поиск пикселя - C#
Формулировка задачи:
добрый день
столкнулся с проблемой: нужно искать определенный пиксель и наводить на него курсор
вот нашел такой код:
ну а как наводить курсор так и не понял...
покажите плз рабочий пример
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
GetPixelColor(1280, 1280);
}
static public System.Drawing.Color GetPixelColor(int x, int y)
{
IntPtr hdc = GetDC(IntPtr.Zero);
uint pixel = GetPixel(hdc, x, y);
ReleaseDC(IntPtr.Zero, hdc);
Color color = Color.FromArgb((int)(pixel & 0x000000FF), (int)(pixel & 0x0000FF00) >> 8, (int)(pixel & 0x00FF0000) >> 16);
return color;
}
[DllImport("user32.dll")]
static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("user32.dll")]
static extern Int32 ReleaseDC(IntPtr hwnd, IntPtr hdc);
[DllImport("gdi32.dll")]
static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos);
}
}Решение задачи: «Поиск пикселя»
textual
Листинг программы
public static Point PixelSearch( Rectangle rect, int PixelColor, int Shade_Variation)
{
Color Pixel_Color = Color.FromArgb(PixelColor);
Point Pixel_Coords = new Point(-1, -1);
Bitmap RegionIn_Bitmap = CaptureScreenRegion(rect);
BitmapData RegionIn_BitmapData = RegionIn_Bitmap.LockBits(new Rectangle(0, 0, RegionIn_Bitmap.Width, RegionIn_Bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int[] Formatted_Color = new int[3] { Pixel_Color.B, Pixel_Color.G, Pixel_Color.R }; //bgr
unsafe
{
for (int y = 0; y < RegionIn_BitmapData.Height; y++)
{
byte* row = (byte*)RegionIn_BitmapData.Scan0 + (y * RegionIn_BitmapData.Stride);
for (int x = 0; x < RegionIn_BitmapData.Width; x++)
{
if (row[x * 3] >= (Formatted_Color[0] - Shade_Variation) & row[x * 3] <= (Formatted_Color[0] + Shade_Variation)) //blue
{
if (row[(x * 3) + 1] >= (Formatted_Color[1] - Shade_Variation) & row[(x * 3) + 1] <= (Formatted_Color[1] + Shade_Variation)) //green
{
if (row[(x * 3) + 2] >= (Formatted_Color[2] - Shade_Variation) & row[(x * 3) + 2] <= (Formatted_Color[2] + Shade_Variation)) //red
{
Pixel_Coords = new Point(x + rect.X, y + rect.Y);
goto end;
}
}
}
}
}
}
end:
return Pixel_Coords;
}
private static Bitmap CaptureScreenRegion(Rectangle rect)
{
Bitmap BMP = new Bitmap(rect.Width, rect.Height, PixelFormat.Format24bppRgb);
Graphics GFX = System.Drawing.Graphics.FromImage(BMP);
GFX.CopyFromScreen(rect.X, rect.Y, 0, 0, rect.Size, CopyPixelOperation.SourceCopy);
return BMP;
}
int GetPixel(int x, int y)
{
Bitmap bmp = new Bitmap(800, 600, PixelFormat.Format32bppPArgb);
Graphics grp = Graphics.FromImage(bmp);
grp.CopyFromScreen(new Point(x, y), Point.Empty, new Size(800, 600));
grp.Save();
label1.Text = bmp.Height.ToString();
label2.Text = bmp.Width.ToString();
for (int y1 = 0; y1 < bmp.Height; y1++)
{
for (int x1 = 0; x1 < bmp.Width; x1++)
{
if (bmp.GetPixel(x1, y1).ToArgb() == -65536)
{
goto lol;
}
}
}
lol:
return bmp.GetPixel(0, 0).ToArgb();
}
private void button1_Click_1(object sender, EventArgs e)
{
MessageBox.Show(GetPixel(MousePosition.X, MousePosition.Y).ToString());
}