将字节数组转换为图像时 - 出现“参数无效”的错误 [英] When converting byte array to image - error coming as "parameter is not valid"

查看:96
本文介绍了将字节数组转换为图像时 - 出现“参数无效”的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个用户可以通过网络摄像头拍摄照片的项目。



捕获后,它将保存在一个文件夹中。



但是在这一行:错误来了:(参数无效。)



i am working on a project where user can capture photo through webcam.

after capture, it will save in a folder.

but in this line : error coming as : (Parameter is not valid.)

Dim Captured_image As System.Drawing.Image = System.Drawing.Image.FromStream(ms, False, True)





请帮助



我尝试过:





please help

What I have tried:

Using ms As New MemoryStream(bytes)
         ms.Write(bytes, 0, bytes.Length)
         Dim Captured_image As System.Drawing.Image = System.Drawing.Image.FromStream(ms, False, True)

         Dim SaveTo As String = Convert.ToString(path) & Session("image_name") + ".png"
         Captured_image.Save(SaveTo, Captured_image.RawFormat)


         Session("image_url") = Convert.ToString("~/Captures/") & Session("image_name") + ".png"

     End Using

推荐答案

您正在使用 MemoryStream(Bytes [])构造函数。这将从传递的数组创建一个流。此后,您将再次将相同的数组写入流。因此,您的流包含图像两次并且 System.Drawing.Image.FromStream()失败,因为它仅支持包含单个图像的流。



你必须省略写电话

You are using the MemoryStream(Bytes[]) constructor. That will create a stream from the passed array. Thereafter you are writing the same array again to the stream. So your stream contains the image two times and System.Drawing.Image.FromStream() fails because it supports only streams containing a single image.

You have to omit the write call
Using ms As New MemoryStream(bytes)
    'ms.Write(bytes, 0, bytes.Length)

或使用不带参数的构造函数

or use the the contructor without arguments

Using ms As New MemoryStream()
    ms.Write(bytes, 0, bytes.Length)


这篇关于将字节数组转换为图像时 - 出现“参数无效”的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆