Advertisement
Guest User

Aula de Wanfranklin

a guest
Apr 6th, 2020
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11.  
  12. namespace Pintar
  13. {
  14.     enum Ferramentas
  15.         {
  16.                 caneta, baldeDeTinta
  17.         }
  18.     public partial class FrmPrincipal : Form
  19.     {
  20.         private Ferramentas ferramentas = Ferramentas.caneta;
  21.         private Color colorA = Color.Black;
  22.         private int prevX;
  23.         private int prevY;
  24.         private bool canDesenhar = false;
  25.         private bool canUsarBarraFerramentas = true;
  26.         private Thread thread = null;
  27.        
  28.         public FrmPrincipal()
  29.         {
  30.             InitializeComponent();
  31.             pbTinta.Image = CriarBmpVazio(Color.White, panel1.Width, panel1.Height);
  32.             pbDesenho.Image = CriarBmpVazio(colorA, pbDesenho.Width, pbDesenho.Height);
  33.             timer1.Start();
  34.         }
  35.  
  36.         private void UIThread(MethodInvoker codigo) {
  37.             if (this.InvokeRequired)
  38.             {
  39.                 this.Invoke(codigo);
  40.             }
  41.             else {
  42.                 codigo.Invoke();
  43.             }
  44.         }
  45.         private Bitmap CriarBmpVazio(Color c, int width, int height) {
  46.             Bitmap bmp = new Bitmap(width, height);
  47.  
  48.             using (var g = Graphics.FromImage(bmp)) {
  49.                 g.Clear(c);
  50.             }
  51.  
  52.             return bmp;
  53.         }
  54.  
  55.         private Bitmap Caneta(Bitmap bmp, MouseEventArgs e) {
  56.             using (var caneta = new Pen(colorA, 1)) {
  57.                 using (var g = Graphics.FromImage(bmp)) {
  58.                     g.DrawLine(caneta, prevX, prevY, e.X, e.Y);
  59.                 }
  60.             }
  61.  
  62.             return bmp;
  63.         }
  64.  
  65.         private Bitmap BaldeDeTinta(Bitmap bmp, Color pC, Point point)
  66.         {
  67.             Stack<Point> stack = new Stack<Point>();
  68.             stack.Push(point);
  69.  
  70.             while (stack.Count > 0)
  71.             {
  72.                 Point p = stack.Pop();
  73.  
  74.                 if (p.X >= 0 && p.X < bmp.Width && p.Y >= 0 && p.Y < bmp.Height)
  75.                 {
  76.                     if (bmp.GetPixel(p.X, p.Y) != pC)
  77.                         continue;
  78.  
  79.                     bmp.SetPixel(p.X, p.Y, colorA);
  80.                     stack.Push(new Point(p.X + 1, p.Y));
  81.                     stack.Push(new Point(p.X - 1, p.Y));
  82.                     stack.Push(new Point(p.X, p.Y + 1));
  83.                     stack.Push(new Point(p.X, p.Y - 1));
  84.                 }
  85.             }
  86.             return bmp;
  87.         }
  88.  
  89.         private void pbDesenho_MouseDown(object sender, MouseEventArgs e)
  90.         {
  91.             prevX = e.X;
  92.             prevY = e.Y;
  93.             canDesenhar = true;
  94.  
  95.             switch (ferramentas) {
  96.                 case Ferramentas.baldeDeTinta:
  97.                     thread = new Thread(() => {
  98.                         UIThread(delegate { flowLayoutPanel1.Enabled = false; });
  99.                         Bitmap b = pbDesenho.Image as Bitmap;
  100.                         Color c = b.GetPixel(e.X, e.Y);
  101.  
  102.                         UIThread(delegate {
  103.                             pbDesenho.Image = BaldeDeTinta(b, c, new Point(e.X, e.Y));
  104.                             flowLayoutPanel1.Enabled = true;
  105.                         });
  106.                     });
  107.  
  108.                     thread.Start();
  109.                 break;
  110.             }
  111.         }
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement