現在、センター画面のピクセルの色の変化を検出しようとしています。しかし、どういうわけか、それは常に最初の変化を返すでしょう。C#センター画面のピクセルの色の変化を検出しました
私の現在のコード:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace pixelChange
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public Color startingColor;
// Get center pixel color rbg once the form loaded
private void Form1_Load(object sender, EventArgs e)
{
startingColor = GetPixelColor(Screen.PrimaryScreen.Bounds.Width/2, Screen.PrimaryScreen.Bounds.Height/2);
}
// Timer which calls the GetPixelColor to check for a difference
private void timer1_Tick(object sender, EventArgs e)
{
checkForColorDifference(startingColor);
}
// Get pixel color function
public Color GetPixelColor(int x, int y)
{
Bitmap snapshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
using (Graphics gph = Graphics.FromImage(snapshot))
{
gph.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
}
return snapshot.GetPixel(x, y);
}
// Function to check if the color of the center pxiel changed
public void checkForColorDifference(Color start)
{
Color starting = start;
Color currentColor = GetPixelColor(Screen.PrimaryScreen.Bounds.Width/2, Screen.PrimaryScreen.Bounds.Height/2);
if (starting != currentColor)
{
MessageBox.Show("Color: " + start + " changed to:" + currentColor.ToString() + ".", "Color change response");
startingColor = currentColor;
}
}
}
}
コードは、これまでの仕組み:
- Form1_Load getがトリガされ、公共の色startingColorています。 GetPixelColor関数の結果である色で塗りつぶされます。
- タイマーは、各ティックごとにcheckForColorDifference関数を呼び出します。
- checkForColorDifference関数は、startingColorをパラメータとして受け入れ、次の方法で色を比較します。starting.ToString()!= currentColor.ToString()
- 色が一致しない場合、メッセージボックスはショーとパブリックColor startingColor; Color currentColorの内部に格納されている2番目の色で塗りつぶされるはずです。
しかし、コードは常に同じ誤った応答(最初の応答)を返します。私は間違って何をしていますか?問題の
スクリーンショット:https://i.gyazo.com/0162e404a3ecef2820024e4f93678d2a.png
、あなたは文字列表現を比較しないで、直接色を比較する必要があります。 'starting'と' currentColor'にはどのような値がありますか?それらは両方とも 'System.Drawing.Color'型であり、多くのプロパティを持っています。これらの値について教えてください。そうすれば、あなたが望むように動作していない理由を知ることができます。 – SlimsGhost
if部分を編集しました。問題はstartingColorが更新されていません。したがって、常に同じ色を比較します。スクリーンショット:https://i.stack.imgur.com/Rg4fg.png – d45ndx
メッセージボックスを閉じるまで色が更新されない – David