TerNovi
TerNovi

Reputation: 400

byte array to image displayed

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!

Upvotes: 0

Views: 2901

Answers (2)

Rick B
Rick B

Reputation: 1166

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

Upvotes: 0

MethodMan
MethodMan

Reputation: 18843

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

Upvotes: 1

Related Questions