Note: You can download the Source Code from the link Source Code Download Link
The main purpose of this article is to explain on how to create a simple Animated Image Slide Show for windows applications using C#.
This Animated Image Slide Show User control has features as
1) Load Images. 2) Display both Thumb and Large size Image. 3) Highlight the Selected and Current Image in Thumb image View. 4) Previous, Next and Play Animation. 5) Small to Big Zoom out Image Animation. 6) Left to Right Scroll Animation. 7) Right to Left Scroll Animation 8) Top to Bottom Scroll Animation. 9) Image Fade In Animation 10) Transparent Horizontal Bar display on image Animation. 11) Transparent Vertical Bar display on image Animation. 12) Transparent Text display on Image Animation. 13) Random Block transparent Animation. 14) Play and Stop Music
This application was developed using C# GDI+ functionality. The goal was to create a flash style animated Image Slide show for Windows Applications. We have added basic animation features for the Image Slide Show user control. Here we can see Fade and Vertical Transparent Bar Animation Screens.
Here we can see the Selected and Current Image in Thump View will be Highlighted with Border and little big Image size.
Now we start with our Code: We have created a Animated Image Slide Show as a User Control so that it can be used easily in all projects. In this article we have attached zip file named as SHANUImageSlideShow.zip. Which contains.. 1) "ShanuImageSlideShow_Cntrl" Folder (This folder contains the Image Slide Show User control Source code). 2) "SHANUImageSlideShow_Demo" Folder (This folder contains the Demo program which includes the Image Slide Show user control ).
Code Part
public
void
LoadImages()
{
DirectoryInfo Folder;
DialogResult result =
this
.folderBrowserDlg.ShowDialog();
imageselected =
false
;
if
(result == DialogResult.OK)
Folder =
new
DirectoryInfo(folderBrowserDlg.SelectedPath);
var imageFiles = Folder.GetFiles(
"*.jpg"
)
.Concat(Folder.GetFiles(
"*.gif"
))
"*.png"
"*.jpeg"
"*.bmp"
)).ToArray();
// Here we filter all image files
pnlThumb.Controls.Clear();
(imageFiles.Length > 0)
true
TotalimageFiles = imageFiles.Length;
}
else
return
int
locnewX = locX;
locnewY = locY;
ctrl =
PictureBox[TotalimageFiles];
AllImageFielsNames =
String[TotalimageFiles];
imageindexs = 0;
foreach
(FileInfo img
in
imageFiles)
AllImageFielsNames[imageindexs] = img.FullName;
loadImagestoPanel(img.Name, img.FullName, locnewX, locnewY, imageindexs);
locnewX = locnewX + sizeWidth + 10;
imageindexs = imageindexs + 1;
CurrentIndex = 0;
StartIndex = 0;
LastIndex = 0;
DrawImageSlideShows();
// This Function will display the Thumb Images.
private
loadImagestoPanel(String imageName, String ImageFullName,
newLocX,
newLocY,
imageindexs)
ctrl[imageindexs] =
PictureBox();
ctrl[imageindexs].Image = Image.FromFile(ImageFullName);
ctrl[imageindexs].BackColor = Color.Black;
ctrl[imageindexs].Location =
Point(newLocX, newLocY);
ctrl[imageindexs].Size =
System.Drawing.Size(sizeWidth - 30, sizeHeight - 60);
ctrl[imageindexs].BorderStyle = BorderStyle.None;
ctrl[imageindexs].SizeMode = PictureBoxSizeMode.StretchImage;
// ctrl[imageindexs].MouseClick += new MouseEventHandler(control_MouseMove);
pnlThumb.Controls.Add(ctrl[imageindexs]);
Here we call the above function in Load Image Click Event.In this function load and display all the Images from the selected folder.
6. Once Image loaded we need highlight the selected and current image. For this we call the below function which will check for the current image and set the image border and Increase the Size of the present image.
#region Highlight The Current Slected image
HighlightCurrentImage()
for
(
i = 0; i <= ctrl.Length - 1; i++)
(i == CurrentIndex)
ctrl[i].Left = ctrl[i].Left - 20;
ctrl[i].Size =
System.Drawing.Size(sizeWidth + 10, sizeHeight);
ctrl[i].BorderStyle = BorderStyle.FixedSingle;
// ctrl[i].Location = new Point(newLocX, newLocY);
System.Drawing.Size(sizeWidth - 20, sizeHeight - 40);
ctrl[i].BorderStyle = BorderStyle.None;
#endregion
#region Draw Animation on selected Image
drawAnimation()
try
switch
(SlideType)
case
0:
// Small to big
SmalltoBigImage_Animation();
break
1:
// left to right
LefttoRight_Animation();
2:
// Rectangle Transparent
Transparent_Bar_Animation();
3:
// Right to Left
RighttoLeft_Animation();
4:
// Top to Bottom
ToptoBottom_Animation();
5:
// Bottom to Top
BottomTop_Animation();
6:
// Rectangle Vertical Block Transparent
Vertical_Bar_Animation();
7:
// Random Block Transparent
Random_Bar_Animation();
8:
// Rectangle Horizontal Block Transparent
Horizontal_Bar_Animation();
9:
// Text Transparent
Transparent_Text_Animation();
10:
// Random Circle and Bar Transparent
Random_Circle_Animation();
default
:
// In Default there is no animation but load next image in time intervals.
picImageSlide.Width = pnlImg.Width;
picImageSlide.Height = pnlImg.Height;
timer1.Enabled =
timer1.Start();
catch
(Exception ex)
// Small to Big Size Image
SmalltoBigImage_Animation()
leftConstant_adjust = 40;
topconstant_adjust = 30;
((picImageSlide.Width < (MINMAX * pnlImg.Width)) &&
(picImageSlide.Height < (MINMAX * pnlImg.Height)))
picImageSlide.Width = Convert.ToInt32(picImageSlide.Width * ZOOMFACTOR);
picImageSlide.Height = Convert.ToInt32(picImageSlide.Height * ZOOMFACTOR);
picImageSlide.Left = Convert.ToInt32(picImageSlide.Left - leftConstant_adjust);
(picImageSlide.Top <= 0)
picImageSlide.Left = 0;
picImageSlide.Top = 0;
picImageSlide.Top = Convert.ToInt32(picImageSlide.Top - topconstant_adjust);
picImageSlide.SizeMode = PictureBoxSizeMode.StretchImage;
//In else part i check for the animation completed if its completed stop the timer 2 and start the timer 1 to load the next image.
Start_Stop_Timer_1(
);
// Start the Timer 1 to load the next Image
Start_Stop_Timer_2(
// Stop the Timer 2 to stop the animation till the next image loaded.
//Left to Right Animation
LefttoRight_Animation()
picImageSlide.Invalidate();
(picImageSlide.Location.X >= 10)
picImageSlide.Left = xval;
xval = xval + 100;
Transparent_Bar_Animation()
// picImageSlide.Invalidate();
(opacity >= 90)
opacity = 100;
// picImageSlide.Refresh();
Graphics g = Graphics.FromImage(picImageSlide.Image);
g.FillRectangle(
SolidBrush(Color.FromArgb(58, Color.White)), 0, 0, picImageSlide.Image.Width, picImageSlide.Image.Height);
opacity = opacity + 10;
picImageSlide.Image = PictuerBoxFadeIn(picImageSlide.Image, opacity);
//calling ChangeOpacity Function
// Right to Left Animation
RighttoLeft_Animation()
(xval_Right <= 100)
xval_Right = 0;
picImageSlide.Left = xval_Right;
xval_Right = xval_Right - 100;
// Top to Bottom Slide Animation
ToptoBottom_Animation()
(yval + 60 >= 30)
picImageSlide.Top = yval;
yval = yval + 100;
// Bottom to Top Slide Animation
BottomTop_Animation()
(yval_Right <= 0)
picImageSlide.Top = yval_Right;
yval_Right = yval_Right - 100;
// vertical transparent Bar Animation
Vertical_Bar_Animation()
(opacity_new <= 0)
opacity_new = 100;
Graphics g2 = Graphics.FromImage(picImageSlide.Image);
recBlockYval = 0;
barheight = barheight + 100;
g2.DrawRectangle(Pens.Black, recBlockXval, recBlockYval, barwidth, barheight);
g2.FillRectangle(
SolidBrush(Color.FromArgb(opacity_new, Color.White)), recBlockXval, recBlockYval, barwidth - 1, barheight - 1);
opacity_new = opacity_new - 10;
recBlockXval = recBlockXval + barwidth + 4;
picImageSlide.Refresh();
// Random bar and Circle transparent Animation
Random_Bar_Animation()
Graphics g3 = Graphics.FromImage(picImageSlide.Image);
recBlockXval = 0;
barwidth = barwidth + 100;
// g3.DrawRectangle(Pens.Black, rnd.Next(0, 200), rnd.Next(0, 200), rnd.Next(100, 600), rnd.Next(60, 400));
g3.FillRectangle(
SolidBrush(Color.FromArgb(opacity_new, Color.White)), rnd.Next(10, 600), rnd.Next(10, 600), rnd.Next(100, 600), rnd.Next(60, 400));
opacity_new = opacity_new - 5;
recBlockYval = recBlockYval + barheight + 4;
//recBlockYval = recBlockYval + 100;
//barheight = barheight + 100;
//Horizontal transparent Bar Animation
Horizontal_Bar_Animation()
Graphics g4 = Graphics.FromImage(picImageSlide.Image);
g4.DrawRectangle(Pens.Black, recBlockXval, recBlockYval, barwidth, barheight);
g4.FillRectangle(
// transparent text Animation
Transparent_Text_Animation()
Graphics g5 = Graphics.FromImage(picImageSlide.Image);
g5.DrawString(
"Shanu Slide Show"
,
Font(
"Arial"
, 86),
SolidBrush(Color.FromArgb(opacity_new, Color.FromArgb(
.rnd.Next(256),
.rnd.Next(256)))),
rnd.Next(600), rnd.Next(400));
// Random Circle Animation
Random_Circle_Animation()
Graphics g6 = Graphics.FromImage(picImageSlide.Image);
g6.FillRectangle(
SolidBrush(Color.FromArgb(opacity_new, Color.White)), rnd.Next(10, 600), rnd.Next(10, 600), rnd.Next(400, 800), rnd.Next(60, 400));
g6.FillPie(
.rnd.Next(256)))), rnd.Next(600), rnd.Next(400), rnd.Next(400, 800), rnd.Next(400), 0, 360);
//for the Image Transparent
static
Bitmap PictuerBoxFadeIn(Image img,
opacity)
Bitmap bmp =
Bitmap(img.Width, img.Height);
Graphics g = Graphics.FromImage(bmp);
ColorMatrix colmat =
ColorMatrix();
colmat.Matrix33 = opacity;
ImageAttributes imgAttr =
ImageAttributes();
imgAttr.SetColorMatrix(colmat, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
g.DrawImage(img,
Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imgAttr);
g.Dispose();
// return the new fade in Image
bmp;
8.After completion save,Build and run the project.
Now we create a Windows application and add test our "SHANUImageSlideShow_Demo" User Control.