简体   繁体   中英

Convert PictureBox image to bitmap

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?

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 eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM