Ejemplo n.º 1
0
        // 事件相应函数
        private IntPtr MainWindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            switch (msg)
            {
            case Win32.WmHotkey:
                int sid = wParam.ToInt32();
                if (sid == Win32.GlobalAddAtom("Alt-C"))
                {
                    MouseXY.GetCursorPos(out Mouse);
                    IntPtr hdc             = Win32.GetDC(new IntPtr(0));
                    System.Drawing.Point p = new System.Drawing.Point(Mouse.X, Mouse.Y);
                    // 一个颜色通道为8为二进制,通过二进制并且运算和位移截取颜色并进行处理
                    int    c = Win32.GetPixel(hdc, p), r = c & 0xFF, g = (c & 0xFF00) >> 8, b = (c & 0xFF0000) >> 16;
                    string clrstr = "#" + ParseColorToString(r) + ParseColorToString(g) + ParseColorToString(b);
                    //Console.WriteLine(Convert.ToString(c, 16));
                    //Console.WriteLine(Convert.ToString(c & 0xFFffffff, 16));
                    //Console.WriteLine(clrstr);
                    ListBoxItem item = NewColorItem(clrstr);
                    ColorList.Items.Add(item);
                    ColorText.Text = clrstr;
                    ColorList.ScrollIntoView(item);
                    System.Windows.Forms.Clipboard.SetDataObject(clrstr);
                }
                handled = true;
                break;
            }

            return(IntPtr.Zero);
        }
Ejemplo n.º 2
0
        // 截取屏幕从截取的屏幕中读取颜色
        public void GetScreenSnapshot(object sender = null, EventArgs e = null)
        {
            try {
                DateTime now    = DateTime.Now;
                var      bitmap = new Bitmap(10, 10, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

                using (Graphics memoryGrahics = Graphics.FromImage(bitmap))
                {
                    MouseXY.GetCursorPos(out Mouse);
                    memoryGrahics.CopyFromScreen(Mouse.X - 5, Mouse.Y - 5, 0, 0, bitmap.Size, CopyPixelOperation.SourceCopy);
                }
                MemoryStream ms = new MemoryStream();
                bitmap.Save(ms, ImageFormat.Bmp);
                byte[] bytes = ms.GetBuffer();  //byte[]   bytes=   ms.ToArray(); 这两句都可以
                ms.Close();
                //Convert it to BitmapImage
                BitmapImage image = new BitmapImage();
                image.BeginInit();
                image.StreamSource = new MemoryStream(bytes);
                image.EndInit();
                MyImage.Source = image;
            } catch (Exception) {
                Console.WriteLine("-----------------截图错误----------------");
            }
        }