正在保存范围外的C#

本文关键字:范围 保存 | 更新日期: 2023-09-27 17:59:39

我正在尝试创建一个程序,用户可以在其中为数据网格视图中的单元格着色,然后将数据网格视图保存为图像。然而,当我以需要的方式生成数据网格视图时,我遇到了范围问题。

我想不出一种好的(快速的)方式来以合理的方式重组这件事。在这种情况下,我如何避免我的数据网格视图超出范围?

谢谢!

public partial class Form2 : Form
{
    bool erasing = false;
    public Form2()
    {
        InitializeComponent();
    }
    private void Form2_Load(object sender, EventArgs e)
    {
        dataGridView1.ColumnCount = Properties.Settings.Default.Width;
        dataGridView1.RowCount = Properties.Settings.Default.Height;
        dataGridView1.RowHeadersVisible = false;
        dataGridView1.ColumnHeadersVisible = false;
        dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
        dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
        dataGridView1.AutoSize = true;
        dataGridView1.ClearSelection();
        dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Black;
        dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);
    }
    void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        if (erasing)
        {
            dataGridView1.DefaultCellStyle.SelectionBackColor = Color.White;
            dataGridView1[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.White;
        }
        else
        {
            dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Black;
            dataGridView1[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.Black;
        }
    }
    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (checkBox1.Checked)
        {
            erasing = true;
        }
        else
        {
            erasing = false; 
        }
    }
    private void savegviewImg()
    {
        Bitmap bmap = new Bitmap(DataGridView1.Bounds.Width, DataGridView1.Bounds.Height);
        DataGridView1.DrawToBitmap(bmap, new Rectangle(1, 1, DataGridView1.Width, DataGridView1.Height));
        bmap.Save("C:''HelpMehStackOFlowYoureMyOnlyHope.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
    }
}

}

正在保存范围外的C#

savegviewImg()方法中应该是dataGridView1而不是dataGridView1,不是吗?这就是你在其他地方提到它的方式。