-1

I am working with a fingerprint module and am able to capture and display the output in a picture box (C#). Now picture.Image is null even though picturebox displays an image. So I am trying to save the picturebox image as bmp and then assign that bmp to the same picturebox so that Picturebox.image is not null.

Here is the code :

Bitmap bmp = new Bitmap(picFP.width, picFP.height);
picFP.DrawToBitmap(bmp, picFP.ClientRectangle);

bmp.Save("path", Imageformat.bmp);
picFP.image = bmp;

Here bitmap image saved is blank. What can be the problem?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
LDin
  • 1
  • 1
  • 1
  • How do you capture and display the output in the picture box? – jhmt Nov 05 '15 at 07:35
  • I am using Nitgen SDK and I am able to capture the fingerprint output and display it in the picturebox. DrawToBitmap() gives me a blank bitmap image – LDin Nov 05 '15 at 07:46
  • I don't have any experience in Nitgen SDK but this question (http://stackoverflow.com/questions/14182487/windowsform-picturebox-image-is-null-even-though-theres-an-image-shown-in-the-f) might be helpful for you. – jhmt Nov 05 '15 at 07:53
  • Thank you, but I have already seen that post. Nitgen is working fine . Its picturebox.DrawToBitmap() which is generating a blank bmp file. – LDin Nov 05 '15 at 08:01
  • How do you bring the captured output into the `PictureBox`? (Also: If you have it, why not save it right away?) - I can't think of another reason than that you may be drawing it onto the `Picturebox` with a `CreateGraphics` object, which is wrong, as usual.. – TaW Nov 05 '15 at 10:42
  • objNitgen=picFP.Handle; objNitgen.capture(); will display the captured output in the picture box. Even though it displays the captured output PicFP.Image returns a null. Hence I need to convert the output displayed to bitmap for futher processing – LDin Nov 05 '15 at 10:52
  • Ah, that explains it. See my updated post! – TaW Nov 05 '15 at 11:02

1 Answers1

1

A PictureBox has three layers it can display and PictureBox.DrawToBitmap will put all three things into the Bitmap:

  • The BackgroundImage
  • The Image
  • Any graphics created in or from the Paint event

If your bitmap comes out black then you have none of the three, or the last you have is all black. From your description it seems as if you can display the image in the PictureBox. So I assume that you don't display it in the right way, probably you do it like this:

using (Graphics G = picFP.CreateGraphics())
       G.DrawImage(yourCapturedImage, ..)

This will not work as it only creates non-persistent graphics. These go away with e.g. each minimize-restore cycle and are not called from the DrawToBitmap call

If you really want to draw it onto the PB's surface use the Paint event! But the more natural choice would be to set the PB's Image directly:

picFP.Image = yourCapturedImage;

Update 1 As you now reveal that you don't display it yourself but simply give the control handle to the external code objNitgen=picFP.Handle; the same applies: It is that Nitgen draws only onto the surface and the result is non-persistent.

In this case the remedy is either

  • Taking a screenshot of the result and then work from that. Here is a post that shows you how to capture a control via screenshot..

  • Or you may want to check if Nitgen will draw into a bitmap directly..

For this you should be to pass it not a handle to the PictureBox but to a Bitmap instead:

private void button_Click(object sender, EventArgs e)
{
    Bitmap bmp = new Bitmap(picFP.ClientSize.Width, picFP.ClientSize.Height);
    Graphics G = Graphics.FromImage(bmp);
    IntPtr dc= G.GetHdc();
    objNitgen = dc;
    objNitgen.capture();
    G.ReleaseHdc(dc);
    pictureBox1.Image = bmp;  // now display..
    bmp.Save(yourfilename);   // .. and/or save
}

Update 2

You noted in a comment that doing a manual screenshot also does not capture the image; so it seems the control handle is only used to overlay it with the image much like video overlays do; if this is the case I doubt you can get at the image without using other, more fitting Nitgen SDK methods.

Community
  • 1
  • 1
TaW
  • 53,122
  • 8
  • 69
  • 111
  • The bitmap image comes out white (blank) . The FP is captured on a button click event which is working absolutely fine. So how do I do this: picFp.image=Image visible in the picturebox – LDin Nov 05 '15 at 11:06
  • To test my assumptions do a minimize-maximize cycle! Does the image persist? - It comes out white from the screenshot code in my link?? – TaW Nov 05 '15 at 11:12
  • See my latest update! The use of DrawToBitmap plus the Paint event didn't work for me but passing out a handle to a bitmap should be possible.. – TaW Nov 05 '15 at 12:20
  • Thanks, I tried your suggestion , the bitmap image saved is black and nothing is displayed in the picture box on button click – LDin Nov 05 '15 at 12:48
  • Hm, are we talking about the latest version using `GetHdc` and `ReleaseHdc` ? – TaW Nov 05 '15 at 12:52
  • I am using VS 2013 and Win8.1 – LDin Nov 05 '15 at 13:05
  • Yeah, well, I meant the latest version of my answer. - Also: Can you cappture the image when you do a regular screenshot (alt-print and paste into a paint program)?? – TaW Nov 05 '15 at 13:12
  • Is it possible that Nitgen may not be able to directly convert to Bitmap ? I may have to convert their data raw output to Bitmap using their SDK ? – LDin Nov 05 '15 at 13:19
  • Well, that could be, but if they can write onto the control the same code ought to write into the bitmap. did you test the manual screen capture? – TaW Nov 05 '15 at 13:23
  • Yeah I am able to do a manual screen capture using printScr. Alt - print again renders the picture box blank – LDin Nov 05 '15 at 13:25
  • Ouch. If the manual screen capture doesn't work it may well be impossible to get at the image without the sdk. – TaW Nov 05 '15 at 13:45