0
Answer

implement a saving button into a graphics c# form

Photo of Andreas Jung

Andreas Jung

23h
53
1

Hello, I have the following form1 (windows forms). Its a graphics form, which shows a nonrectangular shape. It saves automatically the shape into a given folder. Now I want to  save the object with a click of a button. How can I implement  such a button. Thank you.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace DrawingBasicsPaths
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void FillRectangle1(object sender, PaintEventArgs e)
        {
            int width = 860;
            int height = 370;
            Rectangle itsme = new Rectangle(10, 10, width - 20, height - 20);
            GraphicsPath itsme2 = GetRoundedRectPath(itsme, width / 10);
            e.Graphics.FillPath(Brushes.Yellow, itsme2);  
            e.Graphics.DrawPath(Pens.Black, itsme2);
            Graphics g = e.Graphics;
            save(g, itsme2);
        }
        private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
        {
            int diameter = 2 * radius;
            Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
            //in addition to using basics shapes you can compose and draw shapes together using a path.
            //a path is a container for zero or more shapes
            GraphicsPath path = new GraphicsPath();
            //the rounded rectangle is composed of 8 shapes: 4 arcs(one at each of the corners) and 4 lines
            //top left
            path.AddArc(arcRect, 180, 90);
            //top right
            arcRect.X = rect.Right - diameter;
            path.AddArc(arcRect, 270, 90);
            //bottom right
            arcRect.Y = rect.Bottom - diameter;
            path.AddArc(arcRect, 0, 90);
            //botttom left
            arcRect.X = rect.Left;
            path.AddArc(arcRect, 90, 90);
            //after the last arc is added we call the CloseFigure method to join the points
            path.CloseFigure();
            return path;
        }

        private void save(Graphics g, GraphicsPath itsme2)
        {
            //wrap (create) Graphis object
            Image itsme3 = new Bitmap(860, 370, g);
            Graphics i = Graphics.FromImage(itsme3);
            i.FillPath(Brushes.Yellow, itsme2);
            i.DrawPath(Pens.Black, itsme2);
            //save to a file
            itsme3.Save(@"..\..\..\..\..\..\content\pics\RoundedRectangle.png");
        }
    }
}

namespace DrawingBasicsPaths
{
    partial class Form1
    {
        /// <summary>
        /// Erforderliche Designervariable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        #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.pictureBox1 = new System.Windows.Forms.PictureBox();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // pictureBox1
            // 
            this.pictureBox1.Location = new System.Drawing.Point(46, 12);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(864, 375);
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.FillRectangle1);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(930, 406);
            this.Controls.Add(this.pictureBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.PictureBox pictureBox1;
    }
}