关于C#中byte数组转换成图片的问题
这是调用的方法定义:
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadBaseMsg", CharSet = CharSet.Ansi)]
public static extern int Syn_ReadBaseMsg(int iPort, ref byte pucCHMsg, ref uint puiCHMsgLen, ref byte pucPHMsg, ref uint puiPHMsgLen, int iIfOpen);
下面是我定义的参数:
byte[] chmsd=new byte[256];
byte[] phmsg = new byte[1024];
uint chmsglen=0;
uint phmsglen=0;
下面是调用哪个方法:
Syn_ReadBaseMsg(1001, ref chmsd[0], ref chmsglen, ref phmsg[0], ref phmsglen, 0);
MemoryStream ms = new MemoryStream(phmsg);
下面这句话报错说参数无效我该怎么解决呢!加急啊!
Image bmp = Image.FromStream(ms,false,false);
------解决方案--------------------用这个
private void BindingImageSource(byte[] _imgByte)//显示image
{
/*6. 将二进制转化为Imagesource,在Image上显示*/
ImageSource imgSource = ChangeBitmapToBitmapSource(SetByteToBitmap(_imgByte));
}
public static BitmapSource ChangeBitmapToBitmapSource(Draw.Bitmap bmp)
{
BitmapSource returnSource = null;
try
{
returnSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
}
catch
{
returnSource = null;