How do I send contents of a panel to an image column in datagridview?

CLUELESS.EXE 0 Reputation points
2023-03-24T14:05:17.3966667+00:00

I am working in Win Forms here. I have a panel that I've coded to allow for freehand drawing. This is going to act as a box for users to sign their names. Also, I have a combobox that is pulling names from a datagridview. This is how I'd like it to work:

1.) User selects one of the names from combo box

2.) User then "signs" the panel and clicks a button to transfer the "signature"

3.) The signature shows up in a signature column in the same row as the selected name from the combo box.

Does that make sense?

If there's an easier way to do this, please let me know. I am a total novice when it comes to Win forms/coding in general. I'm so grateful for any assistance!

I tried finding code for this in a listview. I tried doing a picturebox instead of a panel.

Here is the code that I have so far that is at least somewhat relevant, I believe:

using System.Drawing.Imaging;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Data;
using System.Windows.Forms;
using System.IO;
using PagedList;
using MySql.Data.MySqlClient;

namespace QTRSIGNINSHEETv_0._5
{
    public partial class Form1 : Form
    {
        public Point current = new Point();
        public Point old = new Point();

        public Graphics g;
        public Graphics graph;

        public Pen pen = new Pen(Color.Black, 5);

        Bitmap surface;
        Bitmap memoryImage;
        DataTable table = new DataTable();

        public Form1()
        {

            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;
            g = canvasPanel.CreateGraphics();
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            pen.SetLineCap(System.Drawing.Drawing2D.LineCap.Round, System.Drawing.Drawing2D.LineCap.Round, System.Drawing.Drawing2D.DashCap.Round);

            surface = new Bitmap(canvasPanel.Width, canvasPanel.Height);

            graph = Graphics.FromImage(surface);

            canvasPanel.BackgroundImage = surface;
            canvasPanel.BackgroundImageLayout = ImageLayout.None;
        }
         //Ignore this, it doesn't work currently.
        private void button1_Click(object sender, EventArgs e)
        {
            int n = dataGridView1.Rows.Add();
            dataGridView1.Rows[n].Cells[0].Value = textBox2.Text + ", " + textBox1.Text;
            dataGridView1.Rows[n].Cells[1].Value = "";
            dataGridView1.Rows[n].Cells[2].Value = "2";
            dataGridView1.Rows[n].Cells[3].Value = "630";
            dataGridView1.Rows[n].Cells[4].Value = "50";
            dataGridView1.Rows[n].Cells[5].Value = "50";
            dataGridView1.Rows[n].Cells[6].Value = "50";
            dataGridView1.Rows[n].Cells[7].Value = "10";
            dataGridView1.Rows[n].Cells[8].Value = "60";
            dataGridView1.Rows[n].Cells[9].Value = "0";
            dataGridView1.Rows[n].Cells[10].Value = "30";
            dataGridView1.Rows[n].Cells[11].Value = "20";
            dataGridView1.Rows[n].Cells[12].Value = "0";
        }

        private void canvas_MouseDown(object sender, MouseEventArgs e)
        {
            old = e.Location;
        }

        private void canvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                current = e.Location;
                g.DrawLine(pen, old, current);
                graph.DrawLine(pen, old, current);

                old = current;
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            graph.Clear(Color.White);
            canvasPanel.Invalidate();
        }

          //Pulls names from datagridview1 and puts them in combobox.
        private void button4_Click(object sender, EventArgs e)
        {
            comboBox1.Items.Clear();
            foreach (DataGridViewRow row in dataGridView1.Rows)
                if (row.Cells[0].Value != null)
                {
                    comboBox1.Items.Add(row.Cells[0].Value);
                }
        }
        private void button5_Click(object sender, System.EventArgs e)
        {
            this.dataGridView1.ClearSelection();
            int DGVOriginalHeight = dataGridView1.Height;
            dataGridView1.Height = (dataGridView1.RowCount * dataGridView1.RowTemplate.Height) + dataGridView1.ColumnHeadersHeight;

            using (Bitmap bitmap = new Bitmap(dataGridView1.Width, dataGridView1.Height))
            {
                string filedate = "QTR_" + DateTime.Now.ToString(" MM.dd.yyyy") + ".png";

                dataGridView1.DrawToBitmap(bitmap, new Rectangle(Point.Empty, dataGridView1.Size));
                string DesktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                bitmap.Save(Path.Combine(DesktopFolder, filedate), ImageFormat.Png);
            }
            dataGridView1.Height = DGVOriginalHeight;
        }

        private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
            printDocument1.DefaultPageSettings.Landscape = true;
            Bitmap imagebmp = new Bitmap(this.dataGridView1.Width, this.dataGridView1.Height);
            dataGridView1.DrawToBitmap(imagebmp, new Rectangle(0, 0, this.dataGridView1.Width, this.dataGridView1.Height));
            e.Graphics.DrawImage(imagebmp, 0, 0);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            printDocument1.DefaultPageSettings.Landscape = true;
            this.dataGridView1.ClearSelection();
            printPreviewDialog1.Document = printDocument1;
            printPreviewDialog1.PrintPreviewControl.Zoom = 1;
            printPreviewDialog1.ShowDialog();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ActiveControl = dataGridView1;
            table.Columns.Add("Name", typeof(string));
            table.Columns.Add("Signature", typeof(string));
            table.Columns.Add("In", typeof(int));
            table.Columns.Add("Out", typeof(int));
            table.Columns.Add("Academics", typeof(int));
            table.Columns.Add("Life Skills", typeof(int));
            table.Columns.Add("Mentoring", typeof(int));
            table.Columns.Add("Parental Involvement", typeof(int));
            table.Columns.Add("Sports & Recreation", typeof(int));
            table.Columns.Add("Service Learning", typeof(int));
            table.Columns.Add("STEM", typeof(int));
            table.Columns.Add("Meal", typeof(int));
            table.Columns.Add("Paid Employee", typeof(int));
            table.Columns.Add("Non-Enrolled", typeof(bool));
            dataGridView1.DataSource = table;
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {

        }

        private void btnNext_Click(object sender, EventArgs e)
        {

        }

          //Pulling data from text file
        private void button7_Click_1(object sender, EventArgs e)
        {
            string[] lines = File.ReadAllLines(@"C:\Users\Owner\Desktop\Programming\C#\TABLE.txt");
            string[] values;

            for (int i = 0; i < lines.Length; i++)
            {
                values = lines[i].ToString().Split('/');
                string[] row = new string[values.Length];

                for (int j = 0; j < values.Length; j++)
                {
                    row[j] = values[j].Trim();
                }
                table.Rows.Add(row);
            }
        }

    }
}
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,884 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,061 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,859 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jiale Xue - MSFT 44,751 Reputation points Microsoft Vendor
    2023-04-05T08:28:36.8633333+00:00

    Hi @CLUELESS.EXE ,Welcome to Microsoft Q&A. Sorry for late reply, I've stripped down your code, mine shows how to save the user's "signature", and save it.

    Finally, add the picture to the dataGridView.

    using System.Data;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp3
    {
        public partial class Form1 : Form
        {
            public Point current = new Point();
            public Point old = new Point();
    
            public Graphics g;
            public Graphics graph;
    
            public Pen pen = new Pen(Color.Black, 5);
            Bitmap surface;
            DataTable table = new DataTable();
            int i = 0;
            public Form1()
            {
                InitializeComponent();
                this.WindowState = FormWindowState.Maximized;
                g = canvasPanel.CreateGraphics();
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                pen.SetLineCap(System.Drawing.Drawing2D.LineCap.Round, System.Drawing.Drawing2D.LineCap.Round, System.Drawing.Drawing2D.DashCap.Round);
                surface = new Bitmap(canvasPanel.Width, canvasPanel.Height);
                graph = Graphics.FromImage(surface);
                canvasPanel.BackgroundImageLayout = ImageLayout.None;
            }
    
            private void Canvas_MouseDown(object sender, MouseEventArgs e)
            {
                old = e.Location;
            }
    
            private void Canvas_MouseMove(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    current = e.Location;
                    g.DrawLine(pen, old, current);
                    old = current;
                }
            }
    
            private void Form1_Load(object sender, System.EventArgs e)
            {
                ActiveControl = dataGridView1;
                table.Columns.Add("Signature", typeof(System.Drawing.Image));
                dataGridView1.DataSource = table;
            }
    
            private void button1_Click(object sender, System.EventArgs e)
            {
                graph.CopyFromScreen(canvasPanel.PointToScreen(Point.Empty), Point.Empty, canvasPanel.Size);
                string filename = "\\test" + (++i).ToString() + ".jpg";
                surface.Save(filename, ImageFormat.Jpeg);
                DataRow dr = table.NewRow();
                dr["Signature"] = System.Drawing.Image.FromFile(filename);
                table.Rows.Add(dr);
            }
    
            private void button2_Click(object sender, System.EventArgs e)
            {
                g.Clear(Color.White);
                canvasPanel.Invalidate();
            }
        }
    }
    

    4.5

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.