C# 小畫家

      在〈C# 小畫家〉中尚無留言

選單

拉入MenuStrip, 填入相關選單, 並於中間拉入一PictureBox

程式碼

namespace Painter
{
   public partial class Form1 : Form
   {
      public Form1()
      {
         InitializeComponent();
      }
      Bitmap bmp;
      int oldx, oldy;
      int penPoint;
      Color penColor;
      Graphics g;
      Pen pen;
      private void Form1_Load(object sender, EventArgs e)
      {
         pen = new Pen(penColor, penPoint);
         bmp = new Bitmap(this.Width, this.Height);
         g = Graphics.FromImage(bmp);
         pictureBox1.Size = new Size(this.Width, this.Height);
         pictureBox1.Location = new Point(0, 25);
         penColor = Color.Black;
         penPoint = 3;
         g.Clear(Color.White);
         pictureBox1.Image = bmp;
         pictureBox1.Refresh();
      }
      private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
      {
         oldx = e.X; oldy = e.Y;
      }
      private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
      {
         if (e.Button == MouseButtons.Left)
         {
            pen = new Pen(penColor, penPoint);
            g = Graphics.FromImage(bmp);
            g.DrawLine(pen, oldx, oldy, e.X, e.Y);
            pictureBox1.Image = bmp;
            oldx = e.X;
            oldy = e.Y;
         }
      }
      private void ptToolStripMenuItem_Click(object sender, EventArgs e)
      {
         penPoint = 1;
      }
      private void ptToolStripMenuItem1_Click(object sender, EventArgs e)
      {
         penPoint = 3;
      }
      private void ptToolStripMenuItem2_Click(object sender, EventArgs e)
      {
         penPoint = 5;
      }
      private void 黑ToolStripMenuItem_Click(object sender, EventArgs e)
      {
         penColor = Color.Black;
      }
      private void 紅ToolStripMenuItem_Click(object sender, EventArgs e)
      {
         penColor = Color.Red;
      }
      private void 黃ToolStripMenuItem_Click(object sender, EventArgs e)
      {
         penColor = Color.Yellow;
      }
      private void 綠ToolStripMenuItem_Click(object sender, EventArgs e)
      {
         penColor = Color.Green;
      }
      private void 存檔ToolStripMenuItem_Click(object sender, EventArgs e)
      {
         bmp.Save("tmp.jpg");
      }
      private void 開啟新檔ToolStripMenuItem_Click(object sender, EventArgs e)
      {
      }
      private void 開啟舊檔ToolStripMenuItem_Click(object sender, EventArgs e)
      {
         try
         {
            FileStream f = new FileStream("tmp.jpg", FileMode.Open);
            bmp = new Bitmap(f);
            f.Close();
            pictureBox1.Image = bmp;
         }
         catch (Exception ex) {
            MessageBox.Show("無暫存檔");
         }
      }
   }
}

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *