Answered load image from database table

  • 11. dubna 2012 22:29
     
     
    Hello ..
     
    I don't know how I can load image from database table . I am store it with path (with copy and paste)in cell type text .
     
    how I can show it in image control?
     

    I ask my question for the third time , I hope to find answers now from my programmers friends .
     
    Thanks ,Regards

Všechny reakce

  • 11. dubna 2012 22:30
     
     
    Plz I want the code?
  • 11. dubna 2012 22:42
     
     

    the easy way to show is is by using bitmap format.

    this.picturebox1.image = new bitmap(path//write the path here.)


    One word frees us of all the weight and pain of life: that word is love.

  • 12. dubna 2012 6:58
     
     
  • 12. dubna 2012 10:21
     
     
    This not what i want ,i want the code able me to show the image from database table 
  • 12. dubna 2012 13:11
     
     Navržená odpověď Obsahuje kód

    Hi Aishaaa,

    Drag a picturebox on your form and set the image as below:

    picturebox1.image = new bitmap("value of path you fetched from db");


    Regards, http://shwetamannjain.blogspot.com

    • Navržen jako odpověď Shweta Jain 12. dubna 2012 13:11
    •  
  • 12. dubna 2012 18:09
     
     Navržená odpověď Obsahuje kód

    Aishaaa

    Try this code. You can save the images in a temporary folder.

    For Image Control

    string conn = ConfigurationManager.ConnectionStrings["tempstring"].ConnectionString;
                using (SqlConnection connection = new SqlConnection(conn))
                {
                    connection.Open();
                    //get the image from the database
                    SqlDataAdapter dap = new SqlDataAdapter("select image from Demo where id=1", conn);
                    DataSet ds = new DataSet();
                    dap.Fill(ds, "temp");
                    Byte[] abyt = null;
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        //convert the image into bytes
                        abyt = (Byte[])row[0];
                    }
                    //save the image in the folder
                    FileStream FS1 = new FileStream(Server.MapPath("Images/image.jpg"), FileMode.Create);
                    FS1.Write(abyt, 0, abyt.Length);
                    FS1.Close();
                    FS1 = null;
                    //asign the image to the asp:Image control
                    Image1.ImageUrl = "Images/image.jpg";
                }

    For Picturebox Control

    string conn = ConfigurationManager.ConnectionStrings["tempstring"].ConnectionString;
    using (SqlConnection connection = new SqlConnection(conn))
                {
                    connection.Open();
                    //get the image from the database
                    SqlDataAdapter dap = new SqlDataAdapter("select image from Demo where id=1", conn);
                    DataSet ds = new DataSet();
                    dap.Fill(ds, "temp");
                    Byte[] abyt = null;
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        //convert the image into bytes
                        abyt = (Byte[])row[0];
                    }
                    System.IO.MemoryStream stream = new System.IO.MemoryStream(abyt, true);
                    stream.Write(abyt, 0, abyt.Length);
                    pictureBox1.Image = new Bitmap(stream);
                }

    Hope this helps.


    Parth


    • Upravený ParthPatel 12. dubna 2012 18:25 Added Extra Code
    • Navržen jako odpověď ParthPatel 12. dubna 2012 18:25
    • Zrušeno navržení jako odpověď ParthPatel 12. dubna 2012 18:27
    • Navržen jako odpověď ParthPatel 12. dubna 2012 18:27
    •  
  • 12. dubna 2012 22:08
     
     
    i work in wpf under c# i don't have dataset 

    sorry the code is not work !

  • 13. dubna 2012 18:02
    Moderátor
     
     Odpovědět

    Hi aishaaa,
    I think I got what's you mean, the "Database table" you said is the table in database, not the datatable in WPF.
    If what I guess is true, that is to say you need to first know how to connect database in WPF, and get the data from datatabe, right?

    For how to bind database to WPF, please check out below link for your reference:

    http://msdn.microsoft.com/en-us/library/aa480226.aspx

    http://www.codeproject.com/Articles/24192/Simple-Demo-of-Binding-to-a-Database-in-WPF-using

    And once you get the path of image from database, you can use code like what above memeber said to show image:

    this.picturebox1.image = new bitmap(path//write the path here.

    Hope it helps.

    Have a nice day.


    Annabella Luo[MSFT]
    MSDN Community Support | Feedback to us

  • 13. dubna 2012 19:06
     
     

    HI

     i work on wpf under c#


    I don't know how I can load image from database table . I am store it with path (with copy and paste)in cell type text .
     and i want to show the image randomly in 4 image . i want to do game

    how I can show it in 4image control?

     

  • 13. dubna 2012 19:08
     
     
    this my skype name if there any helper programmer

    (aisha.dodi)
  • 18. dubna 2012 17:55
    Moderátor
     
     Odpovědět Obsahuje kód

    Hi aishaaa,

    After you get the data of path from database, you can create bitmapimage as the Image Control's Source base on these path, then in a DispatcherTimer to create a randomly integer between 1 and 4, then in a switch sentence set Image's source base on the created random integer.

    How to create randomly integer in C# for your reference:

    private int RandomNumber(int min, int max)
    {
    Random random = new Random();
    return random.Next(min, max); 
    }

    About DispatcherTimer class for your reference:http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer.aspx 

    Hope it helps.

    Have a nice day.


    Annabella Luo[MSFT]
    MSDN Community Support | Feedback to us