Displaying images

I am struggling with a very basic task. I am trying to display images using the wpf carousel control. I have added the carousel to my project using XAML but now i want to add images to carousel using my C# Codebehind.

what i am doing right now is:
     carouselControl.Items.Add(new CarouselItem() {Content = myImage});

myImage is a variable containing a BitmapImage.

Instead of showing the image i get a text string that says "System.Windows.Media.Imaging.BitmapImage". I´ve tried looking for examples of how to accomplish this simple task but all i can find are examples of how to use XAML for the task.

If anyone can provide a simple helloworld style example of how to display an image in a carousel control using C# i would be very gratefull.

Best regards
Thor W. Giversen


4 Replies

VR Venkateshwaran Ramdoss Syncfusion Team July 2, 2018 07:39 AM UTC

Hi Thor, 
 
Thank you for using Syncfusion Products. 
 
We have prepared a sample for your requirement. In this, we have added the images as a child of ViewBox control and added the ViewBox as a content of the CarouselItem. Please find the code example in the below. 
 
Code example 
 
MainWindow.xaml.cs 
 
Image image = new Image(); 
Image image1 = new Image(); 
Image image2 = new Image(); 
Image image3 = new Image(); 
Image image4 = new Image(); 
Image image5 = new Image(); 
 
BitmapImage bitimg = new BitmapImage(new Uri("/Sample;component/Images/Buchanan.png", UriKind.RelativeOrAbsolute)); 
BitmapImage bitimg1 = new BitmapImage(new Uri("/Sample;component/Images/Callahan.png", UriKind.RelativeOrAbsolute)); 
BitmapImage bitimg2 = new BitmapImage(new Uri("/Sample;component/Images/King.png", UriKind.RelativeOrAbsolute)); 
BitmapImage bitimg3 = new BitmapImage(new Uri("/Sample;component/Images/dodsworth.png", UriKind.RelativeOrAbsolute)); 
BitmapImage bitimg4 = new BitmapImage(new Uri("/Sample;component/Images/Fuller.png", UriKind.RelativeOrAbsolute)); 
BitmapImage bitimg5 = new BitmapImage(new Uri("/Sample;component/Images/Leverling.png", UriKind.RelativeOrAbsolute)); 
 
image.Source = bitimg as ImageSource; 
image1.Source = bitimg1 as ImageSource; 
image2.Source = bitimg2 as ImageSource; 
image3.Source = bitimg3 as ImageSource; 
image4.Source = bitimg4 as ImageSource; 
image5.Source = bitimg5 as ImageSource; 
 
Viewbox viewbox1 = new Viewbox(); 
Viewbox viewbox2 = new Viewbox(); 
Viewbox viewbox3 = new Viewbox(); 
Viewbox viewbox4 = new Viewbox(); 
Viewbox viewbox5 = new Viewbox(); 
Viewbox viewbox6 = new Viewbox(); 
 
viewbox1.Child = image; 
viewbox2.Child = image1; 
viewbox3.Child = image2; 
viewbox4.Child = image3; 
viewbox5.Child = image4; 
viewbox6.Child = image5; 
             
carousel.Items.Add(new CarouselItem() {Content = viewbox1 }); 
carousel.Items.Add(new CarouselItem() { Content = viewbox2 }); 
carousel.Items.Add(new CarouselItem() { Content = viewbox3 }); 
carousel.Items.Add(new CarouselItem() { Content = viewbox4 }); 
carousel.Items.Add(new CarouselItem() { Content = viewbox5 }); 
carousel.Items.Add(new CarouselItem() { Content = viewbox6 }); 
 
 
 
We have prepared a sample for your reference and it can be downloaded from the below link. 
 
 
Please check the above sample and let us know if it is helpful. 
 
Regards, 
Venkateshwaran V.R. 



TW Thor Wium Giversen July 2, 2018 10:57 AM UTC

Works perfectly.

Thank you for your help :)


TW Thor Wium Giversen July 2, 2018 11:22 AM UTC

Is there a collection of C# examples anywhere online ? My work would be a lot easier if i didn´t have to guess at the correct way of doing things based on the XAML examples in your documentation. 


VR Venkateshwaran Ramdoss Syncfusion Team July 3, 2018 05:33 AM UTC

Hi Thor, 
 
Thank you for the update. 
 
You can find the C#/ VB codes using View-Model structure in our documentations in the below section. But we have not created and add the items to carousel only in code behind. 
 
 
Please provide your complete requirements in C# codes, we will help you on this further and we will improve the online documentation section with more code behind [C#/VB] codes. 
 
Regards, 
Venkateshwaran V.R. 


Loader.
Up arrow icon