简体   繁体   English

如何将jpg字节数组转换为图像并在PictureBox中显示?

[英]How to convert jpg byte array to image and display in picturebox?

I am a newbie to mono C# programming in raspbian os (pi3 b). 我是raspbian os(pi3 b)中Mono C#编程的新手。 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. 我已经从此github链接中获取了C#mono中指纹扫描仪应用程序的示例代码,现在我在pi3 b板下的Raspbian os中运行相同的应用程序。

Now after scanning the user finger image, I want to display into my winform PictureBox. 现在,在扫描用户手指图像之后,我想显示到我的winform PictureBox中。

once the application scans each finger then it will send the byte[] to the UI using the below callback method. 一旦应用程序扫描了每个手指,它将使用以下回调方法将byte []发送到UI。

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). 我是raspbian os(pi3 b)中Mono C#编程的新手。 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. 我已经用C#编写了一个指纹扫描仪应用程序,并使用了mono,在pi3板下的Raspbian操作系统中运行了相同的应用程序。

Now after scanning the user finger image, I want to display into my PictureBox. 现在,在扫描用户手指图像之后,我想显示到我的PictureBox中。

once the lib scans each finger then it will send the byte[] to the UI using the below callback method. 库扫描完每个手指后,它将使用以下回调方法将byte []发送到UI。

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] 找到空引用或无效值[GDI +状态:InvalidParameter]

so how can I solve this issue and display the image file into my application? 那么我该如何解决此问题并将图像文件显示到我的应用程序中呢?

Thanks 谢谢

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. 新的Windows窗体项目,在窗体中添加一个图片框,将test.jpg文件复制到Debug目录中,并使用上面的代码。 No problem. 没问题。 Image was loaded. 图片已加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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