如何将GrayScale图像转换为字节数组?

时间:2014-02-18 15:18:14

标签: c# image bytearray

我有灰度图像,我想看到它的byte []无论如何要做到这一点?

我在这里得到灰度图像;

 private void OnImage(NGrayscaleImage nImage)
 {
 }

我想将它发送给这个功能;

 private void SendBiometricInfo(byte[] imageBuffer, int imageWidth, int imageHeight)
 {
 }

有没有这样做?如果是这样,你能帮我解决这个问题吗?

这是我试过的......

private void OnImage(NGrayscaleImage nImage)
{
    OnImageByte(ImageToByte(nImage), nImage.Width, nImage.Height);               
    //However here it says there is an invalid arguments.
}


public static byte[] ImageToByte(Image img)
{
    ImageConverter converter = new ImageConverter();
    return (byte[])converter.ConvertTo(img, typeof(byte[]));
}

void OnImageByte(byte[] imgBuffer, int width, int height)
{
}

1 个答案:

答案 0 :(得分:0)

如果您可以将图片保存为JPG,那么NGrayscaleImage可以继承Image,那么您可以尝试将其保存到内存流中

public byte[] imageToByteArray(System.Drawing.Image image)
{
   using (MemoryStream ms = new MemoryStream())
   {
       image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
       return ms.ToArray();
   }
}

希望它有所帮助。

相关问题