C# 创建、绘制线条和保存位图提供通用GDI+;错误

C# 创建、绘制线条和保存位图提供通用GDI+;错误,c#,image,winforms,bitmap,C#,Image,Winforms,Bitmap,我有一个非常简单的方法,将签名作为点列表,并在位图上绘制为线。我想获取位图并将其保存到文件中,但是当我调用save方法时,我得到的是“GDI+中发生的一般错误”,并且没有内部异常 这似乎是相当直截了当的代码,所以我不知道问题是什么 using (var b = new Bitmap(width, height)) { var g = Graphics.FromImage(b); var lastPoint = points[0]; for (int i = 1; i &l

我有一个非常简单的方法,将签名作为点列表,并在位图上绘制为线。我想获取位图并将其保存到文件中,但是当我调用save方法时,我得到的是“GDI+中发生的一般错误”,并且没有内部异常

这似乎是相当直截了当的代码,所以我不知道问题是什么

using (var b = new Bitmap(width, height))
{
    var g = Graphics.FromImage(b);
    var lastPoint = points[0];
    for (int i = 1; i < points.Length; i++)
    {
        var p = points[i];
        // When the user takes the pen off the device, X/Y is set to -1
        if ((lastPoint.X >= 0 || lastPoint.Y >= 0) && (p.X >= 0 || p.Y >= 0))
            g.DrawLine(Pens.Black, lastPoint.X, lastPoint.Y, p.X, p.Y);
        lastPoint = p;
    }
    g.Flush();
    pictureBox.Image = b;
    b.Save("C:\\test.bmp");
}
奇怪的是,如果我删除b.Save(或忽略异常),pictureBox会完美地显示图像


任何帮助都将不胜感激

我将使用
Paint
事件中相应的
图形
绘制到
PictureBox
,然后保存到位图:

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        MyDrawing(e.Graphics);

        Bitmap b = new Bitmap(pictureBox1.Width, pictureBox1.Height);
        pictureBox1.DrawToBitmap(b, pictureBox1.ClientRectangle);
        b.Save(@"C:\test.bmp");

    }
    private void MyDrawing(Graphics g)
    {
        var lastPoint = points[0];

        for (int i = 1; i < points.Count; i++)
        {
            var p = points[i];
            // When the user takes the pen off the device, X/Y is set to -1
            if ((lastPoint.X >= 0 || lastPoint.Y >= 0) && (p.X >= 0 || p.Y >= 0))
                g.DrawLine(Pens.Black, lastPoint.X, lastPoint.Y, p.X, p.Y);
            lastPoint = p;
        }
        g.Flush();
    }
private void pictureBox1\u Paint(对象发送方,PaintEventArgs e)
{
MyDrawing(如图形);
位图b=新位图(pictureBox1.Width,pictureBox1.Height);
pictureBox1.DrawToBitmap(b,pictureBox1.ClientRectangle);
b、 保存(@“C:\test.bmp”);
}
私人图纸(图形g)
{
var lastPoint=点[0];
对于(int i=1;i=0 | | lastPoint.Y>=0)和&(p.X>=0 | | p.Y>=0))
g、 抽绳(黑色钢笔,lastPoint.X,lastPoint.Y,p.X,p.Y);
最后一点=p;
}
g、 冲洗();
}

已保存的BMP:


我将使用
Paint
事件中相应的
Graphics
绘制到
PictureBox
,然后保存到位图:

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        MyDrawing(e.Graphics);

        Bitmap b = new Bitmap(pictureBox1.Width, pictureBox1.Height);
        pictureBox1.DrawToBitmap(b, pictureBox1.ClientRectangle);
        b.Save(@"C:\test.bmp");

    }
    private void MyDrawing(Graphics g)
    {
        var lastPoint = points[0];

        for (int i = 1; i < points.Count; i++)
        {
            var p = points[i];
            // When the user takes the pen off the device, X/Y is set to -1
            if ((lastPoint.X >= 0 || lastPoint.Y >= 0) && (p.X >= 0 || p.Y >= 0))
                g.DrawLine(Pens.Black, lastPoint.X, lastPoint.Y, p.X, p.Y);
            lastPoint = p;
        }
        g.Flush();
    }
private void pictureBox1\u Paint(对象发送方,PaintEventArgs e)
{
MyDrawing(如图形);
位图b=新位图(pictureBox1.Width,pictureBox1.Height);
pictureBox1.DrawToBitmap(b,pictureBox1.ClientRectangle);
b、 保存(@“C:\test.bmp”);
}
私人图纸(图形g)
{
var lastPoint=点[0];
对于(int i=1;i=0 | | lastPoint.Y>=0)和&(p.X>=0 | | p.Y>=0))
g、 抽绳(黑色钢笔,lastPoint.X,lastPoint.Y,p.X,p.Y);
最后一点=p;
}
g、 冲洗();
}

已保存的BMP:


我将使用
Paint
事件中相应的
Graphics
绘制到
PictureBox
,然后保存到位图:

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        MyDrawing(e.Graphics);

        Bitmap b = new Bitmap(pictureBox1.Width, pictureBox1.Height);
        pictureBox1.DrawToBitmap(b, pictureBox1.ClientRectangle);
        b.Save(@"C:\test.bmp");

    }
    private void MyDrawing(Graphics g)
    {
        var lastPoint = points[0];

        for (int i = 1; i < points.Count; i++)
        {
            var p = points[i];
            // When the user takes the pen off the device, X/Y is set to -1
            if ((lastPoint.X >= 0 || lastPoint.Y >= 0) && (p.X >= 0 || p.Y >= 0))
                g.DrawLine(Pens.Black, lastPoint.X, lastPoint.Y, p.X, p.Y);
            lastPoint = p;
        }
        g.Flush();
    }
private void pictureBox1\u Paint(对象发送方,PaintEventArgs e)
{
MyDrawing(如图形);
位图b=新位图(pictureBox1.Width,pictureBox1.Height);
pictureBox1.DrawToBitmap(b,pictureBox1.ClientRectangle);
b、 保存(@“C:\test.bmp”);
}
私人图纸(图形g)
{
var lastPoint=点[0];
对于(int i=1;i=0 | | lastPoint.Y>=0)和&(p.X>=0 | | p.Y>=0))
g、 抽绳(黑色钢笔,lastPoint.X,lastPoint.Y,p.X,p.Y);
最后一点=p;
}
g、 冲洗();
}

已保存的BMP:


我将使用
Paint
事件中相应的
Graphics
绘制到
PictureBox
,然后保存到位图:

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        MyDrawing(e.Graphics);

        Bitmap b = new Bitmap(pictureBox1.Width, pictureBox1.Height);
        pictureBox1.DrawToBitmap(b, pictureBox1.ClientRectangle);
        b.Save(@"C:\test.bmp");

    }
    private void MyDrawing(Graphics g)
    {
        var lastPoint = points[0];

        for (int i = 1; i < points.Count; i++)
        {
            var p = points[i];
            // When the user takes the pen off the device, X/Y is set to -1
            if ((lastPoint.X >= 0 || lastPoint.Y >= 0) && (p.X >= 0 || p.Y >= 0))
                g.DrawLine(Pens.Black, lastPoint.X, lastPoint.Y, p.X, p.Y);
            lastPoint = p;
        }
        g.Flush();
    }
private void pictureBox1\u Paint(对象发送方,PaintEventArgs e)
{
MyDrawing(如图形);
位图b=新位图(pictureBox1.Width,pictureBox1.Height);
pictureBox1.DrawToBitmap(b,pictureBox1.ClientRectangle);
b、 保存(@“C:\test.bmp”);
}
私人图纸(图形g)
{
var lastPoint=点[0];
对于(int i=1;i=0 | | lastPoint.Y>=0)和&(p.X>=0 | | p.Y>=0))
g、 抽绳(黑色钢笔,lastPoint.X,lastPoint.Y,p.X,p.Y);
最后一点=p;
}
g、 冲洗();
}

已保存的BMP:


您的代码有两个问题,一个隐藏另一个:

  • 您的应用程序可能对
    C:
    的根目录没有写入权限,因此
    保存失败
  • 您也不能让
    图片盒
    显示
    位图
    ,然后使用
    块在
    中销毁该位图
因此,您应该将代码更改为以下内容:

var b = new Bitmap(width, height);

using (Graphics g = Graphics.FromImage(b))
{
    var lastPoint = points[0];
    for (int i = 1; i < points.Length; i++)
    {
        var p = points[i];
        // When the user takes the pen off the device, X/Y is set to -1
        if ((lastPoint.X >= 0 || lastPoint.Y >= 0) && (p.X >= 0 || p.Y >= 0))
            g.DrawLine(Pens.Black, lastPoint.X, lastPoint.Y, p.X, p.Y);
        lastPoint = p;
    }
    // g.Flush(); not necessary
    pictureBox1.Image = b;
    b.Save("C:\\temp\\test.bmp");
}
var b=新位图(宽度、高度);
使用(Graphics g=Graphics.FromImage(b))
{
var lastPoint=点[0];
对于(int i=1;i=0 | | lastPoint.Y>=0)和&(p.X>=0 | | p.Y>=0))
g、 抽绳(黑色钢笔,lastPoint.X,lastPoint.Y,p.X,p.Y);
最后一点=p;
}
//g.冲洗();不需要
pictureBox1.Image=b;
b、 保存(“C:\\temp\\test.bmp”);
}

您还应该检查数组的<代码>长度>代码,并考虑使用<代码>列表>代码>,也检查它的<代码>计数>代码>使用<代码>引号>代码> >代码>点。p> 您的代码有两个问题,一个隐藏另一个:

  • 您的应用程序可能对
    C:
    的根目录没有写入权限,因此
    保存失败
  • 您也不能让
    图片盒
    显示
    位图
    ,然后使用
    块在
    中销毁该位图
因此,您应该将代码更改为som