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.Drawing2D;
using System.Drawing.Imaging;
 
namespace Kresleni
{
    enum ToolIndex
    {
        lineIndex,
        rectangleIndex,
        ellipseIndex
    };
 
    public partial class Window : Form
    {
        public Window()
        {
            InitializeComponent();
            pictureBox1_SizeChanged (null, null);
            newColorPanel(Color.Green);
            newColorPanel(Color.Blue);
            p = new Pen(colorPanel.BackColor);
            b = new SolidBrush (Color.Green);
            comboBox1.SelectedIndex = 0;
        }
 
        private void pictureBox1_SizeChanged(object sender, EventArgs e)
        {
            int w = pictureBox1.Width;
            int h = pictureBox1.Height;
            if (w < 1) w = 1;
            if (h < 1) h = 1;
            Image old = pictureBox1.Image;
            if (old != null)
            {
                if (old.Width > w) w = old.Width;
                if (old.Height > h) h = old.Height;
            }
            pictureBox1.Image = new Bitmap(w, h);
            Graphics g = Graphics.FromImage(pictureBox1.Image);
            Brush br = new SolidBrush(Color.White);
            g.FillRectangle(br, 0, 0, w, h);
            if (old != null)
                g.DrawImage (old, 0, 0);
        }
        private void openMenu_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.Image = new Bitmap (openFileDialog1.FileName);
                pictureBox1_SizeChanged (null, null);
 
            }
        }
 
        private void saveMenu_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                // using System.Drawing.Imaging;
                // saveFileDialog1.Filter = "bitmap |*.bmp| jpeg |*.jpg";
                int inx = saveFileDialog1.FilterIndex;
                ImageFormat fmt = ImageFormat.Bmp;
                if (inx == 2) fmt = ImageFormat.Jpeg;
                pictureBox1.Image.Save (saveFileDialog1.FileName, fmt);
            }
 
        }
        private void copyMenu_Click(object sender, EventArgs e)
        {
            Clipboard.SetImage(pictureBox1.Image);
        }
 
        private void pasteMenu_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = Clipboard.GetImage();
            pictureBox1_SizeChanged (null, null);
        }
 
 
        private void quitMenu_Click(object sender, EventArgs e)
        {
            Close();
        }
 
        int cnt = 1;
 
        private void newColorPanel(Color c)
        {
            Panel p = new Panel();
            p.Left = colorPanel.Left +
                     cnt * (colorPanel.Width + colorPanel.Left);
            cnt++;
            p.Top = colorPanel.Top;
            p.Width = colorPanel.Width;
            p.Height = colorPanel.Height;
            p.Parent = toolPanel;
            p.BackColor = c;
            p.MouseDown += colorPanel_MouseDown;
        }
 
        private void newColorMenu_Click(object sender, EventArgs e)
        {
            newColorPanel (Color.Yellow);
        }
 
        Pen p;
        Brush b;
 
        private void colorPanel_MouseDown(object sender, MouseEventArgs e)
        {
            Panel area = (Panel) sender;
            if (e.Button == MouseButtons.Left)
                p = new Pen(area.BackColor);
            else if (e.Button == MouseButtons.Right)
                b = new SolidBrush (area.BackColor);
            else if (e.Button == MouseButtons.Middle)
            {
                colorDialog1.Color = area.BackColor;
                if (colorDialog1.ShowDialog() == DialogResult.OK)
                {
                    area.BackColor = colorDialog1.Color;
                    p = new Pen (area.BackColor);
                }
            }
        }
 
        int x0, y0;
        bool down = false;
        Image orig;
 
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            x0 = e.X;
            y0 = e.Y;
            down = true;
            orig = new Bitmap (pictureBox1.Image);
        }
 
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (down)
            {
                Graphics g = Graphics.FromImage(pictureBox1.Image);
                g.DrawImage (orig, 0, 0);
 
                ToolIndex t = (ToolIndex) comboBox1.SelectedIndex;
 
                if (t == ToolIndex.lineIndex)
                    g.DrawLine (p, x0, y0, e.X, e.Y);
                else if (t == ToolIndex.rectangleIndex)
                {
                    int x1 = x0;
                    int y1 = y0;
                    int x2 = e.X;
                    int y2 = e.Y;
                    if (x2 < x1) { int v = x1; x1 = x2; x2 = v; }
                    if (y2 < y1) { int v = y1; y1 = y2; y2 = v; }
                    g.FillRectangle (b, x1, y1, x2 - x1, y2 - y1);
                    g.DrawRectangle (p, x1, y1, x2 - x1, y2 - y1);
                }
                else if (t == ToolIndex.ellipseIndex)
                {
                    g.FillEllipse (b, x0, y0, e.X - x0, e.Y - y0);
                    g.DrawEllipse (p, x0, y0, e.X - x0, e.Y - y0);
                }
            }
            pictureBox1.Invalidate();
        }
 
        // using System.Drawing.Drawing2D;
 
        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            down = false;
        }
    }
}
 
kresleni2017.txt · Last modified: 2017/10/23 16:40 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