【问题标题】:byte array to image displayed字节数组到显示的图像
【发布时间】:2012-11-15 04:20:35
【问题描述】:

您好,我正在尝试从数据库中获取字节数组并将其转换为可用于在我的 .aspx 页面中显示来自数据库的图像的内容。我使用的是严格的 c#。

这是我的代码。

SqlCommand picCommand = connection.CreateCommand();
picCommand.CommandText = ("SELECT ItemImage FROM Inventory WHERE ItemName = '" +         DropDownList1.SelectedItem.Text + "';");
connection.Open();

object returnPic; 
returnPic = picCommand.ExecuteScalar();//value that is read as the byte array or intended to be read as byte array.


    connection.Close();


    UTF8Encoding utf8 = new UTF8Encoding();
    //where i intend to convert the 
    byte[] image = utf8.GetBytes(returnPic.ToString()); 


    System.Drawing.Image myImage;

    using (MemoryStream inStream = new MemoryStream())
    {
        inStream.Write(image, 0, image.Length);

        myImage = Bitmap.FromStream(inStream);
    }




    this.ItemImageBox.Equals(myImage);

代码编译并运行,但是当它到达执行该行的位置时 myImage = Bitmap.FromStream(instream) 我收到此错误 System.ArgumentException:参数无效。实际上,我是通过查看各种不同的来源获得了这段代码,所以也许这里的人可以告诉我我是否做错了什么。

先谢谢了!

【问题讨论】:

  • 这就是你需要的:Image.FromStream(new MemoryStream((byte[])picCommand.ExecuteScalar()));
  • 考虑重构你的 SQL 语句,这样你就可以避免诸如 Sql Injection 之类的事情。Dave 也有很好的建议
  • 将图像编码为 utf-8 是非常不太可能取得好的结果。 utf-8只能编码文本,不适合编码二进制数据。

标签: c# image


【解决方案1】:

你可以试试这样的东西,它应该适合你

public static Image LoadImage(byte[] imageBytes)
{
     Image image = null;
     using (var inStream = new MemoryStream(imageBytes))
     {
        image = Image.FromStream(ms);
     }
     return image;
}

【讨论】:

    【解决方案2】:

    这是我必须将字节数组转换为位图的扩展方法 在此示例中,我使用将数据保存到 Sql Server 2008R2 数据库

        protected void btnParent1Upload_Click(object sender, EventArgs e)
        {
            ScriptManager.GetCurrent(this).RegisterPostBackControl(this.btnParent1Upload);
            FileUpload FileUpload1 = file_ImageParent1;
            string virtualFolder = "~/UpImages/";
            string physicalFolder = Server.MapPath(virtualFolder);
            FileUpload1.SaveAs(physicalFolder + FileUpload1.FileName);
            lbl_ResultParent1.Text = "Your file " + FileUpload1.FileName + " has been uploaded.";
            Parent1Pic.Visible = true;
            Parent1Pic.ImageUrl = virtualFolder + FileUpload1.FileName;
            byte[] imageBytes = PopulateImageBytes(physicalFolder + FileUpload1.FileName);
            ParentsInfo.Parent1Pic = imageBytes;
            imageBytes = null;
            FileUpload1 = null;
        }
    
        private static byte[] PopulateImageBytes(string p)
        {
            byte[] imageBytes = File.ReadAllBytes(p);
            return imageBytes;
        }
    

    我将 StudentPic 定义如下

    public byte[] StudentPic { get; set; }
    

    我的 SQL 参数之一定义如下

    sqlcmd.Parameters.AddWithValue("@Picture", (object)StudentPic ?? noImage);
    

    这应该可以帮助您了解执行此操作的不同方法之一

    【讨论】:

    • 我的问题是没有转换为位图。
    • 你想转换成图片让我发布一个更好的例子
    猜你喜欢
    • 1970-01-01
    • 2017-01-13
    • 1970-01-01
    • 2019-09-21
    • 2010-12-18
    • 1970-01-01
    • 2013-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多