0

Hi I'm trying to get a byte array from a database and convert it to something I can use to display the image from the database in my .aspx page. I am using strictly c#.

Here is my code.

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);

The code compiles and runs but when it gets to the point where it executes the line myImage = Bitmap.FromStream(instream) i get this error System.ArgumentException: Parameter is not valid. I actually got this code from looking at various different sources so maybe someone on here can tell me if I am doing something wrong.

Thanks ahead!

6
  • 2
    Please read up on Parameterized Queries Commented Nov 14, 2012 at 18:22
  • bobby-tables.com Commented Nov 14, 2012 at 18:23
  • This is what you need: Image.FromStream(new MemoryStream((byte[])picCommand.ExecuteScalar())); Commented Nov 14, 2012 at 18:24
  • Consider refactoring your SQL Statement so that you can Avoid things such as Sql Injection.. Dave has good suggestion as well Commented Nov 14, 2012 at 18:27
  • Encoding an image into utf-8 is very unlikely to come to a good end. Utf-8 can only encode text, it is not suitable to encode binary data. Commented Nov 14, 2012 at 18:32

2 Answers 2

1

you can try something like this and it should work for you

public static Image LoadImage(byte[] imageBytes)
{
     Image image = null;
     using (var inStream = new MemoryStream(imageBytes))
     {
        image = Image.FromStream(ms);
     }
     return image;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Here is an extension method I have to convert a byte array to a bitmap In this example I use am saving the data to a Sql Server 2008R2 Database

    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;
    }

I have StudentPic defined as the following

public byte[] StudentPic { get; set; }

I have one of my SQL Params Defined as the following

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

This should help you to understand one of the different ways to do this

2 Comments

My problem is not converting to a bitmap though.
are you wanting to convert to an Image let me post a better example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.