c#
  • image
  • 2012-11-14 105 views 0 likes 
    0

    嗨我试图从数据库中获取一个字节数组,并将其转换为我可以用来从我的.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(插播广告)我得到这个错误System.ArgumentException点:参数是无效的。实际上,我通过查看各种不同的来源获得了这些代码,所以也许有人在这里可以告诉我,如果我做错了什么。

    谢谢!

    +2

    请在[参数化查询(HTTP读了起来:// www.codinghorror.com/blog/2005/04/give-me-parameterized-sql-or-give-me-death.html) –

    +0

    http://bobby-tables.com/ –

    +0

    这就是你需要的:'Image .FromStream(new MemoryStream((byte [])picCommand.ExecuteScalar()));' – Magnus

    回答

    1

    ,你可以尝试这样的事情,它应该为你

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

    这不起作用:( – TerNovi

    0

    在这里工作是一个扩展方法我有一个字节数组转换为位图 在这个例子中,我用我保存数据到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 PARAMS之一定义为以下

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

    这会帮助你了解不同的方法可以做到这一个

    +0

    虽然我的问题不是转换为位图。 – TerNovi

    +0

    是你想转换为图像让我发布一个更好的例子 – MethodMan

    相关问题