[C#]當屏幕檢測到白色啟動原神,黑色啟動星穹鐵道的抽象項目

首先:這玩意兒就是純純的整活,一個小時就完成了,大家就當看個樂呵

靈感來自于基于【白屏檢測】的 原神啟動器
以及其評論

于是就用C#做了這么一個玩意兒

使用方法也是十分的簡單,只要輸入原神和星鐵的exe地址之后當檢測到全屏的白色或者黑色就會啟動對應的游戲十分簡單粗暴
接下來是代碼:
public Form1()
? ? ? ? {
? ? ? ? ? ? InitializeComponent();
? ? ? ? }
? ? ? ? bool isshowW =false;
? ? ? ? bool isshowB = false;
? ? ? ? private void Timer_Tick(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? Rectangle rectangle = Screen.PrimaryScreen.Bounds;
? ? ? ? ? ? Bitmap bitmap = new Bitmap(rectangle.Width, rectangle.Height, PixelFormat.Format32bppArgb);
? ? ? ? ? ? Graphics g = Graphics.FromImage(bitmap);
? ? ? ? ? ? g.CopyFromScreen(rectangle.Left, rectangle.Top, 0, 0, bitmap.Size);
? ? ? ? ? ? int totalPixels = bitmap.Width * bitmap.Height;
? ? ? ? ? ? int rSum = 0, gSum = 0, bSum = 0;
? ? ? ? ? ? BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
? ? ? ? ? ? IntPtr ptr = bitmapData.Scan0;
? ? ? ? ? ? int bytes = Math.Abs(bitmapData.Stride) * bitmap.Height;
? ? ? ? ? ? byte[] rgbValues = new byte[bytes];
? ? ? ? ? ? Marshal.Copy(ptr, rgbValues, 0, bytes);
? ? ? ? ? ? for (int i = 0; i < bytes; i += 4)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? byte b = rgbValues[i];
? ? ? ? ? ? ? ? byte gs = rgbValues[i + 1];
? ? ? ? ? ? ? ? byte r = rgbValues[i + 2];
? ? ? ? ? ? ? ? rSum += r;
? ? ? ? ? ? ? ? gSum += gs;
? ? ? ? ? ? ? ? bSum += b;
? ? ? ? ? ? }
? ? ? ? ? ? bitmap.UnlockBits(bitmapData);
? ? ? ? ? ? bitmap.Dispose();
? ? ? ? ? ? g.Dispose();
? ? ? ? ? ? int avgR = rSum / totalPixels;
? ? ? ? ? ? int avgG = gSum / totalPixels;
? ? ? ? ? ? int avgB = bSum / totalPixels;
? ? ? ? ? ? bool isScreenWhite = IsColorCloseToWhite(avgR, avgG, avgB);
? ? ? ? ? ? bool isScreenBlack = IsColorCloseToBlack(avgR, avgG, avgB);
? ? ? ? ? ? if (isScreenWhite)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? // 屏幕全白,執(zhí)行相應操作
? ? ? ? ? ? ? ? if(isshowW == false)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? Process.Start(textBox1.Text);
? ? ? ? ? ? ? ? ? ? isshowW = true;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? else if (!isScreenWhite)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? isshowW = false;
? ? ? ? ? ? }
? ? ? ? ? ? if (isScreenBlack)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (isshowB == false)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? Process.Start(textBox2.Text);
? ? ? ? ? ? ? ? ? ? isshowB = true;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? else if (!isScreenBlack)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? isshowB = false;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? private bool IsColorCloseToWhite(int r, int g, int b)
? ? ? ? {
? ? ? ? ? ? // 定義一個閾值,判斷顏色是否接近白色
? ? ? ? ? ? int threshold = 250;
? ? ? ? ? ? return r >= threshold && g >= threshold && b >= threshold;
? ? ? ? }
? ? ? ? private bool IsColorCloseToBlack(int r, int g, int b)
? ? ? ? {
? ? ? ? ? ? // 定義一個閾值,判斷顏色是否接近白色
? ? ? ? ? ? int threshold = 15;
? ? ? ? ? ? return r <= threshold && g <= threshold && b <= threshold;
? ? ? ? }
? ? ? ? bool isqidong =false;
? ? ? ? private void button1_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? Timer timer = new Timer();
? ? ? ? ? ? timer.Tick += Timer_Tick;
? ? ? ? ? ? if (!isqidong)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? timer.Start();
? ? ? ? ? ? ? ? button1.Text = "停止";
? ? ? ? ? ? ? ? isqidong = true;
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? timer.Stop();
? ? ? ? ? ? ? ? button1.Text = "啟動";
? ? ? ? ? ? ? ? isqidong = false;
? ? ? ? ? ? }
? ? ? ? ? ??
? ? ? ? }
? ? }
(放心絕對綠色無污染)
下載地址:
鏈接:https://pan.baidu.com/s/1ghSbFrOEZ0-_RlVf1uxegQ?pwd=f9rk?
提取碼:f9rk

順便預告一下下一個視頻
