c# - 在图像控件中显示 byte[]

标签 c# sql asp.net image

因此,我从存储图像的数据库中收集 varbinary(MAX) 值。 它被转换为 byte[],然后目的是在图像控件中显示它。

这是我从数据库读取的位置

        public TemplateData(SqlDataReader dr)
    {
        initialiseData();
        if (dr.HasRows)
        {

            Logo = (byte[])dr["Logo"];

            //Logo = dr["Logo"].ToString();
            TemplateId = dr["TemplateId"].ToString();
            Comment = dr["Comment"].ToString();
            SchemeCode = dr["SchemeCode"].ToString();
            Version = dr["Version"].ToString();
        }
    }

这是值显示到相应控件中的位置

        protected void ddSchemeCode_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddSchemeCode.SelectedIndex > 0)
        {
            // Existing Data to load from database
            TemplateData temp = DataClass.ReturnData(ddSchemeCode.SelectedItem.Text);
            if (temp != null)
            {
                txtVersion.Text = temp.Version;
                txtComment.Text = temp.Comment;
                txtSchemeCode.Text = temp.SchemeCode;
                txtTemplateId.Text = temp.TemplateId;
                img.Src = temp.Logo;
            }

所以目前我将一个 byte[] 传递到图像控件的源中,它就像一个字符串。我尝试使用 Convert.ToBase64String(Logo)ToString(Logo) 将其转换为字符串,但这些不起作用。

非常感谢任何帮助。干杯,伙计们,姑娘们。

最佳答案

尝试将字节数组转换为图像并将其分配给图片框,如下所示,

        try
        {
            using (MemoryStream mStream = new MemoryStream())
            {
                // where pData is your byte array
                mStream.Write(pData, 0, Convert.ToInt32(pData.Length));
                Image originalImage = Image.FromStream(mStream);
                picBox.Image = originalImage;
            }
        }
        catch (Exception ex)
        {
        }

希望有帮助。

关于c# - 在图像控件中显示 byte[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30374053/

相关文章:

c# - 重用UDP服务器中的流

mysql - 以下 sql 命令出错,即错误 1005 : Can't create table (errno: 150)

sql - mysql中的unix时间

javascript - event.which 的值在按下 ESC 时 keyup 事件为 0

c# - 使用 EntityDataSource 和 WHERE 过滤数据

c# - 如何在 .NET 中将 POST 请求内容保存为文件

c# - Windows Phone 7 中的媒体播放器

c# - 如何在 WCF 服务中将接口(interface)作为参数传递?

php - 如何使用准备好的语句制作适当的 mysqli 扩展类?

asp.net - 使用用户时间在 GridView 中显示 DateTime