使用手写笔时,PictureBox.Image为null

时间:2013-04-21 10:02:53

标签: c# winforms

我正在尝试将PictureBox的内容保存到数据库中。这本身效果很好,但是一旦绘制了签名图片框,它就不会设置PictureBox.Image属性,这意味着我无法继续这个过程。

 Pen myPen;
    bool bMouseDown = false;
    Point prevPoint;
    Graphics gCust;
    Graphics gTech;

private void NewJob_Load(object sender, EventArgs e)
    {
        myPen = new System.Drawing.Pen(System.Drawing.Color.Black);
        gCust = pbCustomerSig.CreateGraphics();
        gCust.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        myPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
        myPen.SetLineCap(LineCap.Round, LineCap.Round, DashCap.Round);
        myPen.Color = Color.Blue;
        myPen.Width = 2f;
}
public static byte[] ImageToBinary(Image image)
    {
        Byte[] buffer = (Byte[])new ImageConverter().ConvertTo(image, typeof(Byte[]));
        return buffer;
    }
private void pbCustomerSig_MouseDown(object sender, MouseEventArgs e)
    {
        prevPoint = e.Location;
        bMouseDown = true;
    }

    private void pbCustomerSig_MouseMove(object sender, MouseEventArgs e)
    {
        if (bMouseDown)
        {
            Point thisPoint = e.Location;
            if (prevPoint.X == 0 && prevPoint.Y == 0)
            {
                prevPoint = thisPoint;
                return;
            }
            gCust.DrawLine(myPen, thisPoint.X, thisPoint.Y, prevPoint.X, prevPoint.Y);
            prevPoint = thisPoint;
        }
    }

    private void pbCustomerSig_MouseUp(object sender, MouseEventArgs e)
    {
        bMouseDown = false;
    }

错误在这里 -

h.CustomerSignature = ImageToBinary(pbCustomerSig.Image);

为什么PictureBox.Image属性为null?

非常感谢!

1 个答案:

答案 0 :(得分:2)

您没有为pbCustomerSig.Image分配值。无效是正常的。 而不是那样尝试绘制到位图。 Here is a sample for drawing on existing bitmap但您可以使用相同的方式在空位图上绘图并同时在图片框上显示。