Click here to Skip to main content
15,882,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi I Working On WPF Project And Need To Show Image From Sqlite In Canvas.


C#
Int32 i = 1;
                if (i > 0)
                {
                    DataRow dataRow = helper.DataSet.Tables[0].Rows[i - 1];
                    //Get Bytes From DataBase
                    byte[] imageBytes = (byte[])dataRow[8];
                    MemoryStream ms = new MemoryStream(imageBytes);

                    System.Drawing.Bitmap bmap = new System.Drawing.Bitmap(ms);
                    
                    IntPtr hBitmap = bmap.GetHbitmap();
                    Image MyImg = New Image();
                    //Convert Bitmap To Image
                    MyImg.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                   
    
                    mycanvas.Children.Clear();
                    RemoveLogicalChild(MyImg);
                    mycanvas.Children.Add(MyImg);


But At mycanvas.Children.Add(MyImg)I Get Error "Specified element is already the logical child of another element. Disconnect it first."

Can Anyone Tell Me How I Can Fix This Problem?

Regards
Posted
Comments
Raul Iloc 24-Jun-14 8:45am    
Could you show us also the code of your method "RemoveLogicalChild()", because maybe the problem is generated from this method!

Hi Try this

C#
MemoryStream Ms = new MemoryStream();
System.Drawing.Bitmap ObjBitmap = null;
ObjBitmap = //Load your Image;
ObjBitmap.Save(Ms, System.Drawing.Imaging.ImageFormat.Bmp);
Ms.Position = 0;
BitmapImage ObjBitmapImage = new BitmapImage();
ObjBitmapImage.BeginInit();
ObjBitmapImage.StreamSource = Ms;
ObjBitmapImage.EndInit();
ImageControl.Source = ObjBitmapImage;
</pre>
 
Share this answer
 
Here Is My Codes I Get Image Byte Array From My DataBase And Convert It To Bitmap And When I Want Show It On Canvas I get This Error

Is There Other Way To I Convert Bitmap To Image And Show It In Canvas Box?

C#
private void comboBox1_SelectionChanged(object sender,SelectionChangedEventArgs e)
        { 
            selected_table_name = comboBox1.SelectedItem.ToString();
            string connectionString = "Data Source=C:\\Gallery\\Appdb.sqlite";

            // Determin the DataAdapter = CommandText + Connection
            string commandText = @"SELECT * FROM " + selected_table_name + " ORDER BY ID";

            // Make a new object
            helper = new dBHelper(connectionString);

            // Load the data
            if (helper.Load(commandText, "") == true)
            {
                // Show the data in the datagridview
                dataGridViewImageList.ItemsSource = helper.DataSet.Tables[0].DefaultView;
                //System.Windows.Visibility.Visible
                dataGridViewImageList.Columns[8].Visibility = System.Windows.Visibility.Collapsed;
                dataGridViewImageList.Columns[6].Visibility = System.Windows.Visibility.Collapsed;
                Int32 i = 1;
                if (i > 0)
                {
                    DataRow dataRow = helper.DataSet.Tables[0].Rows[i - 1];
                    //Get Bytes From DataBase
                    byte[] imageBytes = (byte[])dataRow[8];
                    MemoryStream ms = new MemoryStream(imageBytes);

                    System.Drawing.Bitmap bmap = new System.Drawing.Bitmap(ms);
                    
                    IntPtr hBitmap = bmap.GetHbitmap();
                    //Convert Bitmap To Image
                    MyImg.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                   
    
                    mycanvas.Children.Clear();
                    RemoveLogicalChild(MyImg);
                    mycanvas.Children.Add(MyImg);
                    
                }
               
            }
       
        }


Regards
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900