SNIPPETBANK.NET

C# - Image Slider Control


This Windows control can be used to view images in a slide show with captions. The control is configurable with a few useful settings. The list below contains a set of properties which can be used to configure the ImageSlider control.

Properties 

  • CaptionHeight - Sets the height of the caption area.
  • CaptionTextLeft - Sets the left position of the caption text relative to the caption area.
  • CaptionBackgrounColor - Sets the caption background color.
  • CaptionOpacity - Sets the caption background opacity.
  • CaptionAnimationSpeed - Sets the scroll speed of the caption text.
  • Animation - Boolean value to turn animation on or off.
  • LeftButton - Returns the left navigation button, so that properties of the button can be configured.
  • RightButton - Returns the right navigation button, so that properties of the button can be configured.

Methods

  • AddImage(string path)
  • AddImage(string path, string caption)
  • AddImage(string path, string caption, Color captionBackgroundColor)
  • AddImage(Image img)
  • AddImage(Image img, string caption)
  • AddImage(Image img, string caption, Color captionBackgroundColor)

Using The Control

  1. /** Create instance of ImageSlider **/  
  2.  
  3. imageSlider1.CaptionOpacity = 200;  
  4.  
  5. imageSlider1.AddImage("Chrysanthemum.jpg""A caption for the image goes here"Color.Maroon);  
  6. imageSlider1.AddImage("Desert.jpg""What an amazing photo."Color.Gold);  
  7. imageSlider1.AddImage("Hydrangeas.jpg""For me?...blush blush"Color.LimeGreen);  
  8. imageSlider1.AddImage("Jellyfish.jpg""So... what sort of fish are you again?"Color.Orange);  
  9. imageSlider1.AddImage("Koala.jpg""Will you be my friend?"Color.Gray);  
  10. imageSlider1.AddImage("Lighthouse.jpg""Must be a greate view from up there");  
  11. imageSlider1.AddImage("Penguins.jpg""Fish anyone?"Color.Navy);  
  12. imageSlider1.AddImage(Image.FromFile("Tulips.jpg")"You cant go wrong with tulips"Color.LightSkyBlue)
Download

ImageSlider.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Drawing;  
  4. using System.Drawing.Drawing2D;  
  5. using System.Text;  
  6. using System.Windows.Forms;  
  7.  
  8. class ImageSlider : Panel  
  9. {  
  10.     Timer _timer;  
  11.     int _captionTextLeft = 20;  
  12.     int _captionPosX = 20;  
  13.     int _pageIndex = 0;  
  14.  
  15.     protected List<Image> _imageList = new List<Image>();  
  16.     protected List<string> _captionList = new List<string>();  
  17.     protected List<Color> _captionBgColor = new List<Color>();  
  18.  
  19.     xButton leftButton;  
  20.     xButton rightButton;  
  21.  
  22.     public ImageSlider()  
  23.     {  
  24.         this.Animation = true;  
  25.         this.CaptionAnimationSpeed = 50;  
  26.         this.CaptionTextLeft = 20;  
  27.         this.CaptionHeight = 50;  
  28.         this.CaptionBackgrounColor = Color.Black;  
  29.         this.CaptionOpacity = 100;  
  30.  
  31.         leftButton = new xButton();  
  32.         leftButton.Text = "<";  
  33.         leftButton.Click += new EventHandler(leftButton_Click);  
  34.  
  35.         rightButton = new xButton();  
  36.         rightButton.Text = ">";  
  37.         rightButton.Click += new EventHandler(rightButton_Click);  
  38.  
  39.         this.Resize += ImageSlider_Resize;  
  40.  
  41.         this.Controls.Add(leftButton);  
  42.         this.Controls.Add(rightButton);  
  43.     }  
  44.  
  45.     void ImageSlider_Resize(object sender, EventArgs e)  
  46.     {  
  47.         leftButton.Location = new Point(0, (this.Height / 2) - (leftButton.Height / 2));  
  48.         rightButton.Location = new Point(this.Width - rightButton.Width, (this.Height / 2) - (rightButton.Height / 2));  
  49.     }  
  50.  
  51.     void leftButton_Click(object sender, EventArgs e)  
  52.     {  
  53.  
  54.         if (_pageIndex > 0)  
  55.         {  
  56.             --_pageIndex;  
  57.         }  
  58.         else  
  59.         {  
  60.             _pageIndex = _imageList.Count - 1;  
  61.         }  
  62.  
  63.         if (Animation)  
  64.         {  
  65.             _captionPosX = this.Width;  
  66.             this.DoubleBuffered = true;  
  67.  
  68.             _timer = new Timer();  
  69.             _timer.Interval = 1;  
  70.             _timer.Tick += new EventHandler(_timer_Tick);  
  71.             _timer.Start();  
  72.         }  
  73.         else  
  74.         {  
  75.             _captionPosX = _captionTextLeft;  
  76.             this.Invalidate();  
  77.         }  
  78.     }  
  79.  
  80.     void rightButton_Click(object sender, EventArgs e)  
  81.     {  
  82.  
  83.         if (_pageIndex < _imageList.Count - 1)  
  84.         {  
  85.             ++_pageIndex;  
  86.         }  
  87.         else  
  88.         {  
  89.             _pageIndex = 0;  
  90.         }  
  91.  
  92.         if (Animation)  
  93.         {  
  94.             _captionPosX = this.Width;  
  95.             DoubleBuffered = true;  
  96.  
  97.             _timer = new Timer();  
  98.             _timer.Interval = 1;  
  99.             _timer.Tick += new EventHandler(_timer_Tick);  
  100.             _timer.Start();  
  101.         }  
  102.         else  
  103.         {  
  104.             _captionPosX = _captionTextLeft;  
  105.             this.Invalidate();  
  106.         }  
  107.     }  
  108.  
  109.     void _timer_Tick(object sender, EventArgs e)  
  110.     {  
  111.         if (_captionPosX >= _captionTextLeft)  
  112.         {  
  113.             int subtract = CaptionAnimationSpeed;  
  114.  
  115.             int diff = _captionPosX - subtract;  
  116.  
  117.             if (diff < subtract)  
  118.             {  
  119.                 _captionPosX -= _captionPosX - _captionTextLeft;  
  120.             }  
  121.             else  
  122.             {  
  123.                 _captionPosX -= subtract;  
  124.             }  
  125.  
  126.             this.Invalidate();  
  127.         }  
  128.         else  
  129.         {  
  130.             this.DoubleBuffered = false;  
  131.             _timer.Dispose();  
  132.         }  
  133.     }  
  134.  
  135.     public void AddImage(string path)  
  136.     {  
  137.         Image img = Image.FromFile(path);  
  138.         _AddImage(img, ""this.CaptionBackgrounColor);  
  139.     }  
  140.  
  141.     public void AddImage(string path, string caption)  
  142.     {  
  143.         Image img = Image.FromFile(path);  
  144.         _AddImage(img, caption, this.CaptionBackgrounColor);  
  145.     }  
  146.  
  147.     public void AddImage(string path, string caption, Color captionBackgroundColor)  
  148.     {  
  149.         Image img = Image.FromFile(path);  
  150.         _AddImage(img, caption, captionBackgroundColor);  
  151.     }  
  152.  
  153.     public void AddImage(Image img)  
  154.     {  
  155.         _AddImage(img, ""this.CaptionBackgrounColor);  
  156.     }  
  157.  
  158.     public void AddImage(Image img, string caption)  
  159.     {  
  160.         _AddImage(img, caption, this.CaptionBackgrounColor);  
  161.     }  
  162.  
  163.     public void AddImage(Image img, string caption, Color captionBackgroundColor)  
  164.     {  
  165.         _AddImage(img, caption, captionBackgroundColor);  
  166.     }  
  167.  
  168.     protected void _AddImage(Image img, string caption, Color captionBackgroundColor)  
  169.     {  
  170.         _imageList.Add(img);  
  171.         _captionList.Add(caption);  
  172.         _captionBgColor.Add(captionBackgroundColor);  
  173.     }  
  174.  
  175.     public int CaptionHeight  
  176.     {  
  177.         set;  
  178.         get;  
  179.     }  
  180.  
  181.     public int CaptionTextLeft  
  182.     {  
  183.         set  
  184.         {  
  185.             _captionPosX = value;  
  186.             _captionTextLeft = value;  
  187.         }  
  188.         get  
  189.         {  
  190.             return _captionTextLeft;  
  191.         }  
  192.     }  
  193.  
  194.     public Color CaptionBackgrounColor  
  195.     {  
  196.         set;  
  197.         get;  
  198.     }  
  199.  
  200.     public int CaptionOpacity  
  201.     {  
  202.         set;  
  203.         get;  
  204.     }  
  205.  
  206.     public int CaptionAnimationSpeed  
  207.     {  
  208.         set;  
  209.         get;  
  210.     }  
  211.  
  212.     public bool Animation  
  213.     {  
  214.         set;  
  215.         get;  
  216.     }  
  217.  
  218.     public xButton LeftButton  
  219.     {  
  220.         get  
  221.         {  
  222.             return leftButton;  
  223.         }  
  224.     }  
  225.  
  226.     public xButton RightButton  
  227.     {  
  228.         get  
  229.         {  
  230.             return rightButton;  
  231.         }  
  232.     }  
  233.  
  234.     protected override void OnPaint(PaintEventArgs e)  
  235.     {  
  236.         base.OnPaint(e);  
  237.  
  238.         Graphics g = e.Graphics;  
  239.         try  
  240.         {  
  241.             Color captionBgColor = Color.FromArgb(CaptionOpacity, _captionBgColor[_pageIndex].R, _captionBgColor[_pageIndex].G, _captionBgColor[_pageIndex].B);  
  242.             g.DrawImage(_imageList[_pageIndex], new Rectangle(0, 0, this.Width, this.Height));  
  243.             g.FillRectangle(new SolidBrush(captionBgColor)new Rectangle(0, this.Height - this.CaptionHeight, this.Width, this.Height));  
  244.  
  245.             string caption = _captionList[_pageIndex];  
  246.  
  247.             SizeF fontSize = g.MeasureString(_captionList[_pageIndex], this.Font);  
  248.             g.DrawString(_captionList[_pageIndex], this.Font, new SolidBrush(this.ForeColor), _captionPosX, this.Height - (int)(this.CaptionHeight - (fontSize.Height / 2)));  
  249.         }  
  250.         catch { }  
  251.     }  
  252.  
  253.     public class xButton : Button  
  254.     {  
  255.         public xButton()  
  256.         {  
  257.             this.BackColor = Color.Black;  
  258.             this.Height = 50;  
  259.             this.Width = 50;  
  260.         }  
  261.  
  262.         protected override void OnPaint(PaintEventArgs pevent)  
  263.         {  
  264.             Graphics g = pevent.Graphics;  
  265.             Rectangle area = new Rectangle(0, 0, this.Width, this.Height);  
  266.  
  267.             g.FillRectangle(new SolidBrush(this.BackColor), area);  
  268.             SizeF fontSize = g.MeasureString(this.Text, this.Font);  
  269.             g.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor)(this.Width - fontSize.Width) / 2, (this.Height - fontSize.Height) / 2);  
  270.         }  
  271.     }  
  272. } 

Comments

856068

Please enter the 1st, 3rd, and 6th
Group Chat If you want to know how to develop a group chat application, have a look at this project. The project includes a server and a client. The server listens for TCP client connections. Once a client is co...
Custom InputBox C# unlike Visual Basic, does not have an Input DialogBox class to capture user input. Buts that's not a problem as you can just add a reference to Microsoft.VisualBasic.dll and use the static InputBox...
Text To Speech The .Net Framework makes developing Text to Speech applications very simple with the System.Speech assembly. In this article, I explain how easy it is to get started with Text to Speech.
Image Slider Control This Windows control can be used to view images in a slide show with captions. The control is configurable with a few useful settings. The list below contains a set of properties which can be used to ...
Custom Button Control Creating custom controls with the .Net Framework is relatively easy. In this article I explain how to create a custom button control using GDI.