Also folgender Stand:
Ich habe eine Anwendung, die einen Screenshot erstellt und diesen dann auswertet und die Koordinaten eines bestimmten Farbwertes sucht.
Da er allerdings der Kompletten screen durchsucht wird dauert das ganze zu lange.
Ich möchte gerne nur einen Bereich um die aktuelle Mausposition durchsuchen.
Mit Bitmap etc habe ich noch nicht so viel gearbeitet und ich weis nicht wirklich, wie ich das ganze dann angebe.
Anbei der Code:
Screen vom Bildschirm
Code:
private Bitmap CaptureScreen() { //Point a = new Point(); //a = Control.MousePosition; Bitmap b = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); using (Graphics g = Graphics.FromImage(b)) { g.CopyFromScreen(new Point(0, 0), new Point(0, 0), b.Size); } return b; }
Code:
public Point GetPixelPosition(Color SearchColor, bool IgnoreAlphaChannel) { //Point a = new Point(); //a = Control.MousePosition; _ColorFound = false; Point PixelPt = new Point(0, 0); using (Bitmap b = CaptureScreen()) { for (int i = 0; i < b.Width; i++) { if (this._ColorFound) break; for (int j = 0; j < b.Height; j++) { if (this._ColorFound) break; Color tmpPixelColor = b.GetPixel(i, j); if (((tmpPixelColor.A == SearchColor.A) || IgnoreAlphaChannel) && (tmpPixelColor.R == SearchColor.R) && (tmpPixelColor.G == SearchColor.G) && (tmpPixelColor.B == SearchColor.B) ) { PixelPt.X = i; PixelPt.Y = j; this._ColorFound = true; } } } } return PixelPt; }
hat jemand eine Idee?
Mfg OddN3ss
#SOLVED°!
Code:
public Point GetPixelPosition(Color SearchColor, bool IgnoreAlphaChannel, int pixelsToSearchAround) { Point mousePosition = Cursor.Position; _ColorFound = false; Point PixelPt = new Point(0, 0); using (Bitmap b = CaptureScreen()) { int minX = mousePosition.X - pixelsToSearchAround; int maxX = mousePosition.X + pixelsToSearchAround; int minY = mousePosition.Y - pixelsToSearchAround; int maxY = mousePosition.Y + pixelsToSearchAround; if(minX < 0) minX = 0; if(minY < 0) minY = 0; if(maxX > b.Width) maxX = b.Width; if(maxY > b.Height) maxY = b.Height; for (int i = minX; i < maxX; i++) { if (this._ColorFound) break; for (int j = minY; j < maxY; j++) { if (this._ColorFound) break; Color tmpPixelColor = b.GetPixel(i, j); if (((tmpPixelColor.A == SearchColor.A) || IgnoreAlphaChannel) && (tmpPixelColor.R == SearchColor.R) && (tmpPixelColor.G == SearchColor.G) && (tmpPixelColor.B == SearchColor.B) ) { PixelPt.X = i; PixelPt.Y = j; this._ColorFound = true; } } } } return PixelPt; }