﻿
{"id":5536,"date":"2017-01-16T07:03:05","date_gmt":"2017-01-16T07:03:05","guid":{"rendered":"http:\/\/www.gencayyildiz.com\/blog\/?p=5536"},"modified":"2017-01-16T07:03:05","modified_gmt":"2017-01-16T07:03:05","slug":"c-windows-form-uygulamasinda-paint-yapimi","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/c-windows-form-uygulamasinda-paint-yapimi\/","title":{"rendered":"C# Windows Form Uygulamas\u0131nda Paint Yap\u0131m\u0131"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p>Bu makalemizde C# Windows Form uygulamas\u0131nda \u00e7izgi \u00e7izmeyi irdeleyecek ve \u00f6rnek bir Paint uygulamas\u0131 \u00fczerinde i\u00e7eri\u011fimizi kuvvetlendirece\u011fiz. Windows Form uygulamalar\u0131nda forma \u00e7izgi \u00e7izebilmek i\u00e7in k\u00e2h Graphics s\u0131n\u0131f\u0131 \u00fczerinden Draw ile ba\u015fayan metodlar\u0131 kullanabilir k\u00e2h tek tek pixeller \u00fczerinde i\u015flemler yapabiliriz. Tabi burada ilk verdi\u011fim Graphics s\u0131n\u0131f\u0131 kullan\u0131larak \u00e7izilen \u00e7izgiler genellikle \u015fekillerden olu\u015fan yap\u0131lar\u0131 bar\u0131nd\u0131rmaktayken, tek tek pixellere m\u00fcdahale ederek \u00e7izdirdiklerimiz kullan\u0131c\u0131n\u0131n mouse ile olan insiyatifine ba\u011fl\u0131 tarifsiz \u00e7izgileri ifade etmektedir.<\/p>\n<p>Biz bu i\u00e7eri\u011fimizde ikinci \u00f6neri olan pixellere m\u00fcdahale y\u00f6ntemiyle \u00e7izim yapmay\u0131 inceleyece\u011fiz.<\/p>\n<p>Hemen konuya giri\u015f yapabilmek maksad\u0131yla a\u015fa\u011f\u0131daki gibi bir form tasarlayal\u0131m.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    partial class Form1\r\n    {\r\n        private System.ComponentModel.IContainer components = null;\r\n        \r\n        protected override void Dispose(bool disposing)\r\n        {\r\n            if (disposing &amp;&amp; (components != null))\r\n            {\r\n                components.Dispose();\r\n            }\r\n            base.Dispose(disposing);\r\n        }\r\n\r\n        #region Windows Form Designer generated code\r\n        \r\n        private void InitializeComponent()\r\n        {\r\n            this.txt1 = new System.Windows.Forms.TextBox();\r\n            this.btnRenkSec = new System.Windows.Forms.Button();\r\n            this.cdRenk = new System.Windows.Forms.ColorDialog();\r\n            this.SuspendLayout();\r\n            \/\/ \r\n            \/\/ txt1\r\n            \/\/ \r\n            this.txt1.Location = new System.Drawing.Point(12, 12);\r\n            this.txt1.Name = &quot;txt1&quot;;\r\n            this.txt1.Size = new System.Drawing.Size(100, 20);\r\n            this.txt1.TabIndex = 0;\r\n            this.txt1.TextChanged += new System.EventHandler(this.txt1_TextChanged);\r\n            \/\/ \r\n            \/\/ btnRenkSec\r\n            \/\/ \r\n            this.btnRenkSec.Location = new System.Drawing.Point(118, 9);\r\n            this.btnRenkSec.Name = &quot;btnRenkSec&quot;;\r\n            this.btnRenkSec.Size = new System.Drawing.Size(75, 23);\r\n            this.btnRenkSec.TabIndex = 2;\r\n            this.btnRenkSec.Text = &quot;Renk Se\u00e7&quot;;\r\n            this.btnRenkSec.UseVisualStyleBackColor = true;\r\n            this.btnRenkSec.Click += new System.EventHandler(this.btnRenkSec_Click);\r\n            \/\/ \r\n            \/\/ Form1\r\n            \/\/ \r\n            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);\r\n            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;\r\n            this.BackColor = System.Drawing.SystemColors.ButtonHighlight;\r\n            this.ClientSize = new System.Drawing.Size(737, 512);\r\n            this.Controls.Add(this.btnRenkSec);\r\n            this.Controls.Add(this.txt1);\r\n            this.Name = &quot;Form1&quot;;\r\n            this.Text = &quot;Paint&quot;;\r\n            this.Load += new System.EventHandler(this.Form1_Load);\r\n            this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);\r\n            this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);\r\n            this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);\r\n            this.ResumeLayout(false);\r\n            this.PerformLayout();\r\n\r\n        }\r\n\r\n        #endregion\r\n\r\n        private System.Windows.Forms.TextBox txt1;\r\n        private System.Windows.Forms.Button btnRenkSec;\r\n        private System.Windows.Forms.ColorDialog cdRenk;\r\n    }\r\n<\/pre>\n<p>Formumuzun ekran g\u00f6r\u00fcnt\u00fcs\u00fc a\u015fa\u011f\u0131daki gibidir.<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/01\/C-Windows-Form-Uygulamas\u0131nda-Paint-Yap\u0131m\u0131.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/01\/C-Windows-Form-Uygulamas\u0131nda-Paint-Yap\u0131m\u0131.png\" alt=\"C# Windows Form Uygulamas\u0131nda Paint Yap\u0131m\u0131\" width=\"381\" height=\"252\" class=\"aligncenter size-full wp-image-5538\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/01\/C-Windows-Form-Uygulamas\u0131nda-Paint-Yap\u0131m\u0131.png 381w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/01\/C-Windows-Form-Uygulamas\u0131nda-Paint-Yap\u0131m\u0131-300x198.png 300w\" sizes=\"auto, (max-width: 381px) 100vw, 381px\" \/><\/a><\/p>\n<p>\u00c7al\u0131\u015fma yapaca\u011f\u0131m\u0131z forma \u015f\u00f6yle bir bakt\u0131\u011f\u0131m\u0131zda birer adet TextBox, Button ve ColorDialog nesneleri mevcut. Burada TextBox&#8217;a girilecek de\u011fer forma \u00e7izece\u011fimiz \u00e7izginin kal\u0131nl\u0131\u011f\u0131n\u0131 belirtirken, Button nesnesi ile de ColorDialog nesnesi g\u00f6sterilmekte ve \u00e7izginin rengi belirlenmektedir.<\/p>\n<p>Evet \u015fimdi \u00e7izim i\u015flemimize ge\u00e7ebiliriz. Burada yapaca\u011f\u0131m\u0131z i\u015flem, form ekran\u0131nda mouse t\u0131kland\u0131\u011f\u0131 noktadan ba\u015flayarak, s\u00fcr\u00fcklendi\u011fi t\u00fcm koordinatlarla birlikte ta ki b\u0131rak\u0131ld\u0131\u011f\u0131 noktaya kadar olan t\u00fcm pixcellerin rengini ColorDialog ile belirledi\u011fimiz renge boyamakt\u0131r. Bu i\u015flem i\u00e7in formun t\u0131kland\u0131\u011f\u0131 koordinat\u0131 yakalayabilmek i\u00e7in &#8220;<strong><em>MouseDown<\/em><\/strong>&#8220;, s\u00fcr\u00fcklendi\u011fi an\u0131 yakalayabilmek i\u00e7in &#8220;<strong><em>MouseMove<\/em><\/strong>&#8221; ve b\u0131rak\u0131ld\u0131\u011f\u0131 koordinata eri\u015febilmek i\u00e7in ise &#8220;<strong><em>MouseUp<\/em><\/strong>&#8221; olaylar\u0131n\u0131 kullanaca\u011f\u0131z.<\/p>\n<p>Tabi \u00f6ncelikle \u00e7izim yapacak i\u015flevlerimizi olu\u015fturmam\u0131z gerekecektir.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        private void Form1_Load(object sender, EventArgs e) =&gt; g = this.CreateGraphics();\r\n        Graphics g;\r\n        Bitmap b = new Bitmap(5, 5);\r\n        bool Durum;\r\n        int x = 5;\r\n        private void DrawLine(Graphics g, Point location, Color color)\r\n        {\r\n            if (Durum)\r\n            {\r\n                for (int i = 0; i &lt; x; i++)\r\n                    for (int j = 0; j &lt; x; j++)\r\n                        b.SetPixel(i, j, color);\r\n                g.DrawImageUnscaled(b, location.X, location.Y);\r\n            }\r\n        }\r\n<\/pre>\n<p>Yukar\u0131daki kod blo\u011funu incelerseniz e\u011fer bir Graphics referans\u0131 olu\u015fturmakta ve bu referansa Form_Load event\u0131nda this.CreateGraphics() komutuyla o anki form nesnesi \u00fczerinden bir grafik olu\u015fturup vermekteyiz. Ayriyetten Bitmap nesnesi olu\u015fturup varsay\u0131lan de\u011fer olarak 5&#8217;e 5&#8217;lik bir pixel de\u011ferinde ayarlamaktay\u0131z. Olu\u015fturdu\u011fumuz bool tipteki Durum de\u011fi\u015fkeni ise mouseun o anda t\u0131klayarak hareket edip etmedi\u011fi \u00fczerinde bize bilgi verecektir. DrawLine metodu ise Bitmap i\u00e7erisindeki pixelleri tek tek verilen color tipinde dolduracakt\u0131r. Son olarakta g.DrawImageUnscaled(b, location.X, location.Y); komutu arac\u0131l\u0131\u011f\u0131yla ilgili \u00e7izimi forma yans\u0131tacakt\u0131r.<\/p>\n<p>\u015eimdi ise olaylar\u0131m\u0131z\u0131 a\u015fa\u011f\u0131ya alal\u0131m.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        private void Form1_MouseMove(object sender, MouseEventArgs e)\r\n        {\r\n            if (e.Button == MouseButtons.Left)\r\n                DrawLine(g, e.Location, Renk);\r\n        }\r\n\r\n        private void Form1_MouseUp(object sender, MouseEventArgs e) =&gt; Durum = false;\r\n\r\n        private void Form1_MouseDown(object sender, MouseEventArgs e)\r\n        {\r\n            if (e.Button == MouseButtons.Left)\r\n            {\r\n                Durum = true;\r\n                DrawLine(g, e.Location, Renk);\r\n            }\r\n        }\r\n<\/pre>\n<p>Burada pek fazla a\u00e7\u0131klayama de\u011fer bir yap\u0131 g\u00f6remiyorum. G\u00f6r\u00fcld\u00fc\u011f\u00fc \u00fczere mouse hareketlerine g\u00f6re \u00e7izim ger\u00e7ekle\u015ftirilmektedir.<\/p>\n<p>Son olarak \u00e7izgimizin rengi ve kal\u0131nl\u0131\u011f\u0131n\u0131 belirleyecek yap\u0131lar\u0131m\u0131z\u0131 olu\u015ftural\u0131m.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        Color Renk = Color.Black;\r\n        private void btnRenkSec_Click(object sender, EventArgs e)\r\n        {\r\n            if (cdRenk.ShowDialog() == DialogResult.OK)\r\n                Renk = cdRenk.Color;\r\n        }\r\n\r\n        private void txt1_TextChanged(object sender, EventArgs e)\r\n        {\r\n            TextBox txt = sender as TextBox;\r\n            x = int.Parse(txt.Text);\r\n\r\n            b = new Bitmap(x, x);\r\n        }\r\n<\/pre>\n<p>Yapt\u0131\u011f\u0131m\u0131z i\u015flemin net g\u00f6r\u00fclebilmesi i\u00e7in t\u00fcm \u00e7al\u0131\u015fmay\u0131 a\u015fa\u011f\u0131da bir b\u00fct\u00fcn olarak payla\u015f\u0131yorum.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    public partial class Form1 : Form\r\n    {\r\n        public Form1()\r\n        {\r\n            InitializeComponent();\r\n        }\r\n        private void Form1_Load(object sender, EventArgs e) =&gt; g = this.CreateGraphics();\r\n        Graphics g;\r\n        Bitmap b = new Bitmap(5, 5);\r\n        bool Durum;\r\n        int x = 5;\r\n        private void DrawLine(Graphics g, Point location, Color color)\r\n        {\r\n            if (Durum)\r\n            {\r\n                for (int i = 0; i &lt; x; i++)\r\n                    for (int j = 0; j &lt; x; j++)\r\n                        b.SetPixel(i, j, color);\r\n                g.DrawImageUnscaled(b, location.X, location.Y);\r\n            }\r\n        }\r\n        private void Form1_MouseMove(object sender, MouseEventArgs e)\r\n        {\r\n            if (e.Button == MouseButtons.Left)\r\n                DrawLine(g, e.Location, Renk);\r\n        }\r\n\r\n        private void Form1_MouseUp(object sender, MouseEventArgs e) =&gt; Durum = false;\r\n\r\n        private void Form1_MouseDown(object sender, MouseEventArgs e)\r\n        {\r\n            if (e.Button == MouseButtons.Left)\r\n            {\r\n                Durum = true;\r\n                DrawLine(g, e.Location, Renk);\r\n            }\r\n        }\r\n        Color Renk = Color.Black;\r\n        private void btnRenkSec_Click(object sender, EventArgs e)\r\n        {\r\n            if (cdRenk.ShowDialog() == DialogResult.OK)\r\n                Renk = cdRenk.Color;\r\n        }\r\n\r\n        private void txt1_TextChanged(object sender, EventArgs e)\r\n        {\r\n            TextBox txt = sender as TextBox;\r\n            x = int.Parse(txt.Text);\r\n            b = new Bitmap(x, x);\r\n        }\r\n    }\r\n<\/pre>\n<p>Yapt\u0131\u011f\u0131m\u0131z bu i\u015flemlerden sonra projemizi derleyip \u00e7al\u0131\u015ft\u0131ral\u0131m.<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/01\/C-Windows-Form-Uygulamas\u0131nda-Paint-Yap\u0131m\u0131-1.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/01\/C-Windows-Form-Uygulamas\u0131nda-Paint-Yap\u0131m\u0131-1.png\" alt=\"C# Windows Form Uygulamas\u0131nda Paint Yap\u0131m\u0131\" width=\"768\" height=\"435\" class=\"aligncenter size-full wp-image-5543\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/01\/C-Windows-Form-Uygulamas\u0131nda-Paint-Yap\u0131m\u0131-1.png 768w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/01\/C-Windows-Form-Uygulamas\u0131nda-Paint-Yap\u0131m\u0131-1-300x170.png 300w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><\/a><\/p>\n<p>G\u00f6rd\u00fc\u011f\u00fcn\u00fcz gibi forma istedi\u011fimiz boyut ve renkte \u00e7izgimizi \u00e7izmi\u015f bulunmaktay\u0131z.<\/p>\n<p>Bu i\u00e7eri\u011fimizinde sonuna gelmi\u015f bulunmaktay\u0131z. Umar\u0131m bol bol faydalan\u0131rs\u0131n\u0131z.<\/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, Bu makalemizde C# Windows Form uygulamas\u0131nda \u00e7izgi \u00e7izmeyi irdeleyecek ve \u00f6rnek bir Paint uygulamas\u0131 \u00fczerinde i\u00e7eri\u011fimizi kuvvetlendirece\u011fiz. Windows Form uygulamalar\u0131nda forma \u00e7izgi \u00e7izebilmek i\u00e7in k\u00e2h Graphics s\u0131n\u0131f\u0131 \u00fczerinden Draw ile ba\u015fayan metodlar\u0131 kullanabilir&#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":5543,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[27,1594,1595,1596,1593,1591,1592],"class_list":["post-5536","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-sharp-c","tag-c","tag-c-paint","tag-cta-cizgi-cizme","tag-graphics","tag-paint","tag-windows-form","tag-windows-forms"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/5536","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=5536"}],"version-history":[{"count":0,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/5536\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/5543"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=5536"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=5536"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=5536"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}