C# 如何在PictureBox上绘制网格以保留其包含的图像?

C# 如何在PictureBox上绘制网格以保留其包含的图像?,c#,winforms,C#,Winforms,我想在PictureBox中的图像上绘制网格。 但是,当绘制网格时,图像将消失。 如何修复此代码?选中复选框或使用轨迹栏更改网格大小时,它会绘制网格 private void grid() { int x, y; int w = pictureBox1.Size.Width; int h = pictureBox1.Size.Height; int inc = trackBar1.Value; BackImage = new Bitmap(w, h);

我想在PictureBox中的图像上绘制网格。
但是,当绘制网格时,图像将消失。
如何修复此代码?选中复选框或使用轨迹栏更改网格大小时,它会绘制网格

private void grid()
{
    int x, y;
    int w = pictureBox1.Size.Width;
    int h = pictureBox1.Size.Height;
    int inc = trackBar1.Value;

    BackImage = new Bitmap(w, h);
    Pen myPen = new Pen(Color.Green);

    Graphics gr = Graphics.FromImage(BackImage);
    gr.Clear(SystemColors.Control);

    if (checkBox1.Checked == true)
    {
        myPen.Color = Color.Green;

        for (x = 0; x < w; x += inc)
            gr.DrawLine(myPen, x, pictureBox1.Location.Y, x, h);

        for (y = 0; y < h; y += inc)
            gr.DrawLine(myPen, pictureBox1.Location.X, y, w, y);
    }
    Invalidate();
    myPen.Dispose();
}

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
   grid();
    Refresh();
}

private void trackBar1_Scroll(object sender, EventArgs e)
{
   grid();
   Refresh();
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    if (BackImage != null)
    e.Graphics.DrawImage(BackImage, 0, 0);
}
private void grid()
{
int x,y;
int w=pictureBox1.Size.Width;
int h=pictureBox1.Size.Height;
int inc=trackBar1.Value;
BackImage=新位图(w,h);
钢笔myPen=新钢笔(颜色为绿色);
Graphics gr=Graphics.FromImage(BackImage);
gr.Clear(系统颜色控制);
if(checkBox1.Checked==true)
{
myPen.Color=Color.Green;
对于(x=0;x
将PictureBox的Image属性设置为所需的图像,然后在PictureBox的Paint()事件中使用提供的
e.Graphics

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    pictureBox1.Invalidate();
}

private void trackBar1_Scroll(object sender, EventArgs e)
{
    pictureBox1.Invalidate();
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    if (checkBox1.Checked)
    {
        int x, y;
        int w = pictureBox1.Size.Width;
        int h = pictureBox1.Size.Height;
        int inc = trackBar1.Value;

        Graphics gr = e.Graphics;
        if (checkBox1.Checked == true)
        {
            for (x = 0; x < w; x += inc)
                gr.DrawLine(Pens.Green, x, 0, x, h);

            for (y = 0; y < h; y += inc)
                gr.DrawLine(Pens.Green, 0, y, w, y);
        }
    }
}
private void checkBox1\u CheckedChanged(对象发送方,事件参数e)
{
pictureBox1.Invalidate();
}
私有void trackBar1_滚动(对象发送方,事件参数e)
{
pictureBox1.Invalidate();
}
私有void pictureBox1_Paint(对象发送方,PaintEventArgs e)
{
如果(复选框1.选中)
{
int x,y;
int w=pictureBox1.Size.Width;
int h=pictureBox1.Size.Height;
int inc=trackBar1.Value;
图形gr=e.图形;
if(checkBox1.Checked==true)
{
对于(x=0;x
BackImage=新位图(w,h)这将创建一个空白图像。您需要两个图像变量。一个用于背景,另一个用于绘图。你画的那个只需要画背景的图像,然后你在上面画网格。或者只绘制PictureBox paint事件。将位图设置为PictureBox的
图像
背景图像
,并在控件的paint事件中绘制控件表面上的网格。如果要在其上绘制其他位图,请使用
BackgroundImage
属性。如果只需要绘制网格,请使用
图像
属性。您没有说是否希望在某个时候将位图与网格一起保存。谢谢!!我把我的代码放在绘画活动上,效果很好!