图片框.使用手写笔时,图像为空

本文关键字:图像 手写笔 | 更新日期: 2023-09-27 18:10:07

我试图将PictureBox的内容保存到数据库中。这本身工作得很好,但是一旦签名图片框被绘制上,它就不会设置PictureBox。图像属性,这意味着我不能继续处理。

 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。图像属性为空?

多谢!

图片框.使用手写笔时,图像为空

您没有为pbCustomerSig.Image赋值。为零是正常的。不要那样做,试着画一个位图。这里是一个在现有位图上绘制的示例,但是你可以用同样的方式在空位图上绘制,并同时显示在picturebox上。