none
how to load image from SQL server RRS feed

  • Question

  • Hi,

    I am have student_photo field which is an Image data type in SQL Server and I want to know how can I load the image in my application..

    Here is what ia m doing:

    public class MyKidsData
    {
        public int student_id { get; set; }
        public Image student_photo { get; set; }
        public string first_name { get; set; }
        public string last_name { get; set; }
        public DateTime date_of_birth { get; set; }
    
        public string full_name
        {
            get
            {
                return first_name + " " + last_name;
            }
        }
    
        public string birthday_arabic
        {
            get
            {
                return date_of_birth.ToString("d MMMM yyyy", new CultureInfo("ar-AE"));
            }
        }
    
        /*
        public string site_image
        {
            get
            {
                return string.Format("https://www.jeccd.ae/gallery/sites/{0}.png", site_guid);
            }
        }
        */
    }
    

    and then..

    sql_connection = new SqlConnection("Server=my.server.com; Database=myDB; User Id=sa; Password=myPassword;");
    sql_connection.Open();
    
    sql_command = new SqlCommand("sp_populate_my_kids", sql_connection);
    sql_command.CommandType = CommandType.StoredProcedure;
    sql_command.Parameters.Add(new SqlParameter("@parent", App.getUserInfo("JafariaUserID").Result));
    SqlDataReader sql_reader = sql_command.ExecuteReader();
    
    ObservableCollection<object> trends = new ObservableCollection<object>();
    
    while (sql_reader.Read())
    {
        MyKidsData data1 = new MyKidsData()
        {
            student_id = Convert.ToInt32(sql_reader["student_id"]),
            student_photo = sql_reader["student_photo"],
            first_name = Convert.ToString(sql_reader["first_name"]),
            last_name = Convert.ToString(sql_reader["last_name"]),
            date_of_birth = Convert.ToDateTime(sql_reader["date_of_birth"])
        };
    
        trends.Add(data1);
    }

    Thanks,

    Jassim


    Thursday, June 27, 2019 11:17 AM

Answers

  • Hi Jassim RahmaSoftnames, 

    Thank you for posting here.

    Based on your question, I make a sample on my side and I have a question to confirm with you.

    What’s type of student_photo in your database?

    If the type is varbinary, you can refer the following code:

            public Image byteArrayToImage(byte[] byteArrayIn)
            {
                MemoryStream ms = new MemoryStream(byteArrayIn);
                Image returnImage = Image.FromStream(ms);
                return returnImage;
            }
            private void button1_Click(object sender, EventArgs e)
            {
                SqlConnection sql_connection = new SqlConnection(@"...");
                sql_connection.Open();
                SqlCommand sql_command = new SqlCommand("select student_photo from student", sql_connection);
               
                SqlDataReader sql_reader = sql_command.ExecuteReader();
    
                ObservableCollection<object> trends = new ObservableCollection<object>();
    
                while (sql_reader.Read())
                {
                    MyKidsData data1 = new MyKidsData()
                    {
                        student_photo = byteArrayToImage((byte[])sql_reader["student_photo"]),  
                    };
    
                    pictureBox1.Image = data1.student_photo;
                    trends.Add(data1);
                }
            }

    Result of my test:

    If not, please show more details about the type of student_photo in your database.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, June 28, 2019 9:09 AM
    Moderator

All replies

  • Hi Jassim RahmaSoftnames, 

    Thank you for posting here.

    Based on your question, I make a sample on my side and I have a question to confirm with you.

    What’s type of student_photo in your database?

    If the type is varbinary, you can refer the following code:

            public Image byteArrayToImage(byte[] byteArrayIn)
            {
                MemoryStream ms = new MemoryStream(byteArrayIn);
                Image returnImage = Image.FromStream(ms);
                return returnImage;
            }
            private void button1_Click(object sender, EventArgs e)
            {
                SqlConnection sql_connection = new SqlConnection(@"...");
                sql_connection.Open();
                SqlCommand sql_command = new SqlCommand("select student_photo from student", sql_connection);
               
                SqlDataReader sql_reader = sql_command.ExecuteReader();
    
                ObservableCollection<object> trends = new ObservableCollection<object>();
    
                while (sql_reader.Read())
                {
                    MyKidsData data1 = new MyKidsData()
                    {
                        student_photo = byteArrayToImage((byte[])sql_reader["student_photo"]),  
                    };
    
                    pictureBox1.Image = data1.student_photo;
                    trends.Add(data1);
                }
            }

    Result of my test:

    If not, please show more details about the type of student_photo in your database.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, June 28, 2019 9:09 AM
    Moderator
  • It's perfectly working in my WinForm application. Thank you.

    How can I also use this in UWP?

    Because they use ImageSource and I get an error when trying it:

    'Image' does not contain a definition for 'FromStream'

    On this line:

    Image returnImage = Image.FromStream(ms);


    The code is the same but in UWP I am using:

    MyKidPhoto.Source = byteArrayToImage((byte[])sql_reader["student_photo"]),


    Saturday, June 29, 2019 11:38 AM
  • Hi Jassim AI Rahma,

    According to your description,it will be more appropriate to ask your question in UWP Forums.

    The CLR Forum discuss and ask questions about .NET Framework Base Classes (BCL). Also discuss all the other Microsoft libraries that are built on or extend the .NET Framework, including MEF, Charting Controls, CardSpace, WIF, POS, Transactions.

    Thank you for your understanding.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 1, 2019 9:25 AM
    Moderator