﻿
{"id":6588,"date":"2017-06-27T00:02:31","date_gmt":"2017-06-27T00:02:31","guid":{"rendered":"http:\/\/www.gencayyildiz.com\/blog\/?p=6588"},"modified":"2017-06-27T00:02:31","modified_gmt":"2017-06-27T00:02:31","slug":"win32-c-ile-ekrandaki-mouse-ile-gelinen-pixelin-rengini-okuma","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/win32-c-ile-ekrandaki-mouse-ile-gelinen-pixelin-rengini-okuma\/","title":{"rendered":"Win32 &#8211; C# ile Ekrandaki Mouse ile Gelinen Pixel&#8217;in Rengini Okuma"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p>Win32 k\u00fct\u00fcphaneleri ile yap\u0131sal olarak her ne kadar i\u015fletim sisteminin nimetlerini kullanarak program\u0131n haricine m\u00fcdahale edebiliyor olsakta, asl\u0131nda renksel baz\u0131 \u00e7al\u0131\u015fmalarda algoritmik olarak ekran\u0131n hangi lokasyonunda oldu\u011fumuzu bulmam\u0131z\u0131 sa\u011flayan yahut renk yo\u011funlu\u011funa g\u00f6re belirli i\u015flemlerin yap\u0131lmas\u0131na imkan verecek olan mouse ile gelinen pixeldeki de\u011feri okumay\u0131 inceliyor olaca\u011f\u0131z.<\/p>\n<p>Mouse&#8217;un bulundu\u011fu lokasyondaki pixele eri\u015fip oradaki rengi alg\u0131lamak i\u00e7in &#8220;<em>user32.dll<\/em>&#8221; ve &#8220;<em>gdi32.dll<\/em>&#8221; Win32 k\u00fct\u00fcphanelerini kullanaca\u011f\u0131z.<\/p>\n<p>Hi\u00e7 vakit kaybetmeden kodumuzu in\u015fa edelim&#8230;<\/p>\n<p>Yapaca\u011f\u0131m\u0131z \u00e7al\u0131\u015fma a\u015fa\u011f\u0131daki gibi olacakt\u0131r;<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        &#x5B;DllImport(&quot;user32.dll&quot;)]\r\n        static extern bool GetCursorPos(ref Point lpPoint);\r\n\r\n        &#x5B;DllImport(&quot;gdi32.dll&quot;, CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]\r\n        public static extern int BitBlt(IntPtr hDC, int x, int y, int nWidth, int nHeight, IntPtr hSrcDC, int xSrc, int ySrc, int dwRop);\r\n\r\n        Bitmap screenPixel = new Bitmap(1, 1, PixelFormat.Format32bppArgb);\r\n        public Color GetColorAt(Point location)\r\n        {\r\n            using (Graphics gdest = Graphics.FromImage(screenPixel))\r\n            {\r\n                using (Graphics gsrc = Graphics.FromHwnd(IntPtr.Zero))\r\n                {\r\n                    IntPtr hSrcDC = gsrc.GetHdc();\r\n                    IntPtr hDC = gdest.GetHdc();\r\n                    int retval = BitBlt(hDC, 0, 0, 1, 1, hSrcDC, location.X, location.Y, (int)CopyPixelOperation.SourceCopy);\r\n                    gdest.ReleaseHdc();\r\n                    gsrc.ReleaseHdc();\r\n                }\r\n            }\r\n\r\n            return screenPixel.GetPixel(0, 0);\r\n        }\r\n<\/pre>\n<p>Yukar\u0131daki kod blo\u011funu incelerseniz e\u011fer &#8220;user32.dll&#8221; k\u00fct\u00fcphanesinden extern etti\u011fimiz &#8220;GetCursorPos&#8221; metodu mouseun koordinatlar\u0131n\u0131 bizlere ula\u015ft\u0131rmaktayken, &#8220;gdi32.dll&#8221; k\u00fct\u00fcphanesinden extern edilen &#8220;BitBlt&#8221; metodu ise mouseun o anki bulundu\u011fu pixelle ilgili gerekli bilgileri edindirmektedir. &#8220;GetColorAt&#8221; metodu ise o anki piksele harici metotlar\u0131m\u0131z arac\u0131l\u0131\u011f\u0131yla eri\u015fip istenilen renk kodlar\u0131n\u0131 elde etmekte ve Color tipinden geriye return etmektedir.<\/p>\n<p>\u0130\u015fte bu i\u015flemlerden sonra a\u015fa\u011f\u0131daki gibi sonsuz bir d\u00f6ng\u00fcyle tetiklenen asenkron temelli yakla\u015f\u0131m sayesinde videodan da g\u00f6rece\u011finiz gibi ekrandaki mouse ile gezilen noktalardaki pixel de\u011ferlerini elde edebilirsiniz.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        private async void Form1_Load(object sender, EventArgs e)\r\n        {\r\n            await Task.Run(() =&gt;\r\n            {\r\n                while (true)\r\n                {\r\n                    Point cursor = new Point();\r\n                    GetCursorPos(ref cursor);\r\n\r\n                    var c = GetColorAt(cursor);\r\n                    this.BackColor = c;\r\n                }\r\n            });\r\n        }\r\n<\/pre>\n<p><iframe loading=\"lazy\" width=\"350\" height=\"197\" src=\"https:\/\/www.youtube.com\/embed\/klHi6Uyf-SI\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<p>Sonraki yaz\u0131lar\u0131mda g\u00f6r\u00fc\u015fmek \u00fczere&#8230;<br \/>\n\u0130yi \u00e7al\u0131\u015fmalar&#8230;<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Merhaba, Win32 k\u00fct\u00fcphaneleri ile yap\u0131sal olarak her ne kadar i\u015fletim sisteminin nimetlerini kullanarak program\u0131n haricine m\u00fcdahale edebiliyor olsakta, asl\u0131nda renksel baz\u0131 \u00e7al\u0131\u015fmalarda algoritmik olarak ekran\u0131n hangi lokasyonunda oldu\u011fumuzu bulmam\u0131z\u0131 sa\u011flayan yahut renk yo\u011funlu\u011funa g\u00f6re&#46;&#46;&#46;<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":3014,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,1856],"tags":[1862,1860,1858,1722,1861,1857,1859],"class_list":["post-6588","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-sharp-c","category-win32","tag-bitblt","tag-c-mouse-pixel-color","tag-gdi32-dll","tag-getcursorpos","tag-mouse-pixel-color","tag-user32-dll","tag-win32"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/6588","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/comments?post=6588"}],"version-history":[{"count":0,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/6588\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/3014"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=6588"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=6588"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=6588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}