.net Image.FromStream:参数无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6712557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Image.FromStream: Parameter not valid
提问by blueshift
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.
我正在尝试从字节数组创建图像。字节数组由指纹扫描仪创建(参见 CaptureFrame 方法)。fwidth 为 256,fheight 为 255。
When I run the code below, I get
当我运行下面的代码时,我得到
System.ArgumentException: Parameter is not valid.
System.ArgumentException: 参数无效。
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 useEmbeddedColorManagementand validateImageDataparameters for the FromStream method to False and True, but this does not solve the problem.
字节数组有 65280 个元素。我已经查看了几个与此类似的 StackOverflow 帖子,但没有任何效果。我曾尝试将FromStream 方法的useEmbeddedColorManagement和validateImageData参数设置为 False 和 True,但这并不能解决问题。
Do you have any suggestions on how to correct the ArgumentException?
您对如何纠正 有任何建议ArgumentException吗?
回答by antlersoft
FromStream is expecting data in one of these formats:
FromStream 需要以下格式之一的数据:
Managed GDI+ has built-in encoders and decoders that support the following file types:
- BMP
- GIF
- JPEG
- PNG
- TIFF
Managed GDI+ 具有支持以下文件类型的内置编码器和解码器:
- BMP
- 动图
- JPEG格式
- PNG
- 国际电影节
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.
您想要做的是创建一个 Bitmap 对象并读取字节数组中的每个像素,在位图中为适当的像素调用 SetPixel。你最终会得到一个具有你想要的像素的位图(它是一个图像)。
回答by ggsmartboy
Try the following:
请尝试以下操作:
TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));
Bitmap bitmap1 = (Bitmap)tc.ConvertFrom(byteArray);
回答by mtholen
In addition to >ggsmartboy answer for VB.NET:
除了 VB.NET 的 >ggsmartboy 回答:
On top of module/class/form
在模块/类/表单之上
Imports system.componentmodel
In the code
在代码中
Dim ba As New Byte() 'Make sure you set the byte array to something
Dim tc As TypeConverter = TypeDescriptor.GetConverter(GetType(Bitmap))
Dim bmp As Bitmap = tc.ConvertFrom(ba)
And subsequently:
随后:
PictureBox1.Image = bmp
Cheers
干杯

