Image From Panel to Panel


Aprikose

Grünschnabel
Hallo Community,

Ich bin derweile dabei ein kleines Malprogramm zu basteln und brauche etwas Hilfe.
Ich möchte den aktuellen Zustand eines Panels(Gezeichnete Elemente + BackGround) in einem beliebigen Bildformat speichern und auf ein Anderes Panel übertragen.

Trotz intensivem Googelns und dem Lesen diverser Tutorials bin nicht weitergekommen, zum besseren Verständiss poste ich den kompletten Code, dieser ist imo auch lauffähig.

Form1.CS
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Paint
{
    public enum enumForm
    {
        Stift,
        Ellipse,
        Rechteck
    }

    public partial class Form1 : Form
    {
        private Graphics graph;
        private Graphics graphpreview;
        private Graphics graphBMP;

        private Point lastMousePoint;
        private Pen pen;
        //private Color clr;
        private enumForm currentEnumForm;
        private bool readyToDraw = false;
        private int previewEx;
        private int previewEy;
        private int clickLocationX;
        private int clickLocationY;
        private Bitmap bmp;
        public Form1()
        {
            InitializeComponent();
        }

        private void neuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            preview1.Refresh();
            pen = new Pen(Color.Black);
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            graphpreview = preview1.CreateGraphics();
            graph = backG1.CreateGraphics();
        }

        private void stiftToolStripMenuItem_Click(object sender, EventArgs e)
        {
            currentEnumForm = enumForm.Stift;
        }

        private void eToolStripMenuItem_Click(object sender, EventArgs e)
        {
            currentEnumForm = enumForm.Ellipse;
        }

        private void preview1_MouseMove(object sender, MouseEventArgs e)
        {
            graphpreview = preview1.CreateGraphics();
            previewEx = e.X;
            previewEy = e.Y;
            if (lastMousePoint == null)
            {
                lastMousePoint = new Point(e.X, e.Y);
            }

            if (readyToDraw == true)
            {
                switch (currentEnumForm)
                {
                    case enumForm.Stift:
                        graphpreview.DrawLine(pen, e.X, e.Y, lastMousePoint.X, lastMousePoint.Y);
                        graph.DrawLine(pen, e.X, e.Y, lastMousePoint.X, lastMousePoint.Y);
                        break;
                    case enumForm.Ellipse:
                        preview1.Refresh();
                        graphpreview.DrawEllipse(pen, clickLocationX, clickLocationY, (e.X - clickLocationX), (e.Y - clickLocationY));
                        break;
                    case enumForm.Rechteck:
                        break;
                    default:
                        break;
                }
            }
            lastMousePoint = e.Location;
        }

        private void preview1_MouseDown(object sender, MouseEventArgs e)
        {
            readyToDraw = true;
            clickLocationX = e.X;
            clickLocationY = e.Y;
        }

        private void preview1_MouseUp(object sender, MouseEventArgs e)
        {
            graph = backG1.CreateGraphics();
            readyToDraw = false;
            preview1.Visible = false;
            switch (currentEnumForm)
            {
                case enumForm.Ellipse:
                    graph.DrawEllipse(pen, clickLocationX, clickLocationY, (previewEx - clickLocationX), (previewEy - clickLocationY));
                    break;
                case enumForm.Rechteck:
                    break;
                default:
                    break;
            }
            // Bild vom aktuellen Zustand BackG1 machen
            if (bmp == null)
            {
                bmp = new Bitmap(backG1.ClientSize.Width, backG1.ClientSize.Height);
            }

            graphBMP = Graphics.FromImage(bmp);
            backG1.DrawToBitmap(bmp, new Rectangle(0,0,bmp.Width,bmp.Height));
            preview1.Visible = true;

            // preview Background = Bild
            preview1.BackgroundImage = bmp;
            graphpreview.DrawImage(bmp, 0, 0);
            graphpreview = null;
        }
    }
}

Die entsprechende Designer Datei
Code:
namespace Paint
{
    partial class Form1
    {
        /// <summary>
        /// Erforderliche Designervariable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Verwendete Ressourcen bereinigen.
        /// </summary>
        /// <param name="disposing">True, wenn verwaltete Ressourcen gelöscht werden sollen; andernfalls False.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Vom Windows Form-Designer generierter Code

        /// <summary>
        /// Erforderliche Methode für die Designerunterstützung.
        /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
        /// </summary>
        private void InitializeComponent()
        {
            this.menuStrip1 = new System.Windows.Forms.MenuStrip();
            this.dateiToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.neuToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.öffnenToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.speichernToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.werkzeugToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.farbeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.formToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.stiftToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.eToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.rechteckToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.preview1 = new Paint.Preview();
            this.backG1 = new Paint.BackG();
            this.menuStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // menuStrip1
            // 
            this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.dateiToolStripMenuItem,
            this.werkzeugToolStripMenuItem});
            this.menuStrip1.Location = new System.Drawing.Point(0, 0);
            this.menuStrip1.Name = "menuStrip1";
            this.menuStrip1.Size = new System.Drawing.Size(826, 24);
            this.menuStrip1.TabIndex = 0;
            this.menuStrip1.Text = "menuStrip1";
            // 
            // dateiToolStripMenuItem
            // 
            this.dateiToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.neuToolStripMenuItem,
            this.öffnenToolStripMenuItem,
            this.speichernToolStripMenuItem});
            this.dateiToolStripMenuItem.Name = "dateiToolStripMenuItem";
            this.dateiToolStripMenuItem.Size = new System.Drawing.Size(44, 20);
            this.dateiToolStripMenuItem.Text = "Datei";
            // 
            // neuToolStripMenuItem
            // 
            this.neuToolStripMenuItem.Name = "neuToolStripMenuItem";
            this.neuToolStripMenuItem.Size = new System.Drawing.Size(132, 22);
            this.neuToolStripMenuItem.Text = "Neu";
            this.neuToolStripMenuItem.Click += new System.EventHandler(this.neuToolStripMenuItem_Click);
            // 
            // öffnenToolStripMenuItem
            // 
            this.öffnenToolStripMenuItem.Name = "öffnenToolStripMenuItem";
            this.öffnenToolStripMenuItem.Size = new System.Drawing.Size(132, 22);
            this.öffnenToolStripMenuItem.Text = "Öffnen";
            // 
            // speichernToolStripMenuItem
            // 
            this.speichernToolStripMenuItem.Name = "speichernToolStripMenuItem";
            this.speichernToolStripMenuItem.Size = new System.Drawing.Size(132, 22);
            this.speichernToolStripMenuItem.Text = "Speichern";
            // 
            // werkzeugToolStripMenuItem
            // 
            this.werkzeugToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.farbeToolStripMenuItem,
            this.formToolStripMenuItem});
            this.werkzeugToolStripMenuItem.Name = "werkzeugToolStripMenuItem";
            this.werkzeugToolStripMenuItem.Size = new System.Drawing.Size(67, 20);
            this.werkzeugToolStripMenuItem.Text = "Werkzeug";
            // 
            // farbeToolStripMenuItem
            // 
            this.farbeToolStripMenuItem.Name = "farbeToolStripMenuItem";
            this.farbeToolStripMenuItem.Size = new System.Drawing.Size(113, 22);
            this.farbeToolStripMenuItem.Text = "Farbe";
            // 
            // formToolStripMenuItem
            // 
            this.formToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.stiftToolStripMenuItem,
            this.eToolStripMenuItem,
            this.rechteckToolStripMenuItem});
            this.formToolStripMenuItem.Name = "formToolStripMenuItem";
            this.formToolStripMenuItem.Size = new System.Drawing.Size(113, 22);
            this.formToolStripMenuItem.Text = "Form";
            // 
            // stiftToolStripMenuItem
            // 
            this.stiftToolStripMenuItem.Name = "stiftToolStripMenuItem";
            this.stiftToolStripMenuItem.Size = new System.Drawing.Size(129, 22);
            this.stiftToolStripMenuItem.Text = "Stift";
            this.stiftToolStripMenuItem.Click += new System.EventHandler(this.stiftToolStripMenuItem_Click);
            // 
            // eToolStripMenuItem
            // 
            this.eToolStripMenuItem.Name = "eToolStripMenuItem";
            this.eToolStripMenuItem.Size = new System.Drawing.Size(129, 22);
            this.eToolStripMenuItem.Text = "Ellipse";
            this.eToolStripMenuItem.Click += new System.EventHandler(this.eToolStripMenuItem_Click);
            // 
            // rechteckToolStripMenuItem
            // 
            this.rechteckToolStripMenuItem.Name = "rechteckToolStripMenuItem";
            this.rechteckToolStripMenuItem.Size = new System.Drawing.Size(129, 22);
            this.rechteckToolStripMenuItem.Text = "Rechteck";
            // 
            // preview1
            // 
            this.preview1.Location = new System.Drawing.Point(391, 24);
            this.preview1.Name = "preview1";
            this.preview1.Size = new System.Drawing.Size(435, 404);
            this.preview1.TabIndex = 0;
            this.preview1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.preview1_MouseMove);
            this.preview1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.preview1_MouseDown);
            this.preview1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.preview1_MouseUp);
            // 
            // backG1
            // 
            this.backG1.Dock = System.Windows.Forms.DockStyle.Left;
            this.backG1.Location = new System.Drawing.Point(0, 24);
            this.backG1.Name = "backG1";
            this.backG1.Size = new System.Drawing.Size(385, 404);
            this.backG1.TabIndex = 1;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(826, 428);
            this.Controls.Add(this.preview1);
            this.Controls.Add(this.backG1);
            this.Controls.Add(this.menuStrip1);
            this.MainMenuStrip = this.menuStrip1;
            this.Name = "Form1";
            this.Text = "Form1";
            this.Shown += new System.EventHandler(this.Form1_Shown);
            this.menuStrip1.ResumeLayout(false);
            this.menuStrip1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.MenuStrip menuStrip1;
        private System.Windows.Forms.ToolStripMenuItem dateiToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem neuToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem öffnenToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem speichernToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem werkzeugToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem farbeToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem formToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem stiftToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem eToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem rechteckToolStripMenuItem;
        private BackG backG1;
        private Preview preview1;
    }
}

Und die beiden Klassen von denen ich die zwei angesprochenen Panels abgeleitet habe.

Class1.cs
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Paint
{
    class Preview : Panel
    {
    }
}

Class2.cs
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Paint
{
    class BackG : Panel
    {
        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
        }
    }
}

Entschuldigt den riesen Post. Die kritische Stelle an welcher ich nicht weiterkomme ist rot markiert, befindet sich auch im ersten Codeauszug.

Danke schonmal :)