PictureBox

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;
 
namespace _11PictureBox
{
    public partial class Form1 : Form
    {
        Image img;
        Graphics gObject;
        SolidBrush brush = new SolidBrush(Color.White);
        Pen pen = new Pen(Color.Black);
        Color farbe = Color.Black;
        Color textfarbe = Color.Black;
        Font schriftart = new Font("Arial", 10);
 
        Random rand = new Random();
 
        public Form1()
        {
            InitializeComponent();
 
            img = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            pictureBox1.Image = img;
            gObject = Graphics.FromImage(pictureBox1.Image);
            gObject.FillRectangle(brush,0, 0, pictureBox1.Width, pictureBox1.Height);
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            int r = rand.Next(0, 256);
            int g = rand.Next(0, 256);
            int b = rand.Next(0, 256);
            int a = rand.Next(0, 256);
            brush.Color = Color.FromArgb(a, r, g, b);
            Console.WriteLine(brush.Color.ToString());
            int x = rand.Next(0, pictureBox1.Width - 20);
            int y = rand.Next(0, pictureBox1.Height - 20);
            gObject.FillEllipse(brush, x, y, 20, 20);
            pictureBox1.Refresh();
        }
 
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            Bitmap bmp = (Bitmap)pictureBox1.Image;
            // Bitmap bmp = (Bitmap)img;  //ebenfalls möglich
            farbe = bmp.GetPixel(e.X, e.Y);
            // farbe=((Bitmap)img).GetPixel(e.X, e.Y);   //ebenfalls möglich
            pictureBox2.BackColor = farbe;
            label1.Text = farbe.ToString();
        }
 
        private void pictureBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            brush.Color = textfarbe;
            gObject.DrawString(textBox1.Text, schriftart, brush, e.X, e.Y);
            pictureBox1.Refresh();
        }
 
        private void button3_Click(object sender, EventArgs e)
        {
            if(colorDialog1.ShowDialog()==DialogResult.OK)
            {
                textfarbe = colorDialog1.Color;
                pictureBox3.BackColor = textfarbe;
            }
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            if (fontDialog1.ShowDialog() == DialogResult.OK)
            {
                schriftart = fontDialog1.Font;
                label2.Text = fontDialog1.Font.Name.ToString() + ", " + fontDialog1.Font.Size.ToString();
            }
        }
 
        private void speichernToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(saveFileDialog1.ShowDialog()==DialogResult.OK)
            {
                img.Save(saveFileDialog1.FileName);
            }
        }
 
        private void öffnenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(openFileDialog1.ShowDialog()==DialogResult.OK)
            {
                img = Bitmap.FromFile(openFileDialog1.FileName);
                pictureBox1.Image = img;
                gObject = Graphics.FromImage(pictureBox1.Image);
            }
 
        }
 
        private void neuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            gObject.FillRectangle(new SolidBrush(Color.White), 0, 0, pictureBox1.Width, pictureBox1.Height);
            pictureBox1.Refresh();
        }
 
        private void beendenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form1.ActiveForm.Close();
        }
    }
}