Kompletni prikladv ZIP souboru: http://kmlinux.fjfi.cvut.cz/~culik/pw/Kresleni-2019-11-04.zip

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
 
namespace Kresleni
{
    public partial class Form1 : Form
    {
        Pen pen;
        Brush brush;
 
        const int line = 0;
        const int rectangle = 1;
        const int ellipse = 2;
 
        public Form1()
        {
            InitializeComponent();
            PictureBox1_SizeChanged(null, null);
 
            pen = new Pen(Color.Red);
            pen.Width = 5;
 
            brush = new SolidBrush(Color.Yellow);
 
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBox1.SelectedIndex = line;
 
            Graphics g = Graphics.FromImage(pictureBox1.Image);
            g.DrawLine(pen, 0, 0, 100, 100);
            pictureBox1.Invalidate();
 
            Color[] colors = { Color.Orange, Color.CornflowerBlue, Color.Lime };
 
            int x = colorPanel.Left;
            foreach (Color c in colors)
            {
                x = x + colorPanel.Left + colorPanel.Width;
                Panel p = new Panel ();
                p.Left = x;
                p.Top = colorPanel.Top;
                p.Width = colorPanel.Width;
                p.Height = colorPanel.Height;
                p.BackColor = c;
                p.MouseDown += this.Panel1_MouseDown;
                p.Parent = toolPanel;
            }
 
        }
 
        private void PictureBox1_SizeChanged(object sender, EventArgs e)
        {
            Bitmap old = null;
            if (pictureBox1.Image != null)
               old = new Bitmap (pictureBox1.Image);
 
            int w = pictureBox1.Width;
            int h = pictureBox1.Height;
            if (old != null)
            {
                if (old.Width > w) w = old.Width;
                if (old.Height > h) h = old.Height;
            }
 
            Bitmap b = new Bitmap (w, h);
            pictureBox1.Image = b;
            Graphics g = Graphics.FromImage(pictureBox1.Image);
            Brush brush = new SolidBrush(Color.White);
            g.FillRectangle(brush, 0, 0, w, h);
 
            if (old != null)
               g.DrawImage (old, 0, 0);
 
            pictureBox1.Invalidate();
        }
 
        private int X0, Y0;
        private bool press = false;
        private Image save;
 
        private void PictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            X0 = e.X;
            Y0 = e.Y;
            press = true;
            save = new Bitmap (pictureBox1.Image);
        }
 
        private void PictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (press)
            {
                Graphics g = Graphics.FromImage(pictureBox1.Image);
                g.DrawImage (save, 0, 0);
 
                int inx = comboBox1.SelectedIndex;
                if (inx == line)
                {
                    g.DrawLine(pen, X0, Y0, e.X, e.Y);
                }
                else if (inx == rectangle)
                {
                    int X1 = X0;
                    int Y1 = Y0;
                    int X2 = e.X;
                    int Y2 = e.Y;
                    if (X2 < X1) { int t = X1; X1 = X2; X2 = t; }
                    if (Y2 < Y1) { int t = Y1; Y1 = Y2; Y2 = t; }
                    g.FillRectangle(brush, X1, Y1, X2 - X1, Y2 - Y1);
                    g.DrawRectangle(pen, X1, Y1, X2 - X1, Y2 - Y1);
                }
                else if (inx == ellipse)
                {
                    g.FillEllipse(brush, X0, Y0, e.X - X0, e.Y - Y0);
                    g.DrawEllipse(pen, X0, Y0, e.X - X0, e.Y - Y0);
                }
                pictureBox1.Invalidate();
            }
        }
 
        private void PictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            press = false;
        }
 
        private void OpenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.Image = new Bitmap (openFileDialog1.FileName);
                PictureBox1_SizeChanged(null, null);
            }
        }
 
        private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // using System.Drawing.Imaging;
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                pictureBox1.Image.Save (saveFileDialog1.FileName, ImageFormat.Jpeg);
        }
 
        private void Panel1_MouseDown(object sender, MouseEventArgs e)
        {
            Panel panel = sender as Panel;
            if (e.Button == MouseButtons.Left &&
                Control.ModifierKeys != Keys.Control)
            {
                pen = new Pen (panel.BackColor);
                pen.Width = 5;
            }
            else if (e.Button == MouseButtons.Right)
            {
                if (Control.ModifierKeys != Keys.Control)
                    brush = new SolidBrush(panel.BackColor);
                else
                    // using System.Drawing.Drawing2D;
                    brush = new LinearGradientBrush
                            (new Point(0, 0),
                             new PointF (100, 100),
                             pen.Color,
                             panel.BackColor);
            }
            else if (e.Button == MouseButtons.Middle ||
                     Control.ModifierKeys == Keys.Control)
            {
                colorDialog1.Color = panel.BackColor;
                if (colorDialog1.ShowDialog () == DialogResult.OK)
                {
                    panel.BackColor = colorDialog1.Color;
                    pen = new Pen (panel.BackColor);
                    pen.Width = 5;
                }
            }
        }
 
        private void QuitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }
 
    }
}
 
kresleni2019.txt · Last modified: 2019/11/04 19:12 by 147.32.8.115
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki