Image.FromStream:参数无效参数、Image、FromStream

2023-09-02 10:53:37 作者:流年消逝、梦相思

我想从字节数组创建的图像。字节数组是由一个指纹扫描仪​​(CF CaptureFrame方法)创建的。 fwidth是256和fheight为255。

当我运行code以下,我得到System.ArgumentException:参数是无效的。

 昏暗fWidth短
昏暗fHeight短

DFRProxy.DFRProxy.GetImageDimensions(fWidth,fHeight)

昏暗imgBufLength作为整数= CINT(fWidth)* fHeight

昏暗的手指(imgBufLength)为字节

昏暗startCap短= DFRProxy.DFRProxy.StartCapture(0)

昏暗capFrame短= DFRProxy.DFRProxy.CaptureFrame(0,手指,0)


使用MS作为新IO.MemoryStream(手指)
    thisImage = Image.FromStream(毫秒)
结束使用
 

发生在一行的错误

  thisImage = Image.FromStream(MS)
 
ucrtbased.dll 将一个无效参数传递给了将无效参数视为严重错误的函数

字节数组有65280元。我查阅了几个StackOverflow的帖子是与此类似,但没有奏效。我曾尝试设置useEmbeddedColorManagement和validateImageData参数FromStream方法虚实相生,但这并没有解决问题。

你有关于如何纠正的ArgumentException有什么建议?

解决方案

FromStream期待的数据,这些格式之一:

  

管理GDI +还内置连接支持下列文件类型codeRS和去codeRS:

  BMP

GIF

JPEG

PNG

TIFF
 

您的字节数组,我怀疑是不是这些,没有每种格式期望中的元数据或COM pression信息。

您要做的就是创建一个位图对象,并通过读取字节数组中的每个像素,调用SetPixel位图的相应像素。你会最终有一个位图(这是一个图片),有需要的像素。

I am trying to create an image from a byte array. The byte array is created by a fingerprint scanner (cf CaptureFrame method). fwidth is 256 and fheight is 255.

When I run the code below, I get "System.ArgumentException: Parameter is not valid."

Dim fWidth As Short
Dim fHeight As Short

DFRProxy.DFRProxy.GetImageDimensions(fWidth, fHeight)

Dim imgBufLength As Integer = CInt(fWidth) * fHeight

Dim finger(imgBufLength) As Byte

Dim startCap As Short = DFRProxy.DFRProxy.StartCapture(0)

Dim capFrame As Short = DFRProxy.DFRProxy.CaptureFrame(0, finger, 0)


Using ms As New IO.MemoryStream(finger)
    thisImage = Image.FromStream(ms)
End Using

The error occurs at line

thisImage = Image.FromStream(ms)

The byte array has 65280 elements. I have reviewed several StackOverflow postings that are similar to this, but nothing has worked. I have tried setting the useEmbeddedColorManagement and validateImageData parameters for the FromStream method to False and True, but this does not solve the problem.

Do you have any suggestions on how to correct the ArgumentException?

解决方案

FromStream is expecting data in one of these formats:

Managed GDI+ has built-in encoders and decoders that support the following file types:

BMP

GIF

JPEG

PNG

TIFF

Your byte array I suspect is not in these and does not have the metadata or compression information each of these formats expects.

What you want to do is create a Bitmap object and read through each pixel in the byte array, calling SetPixel in the bitmap for the appropriate pixel. You'll end up with a Bitmap (which is an Image) that has the pixels you want.