0

I have a .net PictureBox on my screen which creates an image in real-time from a signature pad input device.

I am trying to get the bytes from the image to save but because the PictureBox is always open, I am unable to get the .Image property as it is always NULL.

Is there a way to somehow close the PictureBox so I can take the image from it?

This is where I attempt to get the bytes but the .Image is NULL due to the creation in real-time:

private void btnConfirm_Click(object sender, EventArgs e)
{    
    byte[] imgData = null;
    // storage for the img bytes
    picSignature.Update();
    imgData = ImgToByteArray(picSignature.Image, ImageFormat.Jpeg);

    SerialPortListener.MainForm._imagevalues = imgData;
    this.Close();
}

This is the code which I believe writes the signature:

private void SignatureDataHandler(Int32 xAxis, Int32 yAxis, Int32 pressure, Int32 timeStamp)
{
    long x;
    long y;
    Pen myPen = new Pen(Color.Black, 1);

    x = (picSignature.Width * (xAxis - g_xMin)) / (g_xMax - g_xMin);
    y = (picSignature.Height * (yAxis - g_yMin)) / (g_yMax - g_yMin);
    if ((x > picSignature.Width) || (x < 0))
    {
        MessageBox.Show("problem");
    }
    if (0 != pressure)
    {
        if (g_fFirstTouch)
        {
            g_PicGraphics.DrawLine(myPen, x, y, x, y);
            g_fFirstTouch = false;
        }
        else
        {
            g_PicGraphics.DrawLine(myPen, g_lastX, g_lastY, x, y);
        }
    }
    else
    {
        g_fFirstTouch = true;
    }

    g_lastX = x;
    g_lastY = y;
}
iBener
  • 469
  • 6
  • 18
connersz
  • 1,153
  • 3
  • 23
  • 64
  • You lost me at "the PictureBox is always open". What do you mean, open? If the control has been assigned an image, and you see that image, then how is that possible if `Image` is null? Are you drawing to its surface manually? – DonBoitnott Jan 14 '15 at 12:22
  • It hasn't been assigned an image, it is written real-time. As the user writes their signature into the signature reader, you see it appear on the screen in the pictureBox. – connersz Jan 14 '15 at 12:24
  • If you draw on the PictureBox (hopefully in the Paint event!!) then you can use PB.DrawToBitMap to create a Bitmap, which you can save with Bitmap.Save. - If you don't use the Paint event you can't do it; you could draw into an (blank) Image instead. [See here](http://stackoverflow.com/questions/27337825/picturebox-paintevent-with-other-method/27341797#27341797) for the difference! – TaW Jan 14 '15 at 12:24
  • @TaW I tried this and only got the background of the image. It didn't have the signature. – connersz Jan 14 '15 at 12:32
  • DrawToBitMap will include everything: Background, Image and everything you draw __in the Paint event__. but nothing else. where do you draw the signature? – TaW Jan 14 '15 at 12:34
  • @TaW I have posted what I believe writes the signature above, but it is code from the pad manufacturer so I am unfamiliar with it. – connersz Jan 14 '15 at 12:37
  • Hm, it uses a `g_PicGraphics` Graphics object; can you see where and how it is created? – TaW Jan 14 '15 at 12:41
  • 1
    Probably depends upon how `g_PicGraphics` is assigned, then. If it is something equivalent to `CreateGraphics()`, then it is unlikely that `DrawToBitmap()` will work. – DonBoitnott Jan 14 '15 at 12:41
  • @Don: Indeed; if they create and hold on to a reference to a control Graphics then he can't go that way. - In that case he needs to either plug into the drawing code, if that is possible, or use a screen scraping code to capture the control. Plenty of code examples for that; just make sure to get the right part.. – TaW Jan 14 '15 at 12:46
  • Is it possible to screen scrape a windows application? – connersz Jan 14 '15 at 12:49
  • Of course! The only (small) challenge is to set the source Rectangle to the Control. Use ClientToScreen for that.. [See here for an example](http://stackoverflow.com/questions/23187572/take-screenshot-from-window-content-without-border/23187599?s=6|1.0474#23187599) also [this answer](http://stackoverflow.com/questions/24331011/screenshot-method-generates-black-images/24332354#24332354) for one that is restricted to one control! – TaW Jan 14 '15 at 13:01

0 Answers0