字节数组到图像的转换 [英] Byte array to image conversion

查看:50
本文介绍了字节数组到图像的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将字节数组转换为图像.

I want to convert a byte array to an image.

这是我从中获取字节数组的数据库代码:

This is my database code from where I get the byte array:

public void Get_Finger_print()
{
    try
    {
        using (SqlConnection thisConnection = new SqlConnection(@"Data Source=" + System.Environment.MachineName + "\\SQLEXPRESS;Initial Catalog=Image_Scanning;Integrated Security=SSPI "))
        {
            thisConnection.Open();
            string query = "select pic from Image_tbl";// where Name='" + name + "'";
            SqlCommand cmd = new SqlCommand(query, thisConnection);
            byte[] image =(byte[]) cmd.ExecuteScalar();
            Image newImage = byteArrayToImage(image);
            Picture.Image = newImage;
            //return image;
        }
    }
    catch (Exception) { }
    //return null;
}

我的转换代码:

public Image byteArrayToImage(byte[] byteArrayIn)
{
    try
    {
        MemoryStream ms = new MemoryStream(byteArrayIn,0,byteArrayIn.Length);
        ms.Write(byteArrayIn, 0, byteArrayIn.Length);
        returnImage = Image.FromStream(ms,true);//Exception occurs here
    }
    catch { }
    return returnImage;
}

当我到达带有注释的行时,发生以下异常:Parameter is not valid.

When I reach the line with a comment, the following exception occurs: Parameter is not valid.

我该如何解决导致此异常的任何问题?

How can I fix whatever is causing this exception?

推荐答案

您正在写入内存流两次,而且您没有在使用后处理流.您还要求图像解码器应用嵌入式色彩校正.

You are writing to your memory stream twice, also you are not disposing the stream after use. You are also asking the image decoder to apply embedded color correction.

试试这个:

using (var ms = new MemoryStream(byteArrayIn))
{
    return Image.FromStream(ms);
}

这篇关于字节数组到图像的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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