Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace Pintar
- {
- enum Ferramentas
- {
- caneta, baldeDeTinta
- }
- public partial class FrmPrincipal : Form
- {
- private Ferramentas ferramentas = Ferramentas.caneta;
- private Color colorA = Color.Black;
- private int prevX;
- private int prevY;
- private bool canDesenhar = false;
- private bool canUsarBarraFerramentas = true;
- private Thread thread = null;
- public FrmPrincipal()
- {
- InitializeComponent();
- pbTinta.Image = CriarBmpVazio(Color.White, panel1.Width, panel1.Height);
- pbDesenho.Image = CriarBmpVazio(colorA, pbDesenho.Width, pbDesenho.Height);
- timer1.Start();
- }
- private void UIThread(MethodInvoker codigo) {
- if (this.InvokeRequired)
- {
- this.Invoke(codigo);
- }
- else {
- codigo.Invoke();
- }
- }
- private Bitmap CriarBmpVazio(Color c, int width, int height) {
- Bitmap bmp = new Bitmap(width, height);
- using (var g = Graphics.FromImage(bmp)) {
- g.Clear(c);
- }
- return bmp;
- }
- private Bitmap Caneta(Bitmap bmp, MouseEventArgs e) {
- using (var caneta = new Pen(colorA, 1)) {
- using (var g = Graphics.FromImage(bmp)) {
- g.DrawLine(caneta, prevX, prevY, e.X, e.Y);
- }
- }
- return bmp;
- }
- private Bitmap BaldeDeTinta(Bitmap bmp, Color pC, Point point)
- {
- Stack<Point> stack = new Stack<Point>();
- stack.Push(point);
- while (stack.Count > 0)
- {
- Point p = stack.Pop();
- if (p.X >= 0 && p.X < bmp.Width && p.Y >= 0 && p.Y < bmp.Height)
- {
- if (bmp.GetPixel(p.X, p.Y) != pC)
- continue;
- bmp.SetPixel(p.X, p.Y, colorA);
- stack.Push(new Point(p.X + 1, p.Y));
- stack.Push(new Point(p.X - 1, p.Y));
- stack.Push(new Point(p.X, p.Y + 1));
- stack.Push(new Point(p.X, p.Y - 1));
- }
- }
- return bmp;
- }
- private void pbDesenho_MouseDown(object sender, MouseEventArgs e)
- {
- prevX = e.X;
- prevY = e.Y;
- canDesenhar = true;
- switch (ferramentas) {
- case Ferramentas.baldeDeTinta:
- thread = new Thread(() => {
- UIThread(delegate { flowLayoutPanel1.Enabled = false; });
- Bitmap b = pbDesenho.Image as Bitmap;
- Color c = b.GetPixel(e.X, e.Y);
- UIThread(delegate {
- pbDesenho.Image = BaldeDeTinta(b, c, new Point(e.X, e.Y));
- flowLayoutPanel1.Enabled = true;
- });
- });
- thread.Start();
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement