在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#图形和图像处理 → C# Winform 安卓截屏取色,仿按键精灵手机版抓抓

C# Winform 安卓截屏取色,仿按键精灵手机版抓抓

C#图形和图像处理

下载此实例
  • 开发语言:C#
  • 实例大小:2.20M
  • 下载次数:39
  • 浏览次数:2100
  • 发布时间:2016-08-17
  • 实例类别:C#图形和图像处理
  • 发 布 人:equnasp
  • 文件格式:.rar
  • 所需积分:3
 相关标签: winform C# 按键 按键精灵 c

实例介绍

【实例简介】

模仿按键精灵手机版抓抓功能,实现手机截屏,多点找色,并生成代码

【实例截图】

【核心代码】

using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using log4net;
using System.Collections.Generic;
using System.Threading;
using System.Drawing;
using ADevelop;

namespace AndroidDataTransform
{
    public partial class FormDataSelect : Form
    {
        /// <summary>
        /// PAD 数据上传下载
        /// </summary>
        /// <param name="Tag">默认0:PAD-》PC,1:PC-》PAD</param>
        public FormDataSelect(int Tag)
        {
            InitializeComponent();
            m_Tag = Tag;
            switch (m_Tag)
            {
                case 1:
                    break;
                default:
                    break;
            }

        }

        #region 成员变量
        static ILog m_log = LogManager.GetLogger(typeof(FormDataSelect));
        /// <summary>
        /// 判断当前是上传(0),还是下载(1)
        /// </summary>
        int m_Tag = 0;
        #endregion

        #region 窗体事件
        private void Form1_Load(object sender, EventArgs e)
        {
            ReadConfig();
            string sPath = "c:\\ADevelopTemp";
            if (!Directory.Exists(sPath))
            {
                Directory.CreateDirectory(sPath);
            }
        }
                
        private void cbbDevices_SelectedIndexChanged(object sender, EventArgs e)
        {
            var deviceNo = Convert.ToString(cbbDevices.SelectedItem);
            txbDeviceInfo.Text = string.Format("{0} {1} {2}({3})"
                , AdbHelper.GetDeviceBrand(deviceNo)
                , AdbHelper.GetDeviceModel(deviceNo)
                , AdbHelper.GetDeviceVersionRelease(deviceNo)
                , AdbHelper.GetDeviceVersionSdk(deviceNo));
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            //ProcessHelper.WaitTime = (int)nudWaitTime.Value;
        }
        #endregion

        #region 成员函数
        private void GetInfo()
        {
            Cursor.Current = Cursors.WaitCursor;
            ShowInfo("获取信息中···");
            try
            {
                //启动服务
                if (!AdbHelper.StartServer())
                {
                    ShowInfo("服务启动失败!");
                    return;
                }

                //获取设备列表
                var devs = AdbHelper.GetDevices();
                if (devs.Length == 0 )
                {
                    ShowInfo("没有连接设备!");
                    return;
                }
                if (txbDeviceInfo.Text.Contains("error: device offline"))
                {
                    ShowInfo("设备连接失败,请重新拔插USB线!");
                    return;
                }

                cbbDevices.Items.Clear();
                cbbDevices.Items.AddRange(devs);
                cbbDevices.SelectedIndex = 0;
                
                ShowInfo("信息获取完毕。");

                cbbDevices.SelectedItem = mConfig[IndexOfDevices];
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        private void ShowInfo(string info)
        {
            statusStrip.Text = info;
        }
        #endregion

        #region 设置相关
        private const int IndexOfDevices = 0;
        private const int IndexOfPackages = 1;
        private const int IndexOfDbNames = 2;
        private const int IndexOfPcPath = 3;
        private const int IndexOfWaitTime = 4;
        private const int IndexOfExcelPath = 5;
        private string[] mConfig = new[] { "", "", "", "", "", "" };
        private readonly string mConfigPath = Path.Combine(Application.StartupPath, "adb.ini");
        private Thread screenThread;
        private Image image;
        private int selId = 0;


        private void ReadConfig()
        {
            if (!File.Exists(mConfigPath))
                return;
            mConfig = File.ReadAllLines(mConfigPath);

            if (mConfig == null || mConfig.Length != 6)
                mConfig = new[] { "", "", "", "", "", "" };
        }
        private void SaveConfig()
        {
            File.WriteAllLines(mConfigPath, mConfig);
        }

        #endregion

        private void startScreenshot()
        {
            CheckForIllegalCrossThreadCalls = false;
            ShowInfo("正在截屏...");
            var screen = AdbHelper.getScreenshot();
            if(screen == "")
            {
                ShowInfo("截屏失败!");
            }
            else
            {
                ShowInfo("截屏完毕。");
                this.image = Image.FromFile(screen, false);
                addscreenshot();
            }
        }

        private void addscreenshot()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new MethodInvoker(delegate { addscreenshot(); }));
                return;
            }
            screenshotPicture.Width = image.Width;
            screenshotPicture.Height = image.Height;
            screenshotPicture.Image = image;
            imgPanel.Controls.Add(screenshotPicture);
        }

        private void btnRefresh_Click_1(object sender, EventArgs e)
        {
            GetInfo();
        }

        bool bDrawStart = false;
        Point pointStart = Point.Empty;
        Point pointContinue = Point.Empty;
        Graphics RegionSelGraphics;

        private void screenshotPicture_MouseMove(object sender, MouseEventArgs e)
        {
            statusStrip.Text = e.X.ToString() ", " e.Y.ToString();
            if (bDrawStart)
            {
                pointContinue = e.Location;
                Refresh();
            }
        }

        private void screenshotPicture_MouseUp(object sender, MouseEventArgs e)
        {
            bDrawStart = false;
        }

        private void screenshotPicture_Paint(object sender, PaintEventArgs e)
        {
            if (bDrawStart)
            {
                int startX = pointStart.X;
                int startY = pointStart.Y;
                int endX = pointContinue.X - pointStart.X;
                int endY = pointContinue.Y - pointStart.Y;
                //实时的画矩形
                RegionSelGraphics = e.Graphics;
                RegionSelGraphics.DrawRectangle(Pens.Black, startX, startY, endX, endY);
                selRegionText.Text = startX "," startY "," pointContinue.X "," pointContinue.Y;
            }
        }

        private void screenshotPicture_MouseDown(object sender, MouseEventArgs e)
        {
            int x = e.X;
            int y = e.Y;
            string position = x "," y;
            Bitmap myBitmap = (Bitmap)screenshotPicture.Image;
            Color pixelColor = myBitmap.GetPixel(x, y);
            int r = pixelColor.R;
            int g = pixelColor.G;
            int b = pixelColor.B;
            string getColor = ColorTranslator.ToHtml(Color.FromArgb(r, g, b));
            switch (this.selId)
            {
                case -1:
                    if (bDrawStart)
                    {
                        bDrawStart = false;
                    }
                    else
                    {
                        bDrawStart = true;
                        pointStart = e.Location;
                    }
                    break;
                case 1:
                    button1.BackColor = pixelColor;
                    posText1.Text = position;
                    rText1.Text = r.ToString();
                    gText1.Text = g.ToString();
                    bText1.Text = b.ToString();
                    colorText1.Text = getColor;
                    break;

                case 2:
                    button2.BackColor = pixelColor;
                    posText2.Text = position;
                    rText2.Text = r.ToString();
                    gText2.Text = g.ToString();
                    bText2.Text = b.ToString();
                    colorText2.Text = getColor;
                    break;

                case 3:
                    button3.BackColor = pixelColor;
                    posText3.Text = position;
                    rText3.Text = r.ToString();
                    gText3.Text = g.ToString();
                    bText3.Text = b.ToString();
                    colorText3.Text = getColor;
                    break;

                case 4:
                    button4.BackColor = pixelColor;
                    posText4.Text = position;
                    rText4.Text = r.ToString();
                    gText4.Text = g.ToString();
                    bText4.Text = b.ToString();
                    colorText4.Text = getColor;
                    break;

                case 5:
                    button5.BackColor = pixelColor;
                    posText5.Text = position;
                    rText5.Text = r.ToString();
                    gText5.Text = g.ToString();
                    bText5.Text = b.ToString();
                    colorText5.Text = getColor;
                    break;

                case 6:
                    button6.BackColor = pixelColor;
                    posText6.Text = position;
                    rText6.Text = r.ToString();
                    gText6.Text = g.ToString();
                    bText6.Text = b.ToString();
                    colorText6.Text = getColor;
                    break;

                case 7:
                    button7.BackColor = pixelColor;
                    posText7.Text = position;
                    rText7.Text = r.ToString();
                    gText7.Text = g.ToString();
                    bText7.Text = b.ToString();
                    colorText7.Text = getColor;
                    break;

                case 8:
                    button8.BackColor = pixelColor;
                    posText8.Text = position;
                    rText8.Text = r.ToString();
                    gText8.Text = g.ToString();
                    bText8.Text = b.ToString();
                    colorText8.Text = getColor;
                    break;

                case 9:
                    button9.BackColor = pixelColor;
                    posText9.Text = position;
                    rText9.Text = r.ToString();
                    gText9.Text = g.ToString();
                    bText9.Text = b.ToString();
                    colorText9.Text = getColor;
                    break;

                case 10:
                    button10.BackColor = pixelColor;
                    posText10.Text = position;
                    rText10.Text = r.ToString();
                    gText10.Text = g.ToString();
                    bText10.Text = b.ToString();
                    colorText10.Text = getColor;
                    break;

                default:
                    break;
            }

            if (this.selId >= 1)
            {
                TextBox[] posText = new TextBox[11];
                TextBox[] rText = new TextBox[11];
                TextBox[] gText = new TextBox[11];
                TextBox[] bText = new TextBox[11];
                TextBox[] colorText = new TextBox[11];
                string code;
                foreach (System.Windows.Forms.Control control in this.colorCtrlPanel.Controls)
                {
                    if (control.Name.IndexOf("posText") > -1)
                    {
                        code = control.Name.Substring(7);
                        posText[int.Parse(code)] = (TextBox)control;
                    }

                    if (control.Name.Substring(1, 5) == "rText")
                    {
                        code = control.Name.Substring(5);
                        rText[int.Parse(code)] = (TextBox)control;
                    }

                    if (control.Name.IndexOf("gText") > -1)
                    {
                        code = control.Name.Substring(5);
                        gText[int.Parse(code)] = (TextBox)control;
                    }

                    if (control.Name.IndexOf("bText") > -1)
                    {
                        code = control.Name.Substring(5);
                        bText[int.Parse(code)] = (TextBox)control;
                    }

                    if (control.Name.IndexOf("colorText") > -1)
                    {
                        code = control.Name.Substring(9);
                        colorText[int.Parse(code)] = (TextBox)control;
                    }
                }
                string colorContent = "";
                if (posText[1].Text != "")
                {
                    colorContent = "\"" colorText[1].Text.Replace("#", "") "\",\"";
                    string colors = "";
                    for (int i = 2; i <= 10; i )
                    {
                        string[] firstPos = posText[1].Text.Split(',');
                        int firstX = int.Parse(firstPos[0]);
                        int firstY = int.Parse(firstPos[1]);
                        if (posText[i].Text != "")
                        {
                            string[] thisPos = posText[i].Text.Split(',');
                            int thisX = int.Parse(thisPos[0]);
                            int thisY = int.Parse(thisPos[1]);
                            colors = (thisX - firstX).ToString() "|" (thisY - firstY).ToString() "|" colorText[i].Text.Replace("#", "") ",";
                        }
                        else
                        {
                            break;
                        }
                    }
                    if (colors.Length > 0)
                    {
                        colors = colors.Substring(0, colors.Length - 1);
                    }
                    colorContent = colors "\"";
                    multiColorContentText.Text = colorContent;
                }
            }
        }

        private void screenshotsBtn_Click(object sender, EventArgs e)
        {
            if (image != null)
            {
                image.Dispose();
            }

            this.screenThread = new Thread(startScreenshot);
            this.screenThread.Start();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.selId = 1;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.selId = 2;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.selId = 3;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.selId = 4;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.selId = 5;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            this.selId = 6;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            this.selId = 7;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            this.selId = 8;
        }

        private void button9_Click(object sender, EventArgs e)
        {
            this.selId = 9;
        }

        private void button10_Click(object sender, EventArgs e)
        {
            this.selId = 10;
        }

        private void copyColorContentBtn_Click(object sender, EventArgs e)
        {
            if (multiColorContentText.Text != "")
            {
                Clipboard.SetDataObject(multiColorContentText.Text);
            }
        }

        private void clearMultiMsgBtn_Click(object sender, EventArgs e)
        {
            foreach (System.Windows.Forms.Control control in this.colorCtrlPanel.Controls)
            {
                string typename = control.GetType().FullName;
                switch(typename)
                {
                    case "System.Windows.Forms.TextBox":
                        control.Text = "";
                        break;
                    case "System.Windows.Forms.Button":
                        if(control.Name.Substring(0, 6) == "button")
                        {
                            control.BackColor = Color.Empty;
                        }
                        
                        break;
                    default:
                        break;
                }
            }
        }

        private void regionBtn_Click(object sender, EventArgs e)
        {
            this.selId = -1;
        }

        private void pointSingleGetColorBtn_Click(object sender, EventArgs e)
        {
            this.selId = 0;
        }



        private void cpRegionCodeBtn_Click(object sender, EventArgs e)
        {
            if (selRegionText.Text != "")
            {
                Clipboard.SetDataObject(selRegionText.Text);
            }
        }

        private void restartAdbBtn_Click(object sender, EventArgs e)
        {
            Thread restartThread = new Thread(restartAdb);
            restartThread.Start();
        }

        private void restartAdb()
        {
            ShowInfo("正在重启....");
            AdbHelper.KillServer();
            AdbHelper.StartServer();
            ShowInfo("重启完成!");
            MessageBox.Show("重启完成!");
        }
    }
}

实例下载地址

C# Winform 安卓截屏取色,仿按键精灵手机版抓抓

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警