New to Telerik UI for WinForms? Download free 30-day trial

ImagePrimitive

Use the ImagePrimitive class whenever you need to draw an image as part of rendering a control. The properties specific to displaying images are:

  • Image: A Windows Forms standard Image object.

  • ImageKey: The key of an image in an ImageList control.

  • ImageIndex: The index of an image in an ImageList control.

  • ImageScaling: Either None or ScaleToFit controls whether the image should be scaled to fit the available space.

  • SvgImage: Gets or sets the SVG image.

The ImageList property is introduced in the RadControl class.

The example below loads a series of images from a directory and creates an ImagePrimitive for each.

tpf-primitives-imageprimitive 001

Creating an ImagePrimitive

public class MyImagePrimitiveElement : RadElement
{
    protected override void CreateChildElements()
    {
        WrapLayoutPanel layoutPanel = new WrapLayoutPanel();
        layoutPanel.Orientation = System.Windows.Forms.Orientation.Horizontal;
        string myPicturesPath = @"C:\Users\Public\Pictures\Sample Pictures";
        string[] files = Directory.GetFiles(myPicturesPath, "*.jpg");
        foreach (string path in files)
        {
            StackLayoutPanel panel = new StackLayoutPanel();
            panel.Orientation = System.Windows.Forms.Orientation.Vertical;
            panel.Margin = new System.Windows.Forms.Padding(3);
            ImagePrimitive imagePrimitive = new ImagePrimitive();
            imagePrimitive.Image = Image.FromFile(path).GetThumbnailImage(110, 85, null, IntPtr.Zero);
            imagePrimitive.MinSize = new Size(100, 85);
            panel.Children.Add(imagePrimitive);
            TextPrimitive textPrimitive = new TextPrimitive();
            textPrimitive.Font = new Font(textPrimitive.Font.FontFamily, 9.25f, FontStyle.Bold);
            textPrimitive.Text = Path.GetFileName(path);
            textPrimitive.ForeColor = Color.DarkGray;
            panel.Children.Add(textPrimitive);
            layoutPanel.Children.Add(panel);
        }
        this.Children.Add(layoutPanel);
        base.CreateChildElements();
    }
}