Submission #7486554


Source Code Expand

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
using System.Collections.Generic;


namespace WindowsFormsApp1
{
    public partial class PaintToolControl : Form
    {
        bool MouseTap, MouseDrawed; //trueで描画 _ マウス押下している間はtureとなる
        int startX , startY,imgIndex;
        double SizeCT ;
        string FilePath;
        int  pictureBoxX, pictureBoxY;
        String fn;
        List<Image> PaintList = new List<Image>();
        bool SaveFlg = false;
        Bitmap PaintCanvas;
        Graphics grfx;
        Brush brush;
        Color color;
        Bitmap BoxResize;

        public PaintToolControl()
        {
            InitializeComponent();
        }
        
        private void Form1_Load(object sender, EventArgs e)
        {
            color = Color.Black;
            brush = Brushes.Black;
            SaveButton.Visible = false;
            PenSizeBox.Text = "6pt";


            ImageBox.Location = new Point(0, 0);
            PaintBox.Location = new Point(0, 0);

            ImageBox.Parent = BasePanel;
            PaintBox.Parent = ImageBox;


            PaintBox.MouseDown += new System.Windows.Forms.MouseEventHandler(this.PaintBox_MouseDown); //
            PaintBox.MouseUp += new System.Windows.Forms.MouseEventHandler(this.PaintBox_MouseUp); //
            PaintBox.MouseMove += new System.Windows.Forms.MouseEventHandler(this.PaintBox_MouseMove); //

        }

        private void Preparation()
        {
            MouseTap = false;
            MouseDrawed = false;
            imgIndex = 0;
            
            Bitmap tmp = new Bitmap(PaintBox.Width, PaintBox.Height);
            PaintBox.Image = tmp;
            
            tmp.MakeTransparent(Color.White);
            PaintBox.BackColor = Color.Transparent;

        }


        private void OpenButton_Click(object sender, EventArgs e) //参照ボタン処理--------------------------------------------------------------------
        {
            try
            {
                if (SaveFlg == false && ImageBox.Image != null && imgIndex != 0)
                {
                    DialogResult OfdMsg = MessageBox.Show("画像への描画は保存されていません。よろしいですか。", "警告", MessageBoxButtons.YesNo);
                    if (OfdMsg == DialogResult.No)
                    {
                        return;
                    }
                }

                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "画像ファイル|*.gif;*.jpg;*.png|全てのファイル|*.*";
                ofd.Title = "ファイルを開く";
                DialogResult result = ofd.ShowDialog();

                if (ImageBox.Image == null) Preparation();

                if (result == DialogResult.OK)
                {
                    FilePath = ofd.FileName;

                    BasePanelFunc(Image.FromFile(FilePath));
                    PaintListReset();
                    ImageBox.Image = Image.FromFile(FilePath);
                    SaveButton.Visible = true;
                    fn = ofd.FileName;


                    //PaintBox.Imageサイズ変更                
                    BoxResize = new Bitmap(PaintBox.Image, ImageBox.Image.Width, ImageBox.Image.Height);
                    PaintBox.Image = BoxResize;
                    PaintList.Add(PaintBox.Image);

                    //ファイル情報出力機能
                    String FileNameInfo = "ファイル名 : ", FileBirthInfo = "ファイル作成日 : ", FileUpDayInfo = "ファイル更新日 : ";
                    FileNameInfo += System.IO.Path.GetFileName(ofd.FileName);
                    FileBirthInfo += System.IO.File.GetCreationTime(ofd.FileName);
                    FileUpDayInfo += System.IO.File.GetLastWriteTime(ofd.FileName);
                    FileDataOutputLabel.Text = FileNameInfo + Environment.NewLine + Environment.NewLine + FileBirthInfo + "     " + FileUpDayInfo;

                    //画像サイズ変更処理時使用----------------------------------------------------------------------                
                    pictureBoxX = ImageBox.Image.Width;
                    pictureBoxY = ImageBox.Image.Height;

                    ImageBairitsuInfo.Text = "x1";
                    imgIndex = 0;
                    SizeCT = 1;
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Error");
                if (PaintList.Count == 0)
                {
                    BoxResize = new Bitmap(PaintBox.Image, ImageBox.Image.Width, ImageBox.Image.Height);
                    PaintBox.Image = BoxResize;
                    PaintList.Add(PaintBox.Image);
                    OpenButton.PerformClick();
                }
            }

        }

        private void PaintListReset()//Panel設定-----------------------
        {
            if (ImageBox.Image == null) return;
            PaintBox.Image = PaintList[0];
            PaintList = new List<Image>();
            PaintList.Add(PaintBox.Image);
            PaintBox.Image = PaintList[0];
        }



            private void BasePanelFunc(Image FileImageInfo)//Panel設定-----------------------
        {
            BasePanel.Controls.Add(PaintBox);
            BasePanel.Controls.Add(ImageBox);

            ImageBox.Parent = BasePanel;
            PaintBox.Parent = ImageBox;

            int I_FileWidth = FileImageInfo.Width;
            int I_FileHeight = FileImageInfo.Height;

            BasePanel.AutoScroll = true;

            PaintBox.Width = ImageBox.Width;
            PaintBox.Height = ImageBox.Height;

        }
               

        private void SaveButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (imgIndex == 0)
                {
                    MessageBox.Show("線を描画してください。", "警告");
                    return;
                }

                //保存処理-----------------------------------------------------------------------------------------------------------------------------------------------------
                SaveFileDialog sfd = new SaveFileDialog();

                sfd.Title = "画像ファイルの選択";
                //sfd.Filter = "Image Files|*.jpg";
                sfd.FileName = "新しいファイル.jpg";

                //Bitmap baseImage = new Bitmap(pictureBox1.Image);
                Bitmap baseImage = new Bitmap(ImageBox.Image);
                Bitmap paintImage = new Bitmap(PaintBox.Image);
                Bitmap newImage = new Bitmap(baseImage);

                Graphics g = Graphics.FromImage(newImage);
                g.DrawImage(paintImage, 0, 0, paintImage.Width, paintImage.Height);

                g.Dispose();

                baseImage.Dispose();
                paintImage.Dispose();

                //タッチキーボード

                using (Process process = new Process())
                {
                    //VisualStudio上で、事前にプロジェクト_プロパティ_ビルド_「32bitを優先」のチェックを外す
                    process.StartInfo.FileName = "osk.exe";
                    process.Start();
                }

                //SaveFileDialogキャンセル時
                if (sfd.ShowDialog() == DialogResult.Cancel) return;

                //保存処理
                newImage.Save(sfd.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                newImage.Dispose();

                SaveFlg = true;
            }
            catch (Exception)
            {
                MessageBox.Show("Error");
            }
        }

        private void CloseButton_Click(object sender, EventArgs e)
        {
            if (ImageBox.Image != null && SaveFlg == false)
            {
                DialogResult MsgRes = MessageBox.Show("画像への編集は保存されていません。よろしですか。","警告",MessageBoxButtons.YesNo);
                if (MsgRes == DialogResult.No) return;
            }

            this.Close();
        }
        
    
        private void PenButton_Click(object sender, EventArgs e)
        {
            color = Color.Black;
            brush = Brushes.Black;
        }

        private void EraserButton_Click(object sender, EventArgs e)
        {
            color = Color.Transparent;
            brush = Brushes.Transparent;
        }

        private void BlackColorPenButton_Click(object sender, EventArgs e)
        {
            color = Color.Black;
            brush = Brushes.Black;
        }

        private void WhiteColorPenButton_Click(object sender, EventArgs e)
        {
            color = Color.White;
            brush = Brushes.White;
        }

        private void GrayColorPenButton_Click(object sender, EventArgs e)
        {
            color = Color.Gray;
            brush = Brushes.Gray;
        }

        private void LimeColorPenButton_Click(object sender, EventArgs e)
        {
            color = Color.Lime;
            brush = Brushes.Lime;
        }

        private void YellowColorPenButton_Click(object sender, EventArgs e)
        {
            color = Color.Yellow;
            brush = Brushes.Yellow;
        }

        private void RedButton_Click(object sender, EventArgs e)
        {
            color = Color.Red;
            brush = Brushes.Red;
        }

        private void AquaColorPenButton_Click(object sender, EventArgs e)
        {
            color = Color.Aqua;
            brush = Brushes.Aqua;
        }

        private void FuchsiaColorPenButton_Click(object sender, EventArgs e)
        {
            color = Color.Fuchsia;
            brush = Brushes.Fuchsia;
        }

        private void CoralColorPenButton_Click(object sender, EventArgs e)
        {
            color = Color.Coral;
            brush = Brushes.Coral;
        }

        private void BlueColorPenButton_Click(object sender, EventArgs e)
        {
            color = Color.Blue;
            brush = Brushes.Blue;
        }

        private void ClearButton_Click(object sender, EventArgs e) //描画リセット処理
        {
            if (ImageBox.Image == null) return;
            PaintBox.Image = PaintList[0];
            PaintList.Add(PaintBox.Image);
            imgIndex = PaintList.Count - 1;
        }

        private void CloseUpButton_Click(object sender, EventArgs e)//拡大処理------------------------------
        {
            try
            {
                if (ImageBox.Image == null) return;
                Bitmap bmp;
                Graphics ImGrfx;
                double SizeReg = 0.25;

                if (SizeCT == 1.5) return;
                ImageBox.Image = null;
                ImageBox.Image = Image.FromFile(fn);
                bmp = new Bitmap(ImageBox.Image, (int)(pictureBoxX * (SizeCT + SizeReg)), (int)(pictureBoxY * (SizeCT + SizeReg)));
                ImageBox.Image = bmp;

                PaintBox.Size = new Size(ImageBox.Width, ImageBox.Height);
                bmp = new Bitmap(PaintList[imgIndex], ImageBox.Image.Width, ImageBox.Image.Height);
                ImGrfx = Graphics.FromImage(bmp);
                ImGrfx.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
                ImGrfx.DrawImage(bmp, 0, 0, (int)(ImageBox.Width), (int)(ImageBox.Height));

                PaintBox.Image = bmp;
                PaintList.Add(PaintBox.Image);

                ImageBairitsuInfo.Text = "x" + (SizeCT + SizeReg);
                SizeCT += 0.25;
            }
            catch (Exception)
            {
                MessageBox.Show("Error");
            }
        }


        private void CloseDownButon_Click(object sender, EventArgs e)//縮小処理-------------------------------------------
        {
            try
            {
                if (ImageBox.Image == null) return;
                Bitmap bmp;
                Graphics ImGrfx;
                double SizeReg = 0.25;

                if (SizeCT == 0.5) return;
                ImageBox.Image = null;
                ImageBox.Image = Image.FromFile(fn);
                bmp = new Bitmap(ImageBox.Image, (int)(pictureBoxX * (SizeCT - SizeReg)), (int)(pictureBoxY * (SizeCT - SizeReg)));
                ImageBox.Image = bmp;

                PaintBox.Size = new Size(ImageBox.Width, ImageBox.Height);
                bmp = new Bitmap(PaintList[imgIndex], ImageBox.Image.Width, ImageBox.Image.Height);
                ImGrfx = Graphics.FromImage(bmp);
                ImGrfx.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
                ImGrfx.DrawImage(bmp, 0, 0, (int)(ImageBox.Width), (int)(ImageBox.Height));

                PaintBox.Image = bmp;
                PaintList.Add(PaintBox.Image);

                ImageBairitsuInfo.Text = "x" + (SizeCT - SizeReg);
                SizeCT -= 0.25;
            }
            catch (Exception)
            {
                MessageBox.Show("Error");
            }
        }

        private void UNDObutton_Click(object sender, EventArgs e)
        {       
            try
            {
                if (imgIndex == 0) return;
                imgIndex--;

                if (PaintBox.Width != PaintList[imgIndex].Width)
                {
                    BoxResize = new Bitmap(PaintList[imgIndex], ImageBox.Image.Width, ImageBox.Image.Height);
                    PaintBox.Image = BoxResize;
                }
                else
                {
                    PaintBox.Image = PaintList[imgIndex];
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Error");
            }
        }

        private void PaintBox_Click(object sender, EventArgs e)
        {

        }
        private void PaintBox_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) //マウス押下時座標取得
        {         
            MouseTap = true;
                startX = e.X;
                startY = e.Y;
                drawLine(e);            
        }

        private void PaintBox_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) //描画終了処理
        {
            

            removeListAfterIndex();

            MouseTap = false;

            if (MouseDrawed)
            {
                MouseDrawed = false;
                PaintList.Add(PaintBox.Image);
                imgIndex = PaintList.Count - 1;
            }
        }

        private void removeListAfterIndex()
        {
            while (imgIndex+1 < PaintList.Count)
            {
                PaintList.RemoveAt(imgIndex+1);
            }
        }


        private void PaintBox_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) //描画処理
        {
            if (MouseTap == false) return;
            MouseDrawed = true;
            drawLine(e);
            }

        private void drawLine(MouseEventArgs e)
        {
            if (ImageBox.Image == null) return;

            int PenSize = int.Parse((PenSizeBox.Text).Remove(PenSizeBox.Text.Length - 2));
            if(SizeCT != 1)
            {
                double PenResize  = (double)PenSize * SizeCT;
                PenSize = (int)(PenResize);
            }

            Pen pen = new Pen(color, PenSize);
            
            try
            {
                PaintCanvas = new Bitmap(PaintBox.Image);
                grfx = Graphics.FromImage(PaintCanvas);
                grfx.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
                grfx.FillEllipse(brush, startX - PenSize / 2, startY - PenSize / 2, PenSize, PenSize);
                grfx.DrawLine(pen, startX, startY, e.X, e.Y);
                startX = e.X;
                startY = e.Y;
                
                PaintBox.Image = PaintCanvas;
            }
            catch (Exception)
            {
                MessageBox.Show("Error");
            }
            finally
            {
                pen.Dispose();
                grfx.Dispose();
            }

        }
    }



}

Submission Info

Submission Time
Task A - 積雪深差
User SoySauceMan
Language C# (Mono 4.6.2.0)
Score 0
Code Size 16700 Byte
Status CE

Compile Error

./Main.cs(2,14): error CS0234: The type or namespace name `Drawing' does not exist in the namespace `System'. Are you missing `System.Drawing' assembly reference?
./Main.cs(3,22): error CS0234: The type or namespace name `Forms' does not exist in the namespace `System.Windows'. Are you missing `System.Windows.Forms' assembly reference?
./Main.cs(10,45): error CS0246: The type or namespace name `Form' could not be found. Are you missing an assembly reference?
./Main.cs(18,14): error CS0246: The type or namespace name `Image' could not be found. Are you missing an assembly reference?
./Main.cs(20,9): error CS0246: The type or namespace name `Bitmap' could not be found. Are you missing an assembly reference?
./Main.cs(21,9): error CS0246: The type or namespace name `Graphics' could not be found. Are you missing an assembly reference?
./Main.cs(22,9): error CS0246: The type or namespace name `Brush' could not be found. Are you missing an assembly reference?
./Main.cs(23,9): error CS0246: The type or namespace nam...