将图像转换为流 [英] Convert Image to Stream

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

问题描述

我想将图片转换为流并将其作为参数传递给其他函数请帮我这个...

提前感谢

解决方案

public Image GetImageById(int id)

{

//初始化连接。

SqlConnection dbconn = new SqlConnection(connectionstring );



尝试

{

//创建命令并获取图像。

using(SqlCommand command = dbconn.CreateCommand())

{

//设置选择查询并添加带有值的Id参数。

command.CommandText =SELECT [ImageField] FROM [Table1] WHERE [Id] = @Id;

command.Parameters.Add(@ Id,SqlDbType.Int,4).Value = id;



//打开连接。

dbconn.Open();



//超越选择查询。

byte [] imageData =(byte [])command.ExecuteScalar();



//如果我们收到一张图片,请将其退回;否则

//只返回一个空参考值。

if(imageData!= null && imageData.Lenght!= 0)

{

使用(MemoryStream stream = new MemoryStream(imageData))

{

返回Image.FromStream(stream);

}

}

其他

{

//找不到图片。

返回null;

}

}

}

终于

{

dbconn.Close();

}

}






看看这里:

http://stackoverflow.com/a / 1668493 [ ^ ]


  public 图片GetImageById( int  id)
{
// 初始化连接。
SqlConnection dbconn = new SqlConnection( connectionstring);

尝试
{
// 创建命令并获取图像。
使用(SqlCommand command = dbconn.CreateCommand())
{
// 设置选择查询并添加带有值的Id参数。
command.CommandText = SELECT [ImageField] FROM [Table1] WHERE [Id] = @Id;
command.Parameters.Add( @ Id,SqlDbType.Int, 4 )。Value = id;

// 打开连接。
dbconn.Open( );

// 超越选择查询。
byte [] imageData =( byte [])command.ExecuteScalar();

// 如果我们收到了一张图片,请将其退回;否则
// 只返回一个空参考值。
if (imageData!= null && imageData.Lenght!= 0
{
使用(MemoryStream stream = new MemoryStream(imageData))
{
return Image.FromStream(stream);
}
}
else
{
// 找不到图片。
return ;
}
}
}
最后
{
dbconn.Close();
}
}


I want to convert image to stream and pass it as parameter to other function please help me with this...
thanks in advance

解决方案

public Image GetImageById( int id )
{
// Initialize connection.
SqlConnection dbconn = new SqlConnection( "connectionstring" );

try
{
// Create command and fetch image.
using(SqlCommand command = dbconn.CreateCommand())
{
// Set select query and add Id parameter with value.
command.CommandText = "SELECT [ImageField] FROM [Table1] WHERE [Id] = @Id";
command.Parameters.Add("@Id", SqlDbType.Int, 4).Value = id;

// Open the connection.
dbconn.Open();

// Excecute the select query.
byte[] imageData = (byte[])command.ExecuteScalar();

// If we recieved an image, return it; otherwise
// just return a null reference value.
if( imageData != null && imageData.Lenght != 0 )
{
using(MemoryStream stream = new MemoryStream( imageData ))
{
return Image.FromStream( stream );
}
}
else
{
// Image not found.
return null;
}
}
}
finally
{
dbconn.Close();
}
}


Hi,

Have a look here:
http://stackoverflow.com/a/1668493[^]


public Image GetImageById( int id )
{
    // Initialize connection.
    SqlConnection dbconn = new SqlConnection( "connectionstring" );

    try
    {
        // Create command and fetch image.
        using(SqlCommand command = dbconn.CreateCommand())
        {
            // Set select query and add Id parameter with value.
            command.CommandText = "SELECT [ImageField] FROM [Table1] WHERE [Id] = @Id";
            command.Parameters.Add("@Id", SqlDbType.Int, 4).Value = id;
            
            // Open the connection.
            dbconn.Open();
            
            // Excecute the select query.
            byte[] imageData = (byte[])command.ExecuteScalar();
            
            // If we recieved an image, return it; otherwise
            // just return a null reference value.
            if( imageData != null && imageData.Lenght != 0 )
            {            
                using(MemoryStream stream = new MemoryStream( imageData ))
                {
                    return Image.FromStream( stream );
                }
            }
            else
            {
                // Image not found.
                return null;
            }
        }
    }
    finally
    {
        dbconn.Close();
    }
}


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

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