コード例 #1
0
        public static Bitmap Classify(out CursorClassification classification)
        {
            var result = new Bitmap(32, 32);

            try
            {
                NativeMethods.CURSORINFO pci;
                pci.cbSize = Marshal.SizeOf(typeof(NativeMethods.CURSORINFO));

                using (var g = Graphics.FromImage(result))
                {
                    if (NativeMethods.GetCursorInfo(out pci))
                    {
                        if (pci.flags == NativeMethods.CURSOR_SHOWING)
                        {
                            var hdc = g.GetHdc();
                            NativeMethods.DrawIconEx(hdc, 0, 0, pci.hCursor, 0, 0, 0, IntPtr.Zero, NativeMethods.DI_NORMAL);
                            g.ReleaseHdc();
                        }
                    }
                }

                var hash = ImageHashing.AverageHash(result);

                //var filename = hash + ".bmp";
                //if (!File.Exists(filename))
                //{
                //    result.Save(filename);
                //}

                var matching = imageHashes.SelectMany(i => i.Value.Select(v => (similarity: ImageHashing.Similarity(hash, v), imagehash: i)))
                               .Where(t => t.similarity > 80)
                               .OrderByDescending(t => t.similarity)
                               .FirstOrDefault();

                classification = matching.imagehash.Key;
                Debug.WriteLine(classification.ToString() + " " + matching.similarity);

                if (classification == 0)
                {
                    classification = CursorClassification.None;
                }

                return(result);
            }
            catch
            {
                classification = CursorClassification.Unknown;
                return(result);
            }
        }
コード例 #2
0
        public static Bitmap Classify(out CursorClassification classification)
        {
            var result = new Bitmap(32, 32);

            try
            {
                NativeMethods.CURSORINFO pci;
                pci.cbSize = Marshal.SizeOf(typeof(NativeMethods.CURSORINFO));

                using (var g = Graphics.FromImage(result))
                {
                    if (NativeMethods.GetCursorInfo(out pci))
                    {
                        if (pci.flags == NativeMethods.CURSOR_SHOWING)
                        {
                            var hdc = g.GetHdc();
                            NativeMethods.DrawIconEx(hdc, 0, 0, pci.hCursor, 0, 0, 0, IntPtr.Zero, NativeMethods.DI_NORMAL);
                            g.ReleaseHdc();
                        }
                    }
                }

                var hash = ImageHashing.AverageHash(result);
                //logger.LogInformation("Hash: " + hash);

                var matching = imageHashes.Select(i => (similarity: ImageHashing.Similarity(hash, i.Value), imagehash: i))
                               .OrderByDescending(t => t.similarity)
                               .First();

                classification = matching.imagehash.Key;
                //System.Diagnostics.logger.LogInformation(classification);
                return(result);
            }
            catch
            {
                classification = CursorClassification.Unknown;
                return(result);
            }
        }