vb.net 如何将 System.Drawing.Image 转换为字节数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12961144/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:21:46  来源:igfitidea点击:

How to Convert System.Drawing.Image to Byte Array?

vb.netimagebytearray

提问by PaShKa

Hi I'm trying to convert image to byte array to pass it into sql as byte(). Im trying to use Image Converter but it keeps failing

嗨,我正在尝试将图像转换为字节数组,以将其作为 byte() 传递给 sql。我正在尝试使用 Image Converter,但它一直失败

Dim converter As New ImageConverter
nRow.Signature = converter.ConvertTo(imgSignature, TypeOf(Byte())

the error I keep getting is byte is a type not expression

我不断收到的错误是字节是一种类型而不是表达式

回答by Reed Copsey

You can use a MemoryStream. By saving the image into a MemoryStream, you can get the byte array of data from the image:

您可以使用一个MemoryStream. 通过将图像保存到 a 中MemoryStream,您可以从图像中获取数据的字节数组:

Dim ms = new MemoryStream()
imgSegnature.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg) ' Use appropriate format here
Dim bytes = ms.ToArray()

回答by Hans Passant

The VB.NET TypeOf operator doesn't do what you think it does. Somewhat confusing perhaps due to the C# typeofoperator. The VB.NET equivalent is the GetType() function. This works fine:

VB.NET TypeOf 操作符并没有像你想象的那样做。可能由于 C#typeof运算符而有些混乱。VB.NET 等效项是 GetType() 函数。这工作正常:

Dim converter As New ImageConverter
nRow.Signature = converter.ConvertTo(imgSignature, GetType(Byte()))

The type converter uses a MemoryStream to make the conversion, using the PNG image format.

类型转换器使用 MemoryStream 进行转换,使用 PNG 图像格式。