please help C# loading images from access database.

hey guys,

 

I am trying to place an image on a panel inside my form.

I have to do this dynamically for this project I am working on.

I have a column in my database titled file. which lists the file path to my image "cba.jpg"

my database is called Buttons.mdb  my image i want is cba.jpg. i want it read in from my access database>the file column.

 

What am I screwing up

below is my code. note i have tried several different ways.

I need some advice and guidance on how to do it.

 

here's my code:

 

Thank you so much for any hint/help !!!


 

private void Form1_Load(object sender, EventArgs e)

{

//creates empty ButID array and puts buttons into each position

Button[] ButID = new Button[15];

ButID[0] = button1;

ButID[1] = button2;

ButID[2] = button3;

ButID[3] = button4;

ButID[4] = button5;

ButID[5] = button6;

ButIDDevil = button7;

ButID[7] = button8;

/* ButIDMusic = button9;

ButID[9] = button10;

ButID[10] = button11;

ButID[11] = button12;

ButID[12] = button13;

ButID[13] = button14;

ButID[14] = button15;*/

//creates label array

Label[] lblID = new Label[15];

lblID[0] = lbl1;

/*lblID[1] = lbl2;

lblID[2] = lbl3;

lblID[3] = lbl4;

lblID[4] = lbl5;

lblID[5] = lbl6;

lblIDDevil = lbl7;

lblID[7] = lbl8;*/

/*lblIDMusic = lbl9;

lblID[9] = lbl10;

lblID[10] = lbl11;

lblID[11] = lbl12;

lblID[12] = lbl13;

lblID[13] = lbl14;

lblID[14] = lbl15;*/

// Images[] ImageList = new Images[5];

// Images[0] = Image.FromFile(cba.jpg);

string strConn, strSQL;

int idcount = 0;

// You should replace the bold image

// in the sample below with an icon of your own choosing.

// Note the escape character used (@) when specifying the path.

pic1.Visible = true;

//pictureBox1.Image = Image.FromFile(@"cba.JPG");

//Image.FromFile(

// pictureBox1.BackgroundImage.

strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\KIOSK PROJECT\\buttons.mdb";

OleDbConnection conn = new OleDbConnection(strConn);

conn.Open();

strSQL = "SELECT ID, title, locationx, locationy, width, height, font, fontsize, type FROM Buttons";

OleDbCommand cmd = new OleDbCommand(strSQL, conn);

OleDbDataReader rdr = cmd.ExecuteReader();

try

{

while (rdr.Read())

{

if (ButID[idcount] != null)

{

ButID[idcount].Text = rdr["title"].ToString();

ButID[idcount].Left = Convert.ToInt32(rdr["locationx"]);

ButID[idcount].Top = Convert.ToInt32(rdr["locationy"]);

ButID[idcount].Width = Convert.ToInt32(rdr["width"]);

ButID[idcount].Height = Convert.ToInt32(rdr["height"]);

// pic1.BackgroundImage = Convert(rdr["file"]); // this has never worked for me.wont work with any kind of parsing or converting.

// lbl

// Image.FromFile(image1);

/* if (rdr["title"].ToString() == "empty")

{

break;

}

*/

// ButID[idcount].Visible = true;

idcount++;

}

}

}

catch(Exception ex)

{

MessageBox.Show("An error occurred while reading from the database");

}

//pic1.BackgroundImage = Image.source = "c:\\";

 

 

 

rdr.Close();

conn.Close();

}

 

 

Thanks again !!



Answer this question

please help C# loading images from access database.

  • DonLovesDogs

    Please make sure the path exist before loading the image. Use an extra check to do this:


    String filepath = (String)rdr["file"];

    if( !File.Exists( filepath ) )
    {
    throw new Exception( "File doesn't exist anymore." );
    }

    Image image = Image.FromFile( filepath );
    pic1.BackgroundImage = image;



  • Rishikesh

    If the file column contains the path to the file, you do this;

    pic1.BackgroundImage = Image.FromFile(rdr["file"].ToString());




  • musado

    i tried:

    String filepath = (String)rdr["file"];
    Image image = Image.FromFile(filepath);
    pic1.BackgroundImage = image;

    still nada it gets caught by the try catch block,,

    what am i messing up here
    thx again

  • RustyP

    You can use the Image.FromFile method to create an image object from a specified file. Of you don't store the full path, don't forget to make the path relative!

    Here is a little example:

    String filepath = (String)rdr["file"];
    Image image = Image.FromFile( filepath );
    pic1.BackgroundImage = image;

     



  • James Wyatt

    PJ. van de Sande,

    I could get it working with this:

    String filepath = "C:\\KIOSK PROJECT\\images\\cba.jpg";


    if (!File.Exists(filepath))

    {
    MessageBox.Show("File doesn't exist anymore.");
    }

    Image image = Image.FromFile(filepath);
    pic1.BackgroundImage = image;

    but i couldnt get it to work with what you gave me or with:
    String filepath = (string)(rdr["file"]);
    I have tried to put this in access:
    C:\\KIOSK PROJECT\\images\\cba.jpg
    and C:\KIOSK PROJECT\images\cba.jpg

    still nada..
    what am i missing
    thanks again for your help
    -Felix

  • yaduo79

    when i tried

    pic1.BackgroundImage = Image.FromFile(rdr["file"].ToString());

    it got caught in the try & catch block
    i debugged and it said image was null.

    -Felix

  • please help C# loading images from access database.