1

I am a newbie to mono C# programming in raspbian os (pi3 b). I have taken a sample code for fingerprint scanner application in C# mono from this github link, Now I am running the same application in Raspbian os under the pi3 b board.

Now after scanning the user finger image, I want to display into my winform PictureBox.

once the application scans each finger then it will send the byte[] to the UI using the below callback method.

private void FingerPrintlib_OnEnrollImageResult(byte[] enrollImage, int count)
    {
        //lblinstruction.Invoke((MethodInvoker)delegate
        //{
        //    lblinstruction.Visible = false;
        //});

        if (count == 0)
        {
            pictureBox4.Invoke((MethodInvoker)delegate
            {
                //pictureBox4.Image = byteArrayToImage(enrollImage);
                pictureBox4.SizeMode = PictureBoxSizeMode.StretchImage;
            });
        }
        else
        {
            pictureBox5.Invoke((MethodInvoker)delegate
            {
                //pictureBox5.Image = byteArrayToImage(enrollImage);
                pictureBox5.SizeMode = PictureBoxSizeMode.StretchImage;
            });
        }
    }

I am a newbie to mono C# programming in raspbian os (pi3 b). I have written a fingerprint scanner application in C# and using mono, I am running the same application in Raspbian os under the pi3 board.

Now after scanning the user finger image, I want to display into my PictureBox.

once the lib scans each finger then it will send the byte[] to the UI using the below callback method.

private void FingerPrintlib_OnEnrollImageResult(byte[] enrollImage, int count)
{
    //lblinstruction.Invoke((MethodInvoker)delegate
    //{
    //    lblinstruction.Visible = false;
    //});

    if (count == 0)
    {
        pictureBox4.Invoke((MethodInvoker)delegate
        {
            //pictureBox4.Image = byteArrayToImage(enrollImage);
            pictureBox4.SizeMode = PictureBoxSizeMode.StretchImage;
        });
    }
    else
    {
        pictureBox5.Invoke((MethodInvoker)delegate
        {
            //pictureBox5.Image = byteArrayToImage(enrollImage);
            pictureBox5.SizeMode = PictureBoxSizeMode.StretchImage;
        });
    }
}


 Image byteArrayToImage(byte[] byteArrayIn) 
{
 try { MemoryStream ms = new MemoryStream(byteArrayIn); 
Image returnImage = Image.FromStream(ms); 
return returnImage;
 } 
catch (Exception ex)
 { 
   MessageBox.Show(ex.Message); 
 } 
   return null; 
}

when I execute the above code I am getting an exception like

A null reference or invalid value was found [GDI+ status: InvalidParameter]

so how can I solve this issue and display the image file into my application?

Thanks

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
SaddamBinSyed
  • 553
  • 3
  • 17
  • On what line you get the error? – Julo Sep 23 '18 at 04:57
  • Where exactly are you getting the error. – Hursey Sep 23 '18 at 05:00
  • @Julo,Hursey, The libfprint-cs is saving the finger image file automatically after capturing the finger image, so with the use of savedImgpath filename, I am reading the image using File.ReadLines() and then converting the byte[] into the image using above C# code (above post) method , Inside the byteArrayToImage() I am getting this exception. – SaddamBinSyed Sep 23 '18 at 05:11
  • I do not see any problem in the conversion `byte[]` --> `Image` you use. This can mean, that the error is on a different place or in the converted data. I have no idea how do you load the image. I can not imagine how do you use `File.ReadLines()` to read a binary _(image)_ file. The problem may be there _(in the source of `byte[]`)_. – Julo Sep 23 '18 at 05:19
  • @Julo, Could you please advise me how to display the saved image into the PictureBox?. In C# I do like, pbx.Image = Image.FromFile(path); – SaddamBinSyed Sep 23 '18 at 05:25
  • 2
    You can use [`File.ReadAllBytes()`](https://learn.microsoft.com/en-us/dotnet/api/system.io.file.readallbytes?view=netframework-4.7.2) in stead of `File.ReadLines()`. See example [here](https://stackoverflow.com/a/2030865/2826535). – Julo Sep 23 '18 at 05:29
  • @Julo, Sorry to inform that , I am already using the ReadBytes() instead of ReadLines(). But the output remians same – SaddamBinSyed Sep 23 '18 at 05:46
  • Also I am surprising why I am strange error while soing simple operation. – SaddamBinSyed Sep 23 '18 at 05:47
  • Then the last option I can think of is, that the image is in a format, that is not supported by the framework. – Julo Sep 23 '18 at 05:59
  • @Julo, I have tried this too, File.ReadAllBytes("enroll_" + enrollCount + ".jpg"); But no change – SaddamBinSyed Sep 23 '18 at 06:07
  • Are you sure that, at this stage, you're not reading the `PGM` format? That library has an utility to convert the native `PGM` format to GDI+ `Bitmap` (`PgmFormatReader` class, `Read(byte[])` method). – Jimi Sep 23 '18 at 17:38
  • @Jimi, Thanks for letting me know, I will try that and will post the result here – SaddamBinSyed Sep 24 '18 at 06:41

1 Answers1

0

The code below works just fine:

using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace ImageLoad
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      byte[] data = File.ReadAllBytes("test.jpg");
      this.pictureBox1.Image = GetImage(data);
    }

    private static Image GetImage(byte[] data)
    {
      using (MemoryStream ms = new MemoryStream(data))
      {
        return (Image.FromStream(ms));
      }
    }
  }
}

New Windows form project, add a picture box to form, copy test.jpg file to Debug directory and use code above. No problem. Image was loaded.

Julo
  • 1,102
  • 1
  • 11
  • 19
  • Did you run at the Linux side or Windows? – SaddamBinSyed Sep 23 '18 at 06:22
  • Unfortunately I do not have Raspberry Pi at home. And since I'm now in Windows, I tested this only in Windows and Ubuntu _(Mono JIT compiler version 4.2.1 (Debian 4.2.1.102+dfsg2-7ubuntu4))_ in VirtualBox. It works in both without a problem. – Julo Sep 23 '18 at 06:31