动画图片幻灯片






4.77/5 (21投票s)
使用 C# 的 Winforms 动画图片幻灯片
引言
本文的主要目的是解释如何使用 C# 为 Windows 应用程序创建一个简单的动画图像幻灯片放映。我的目的是创建一个简单的可重用的动画图像幻灯片,因此我创建了图像幻灯片放映作为一个用户控件,用户只需将用户控件添加到项目中即可使用它。此动画图像幻灯片放映用户控件具有以下功能:
- 加载图像
- 显示缩略图和大尺寸图像
- 在缩略图图像视图中突出显示选定和当前图像
- 上一张、下一张和播放动画
- 从小到大缩小图像动画
- 从左到右滚动动画
- 从右到左滚动动画
- 从上到下滚动动画
- 图像淡入动画
- 在图像动画上显示透明水平条
- 在图像动画上显示透明垂直条
- 在图像动画上显示透明文本
- 随机块透明动画
- 播放和停止音乐
背景
主要目的是开发一个简单易用的 .NET 用户控件用于图像幻灯片放映。此应用程序是使用 .NET 4.0 (C#) 开发的,并使用 GDI+ 功能。目标是为 Windows 应用程序创建一个 Flash 样式的动画图像幻灯片放映。我为图像幻灯片放映用户控件添加了基本的动画功能。在这里,我们可以看到我的项目的淡入和垂直透明条动画屏幕。
在这里,我们可以看到缩略图视图中的选定和当前图像将以边框突出显示,并且图像尺寸略大。
现在我们开始编写代码。
我创建了一个动画图像幻灯片放映作为一个用户控件,以便可以在所有项目中轻松使用。
在本文中,我附加了一个名为 *SHANUImageSlideShow.zip* 的 zip 文件,其中包含
- "ShanuImageSlideShow_Cntrl" 文件夹(此文件夹包含图像幻灯片放映用户控件源代码)。
- "SHANUImageSlideShow_Demo" 文件夹(此文件夹包含包含图像幻灯片放映用户控件的演示程序)。
Using the Code
#region Highlight The Current Selected image
public void HighlightCurrentImage()
{
for (int i = 0; i <= ctrl.Length - 1; i++)
{
if (i == CurrentIndex)
{
ctrl[i].Left = ctrl[i].Left - 20;
ctrl[i].Size = new System.Drawing.Size(sizeWidth + 10, sizeHeight);
ctrl[i].BorderStyle = BorderStyle.FixedSingle;
}
else
{
// ctrl[i].Location = new Point(newLocX, newLocY);
ctrl[i].Size = new System.Drawing.Size(sizeWidth - 20, sizeHeight - 40);
ctrl[i].BorderStyle = BorderStyle.None;
}
}
}
#endregion
- 首先,我们将从用户控件开始。要创建用户控件,
- 创建一个新的 Windows 控件库项目。
- 设置项目名称,然后单击确定(在这里,我的用户控件名称是
ShanuImageSlideShow_Cntrl
)。 - 添加所有需要的控件。
- 在代码隐藏中,声明所有的
public
变量和Public
属性变量。 - 图像加载按钮单击。在这里,我们从文件夹中加载所有图像作为缩略图图像视图到我们的控件中。
#region LoadImage 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")) .Concat(Folder.GetFiles("*.png")) .Concat(Folder.GetFiles("*.jpeg")) .Concat(Folder.GetFiles("*.bmp")).ToArray(); // Here, we // filter all image files pnlThumb.Controls.Clear(); if (imageFiles.Length > 0) { imageselected = true; TotalimageFiles = imageFiles.Length; } else { return; } int locnewX = locX; int locnewY = locY; ctrl = new PictureBox[TotalimageFiles]; AllImageFielsNames = new String[TotalimageFiles]; int 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 void loadImagestoPanel(String imageName, String ImageFullName, int newLocX, int newLocY, int imageindexs) { ctrl[imageindexs] = new PictureBox(); ctrl[imageindexs].Image = Image.FromFile(ImageFullName); ctrl[imageindexs].BackColor = Color.Black; ctrl[imageindexs].Location = new Point(newLocX, newLocY); ctrl[imageindexs].Size = new 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]); } #endregion
在这里,我在加载图像单击事件中调用上述函数。在此函数中,加载并显示来自选定文件夹的所有图像。
- 加载图像后,我们需要突出显示选定的当前图像。为此,我调用下面的函数,该函数将检查当前图像并设置图像边框并增加当前图像的大小。
- 这是用户控件的重要功能,我在其中为选定的当前图像进行动画处理。此函数将在计时器滴答事件中调用。动画完成后,我停止计时器并激活主计时器以加载下一张图像。从主计时器,我创建从 1 到 11 的随机数并激活子计时器。子计时器用于显示动画。我已在每种情况下注释了用途。
#region Draw Animation on selected Image public void drawAnimation() { try { switch (SlideType) { case 0:// Small to big SmalltoBigImage_Animation(); break; case 1:// left to right LefttoRight_Animation(); break; case 2:// Rectangle Transparent Transparent_Bar_Animation(); break; case 3:// Right to Left RighttoLeft_Animation(); break; case 4:// Top to Bottom ToptoBottom_Animation(); break; case 5:// Bottom to Top BottomTop_Animation(); break; case 6:// Rectangle Vertical Block Transparent Vertical_Bar_Animation(); break; case 7:// Random Block Transparent Random_Bar_Animation(); break; case 8:// Rectangle Horizontal Block Transparent Horizontal_Bar_Animation(); break; case 9:// Text Transparent Transparent_Text_Animation(); break; case 10:// Random Circle and Bar Transparent Random_Circle_Animation(); break; default: // In default, there is no animation // but load next image in time intervals. picImageSlide.Width = pnlImg.Width; picImageSlide.Height = pnlImg.Height; timer1.Enabled = true; timer1.Start(); break; } } catch (Exception ex) { } } // Small to Big Size Image private void SmalltoBigImage_Animation() { int leftConstant_adjust = 40; int topconstant_adjust = 30; if ((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); if (picImageSlide.Top <= 0) { picImageSlide.Left = 0; picImageSlide.Top = 0; } picImageSlide.Top = Convert.ToInt32(picImageSlide.Top - topconstant_adjust); picImageSlide.SizeMode = PictureBoxSizeMode.StretchImage; } else //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. { picImageSlide.Width = pnlImg.Width; picImageSlide.Height = pnlImg.Height; Start_Stop_Timer_1(true); // Start the Timer 1 // to load the next Image Start_Stop_Timer_2(false);// Stop the Timer 2 to stop // the animation till // the next image loaded. } } //Left to Right Animation private void LefttoRight_Animation() { picImageSlide.Invalidate(); if (picImageSlide.Location.X >= 10) { picImageSlide.Left = 0; Start_Stop_Timer_1(true); // Start the Timer 1 // to load the next Image Start_Stop_Timer_2(false);// Stop the Timer 2 // to stop the animation // till the next image loaded. } else { picImageSlide.Left = xval; xval = xval + 100; } picImageSlide.Width = pnlImg.Width; picImageSlide.Height = pnlImg.Height; } //Left to Right Animation private void Transparent_Bar_Animation() { // picImageSlide.Invalidate(); if (opacity >= 90) { Start_Stop_Timer_1(true); // Start the Timer 1 // to load the next Image Start_Stop_Timer_2(false);// Stop the Timer 2 // to stop the animation // till the next image loaded. opacity = 100; } // picImageSlide.Refresh(); Graphics g = Graphics.FromImage(picImageSlide.Image); g.FillRectangle(new 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 private void RighttoLeft_Animation() { picImageSlide.Invalidate(); if (xval_Right <= 100) { picImageSlide.Left = 0; xval_Right = 0; Start_Stop_Timer_1(true); // Start the Timer 1 // to load the next Image Start_Stop_Timer_2(false);// Stop the Timer 2 to stop the animation // till the next image loaded. } else { picImageSlide.Left = xval_Right; xval_Right = xval_Right - 100; } picImageSlide.Width = pnlImg.Width; picImageSlide.Height = pnlImg.Height; } // Top to Bottom Slide Animation private void ToptoBottom_Animation() { picImageSlide.Invalidate(); if (yval + 60 >= 30) { picImageSlide.Top = 0; Start_Stop_Timer_1(true); // Start the Timer 1 // to load the next Image Start_Stop_Timer_2(false);// Stop the Timer 2 to stop the // animation till the // next image loaded. } else { picImageSlide.Top = yval; yval = yval + 100; } picImageSlide.Width = pnlImg.Width; picImageSlide.Height = pnlImg.Height; } // Bottom to Top Slide Animation private void BottomTop_Animation() { picImageSlide.Invalidate(); if (yval_Right <= 0) { picImageSlide.Left = 0; xval_Right = 0; Start_Stop_Timer_1(true); // Start the Timer 1 // to load the next Image Start_Stop_Timer_2(false);// Stop the Timer 2 to stop the // animation till the // next image loaded. } else { picImageSlide.Top = yval_Right; yval_Right = yval_Right - 100; } picImageSlide.Width = pnlImg.Width; picImageSlide.Height = pnlImg.Height; } // vertical transparent Bar Animation private void Vertical_Bar_Animation() { if (opacity_new <= 0) { Start_Stop_Timer_1(true); // Start the Timer 1 // to load the next Image Start_Stop_Timer_2(false);// Stop the Timer 2 // to stop the animation // till the next image loaded. opacity_new = 100; } // picImageSlide.Refresh(); Graphics g2 = Graphics.FromImage(picImageSlide.Image); recBlockYval = 0; barheight = barheight + 100; g2.DrawRectangle(Pens.Black, recBlockXval, recBlockYval, barwidth, barheight); g2.FillRectangle(new 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 private void Random_Bar_Animation() { if (opacity_new <= 0) { Start_Stop_Timer_1(true); // Start the Timer 1 // to load the next Image Start_Stop_Timer_2(false);// Stop the Timer 2 // to stop the animation // till the next image loaded. opacity_new = 100; } // picImageSlide.Refresh(); 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(new 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; picImageSlide.Refresh(); } //Horizontal transparent Bar Animation private void Horizontal_Bar_Animation() { if (opacity_new <= 0) { Start_Stop_Timer_1(true); // Start the Timer 1 // to load the next Image Start_Stop_Timer_2(false);// Stop the Timer 2 // to stop the animation // till the next image loaded. opacity_new = 100; } recBlockXval = 0; barwidth = barwidth + 100; Graphics g4 = Graphics.FromImage(picImageSlide.Image); g4.DrawRectangle(Pens.Black, recBlockXval, recBlockYval, barwidth, barheight); g4.FillRectangle(new SolidBrush (Color.FromArgb(opacity_new, Color.White)), recBlockXval, recBlockYval, barwidth - 1, barheight - 1); opacity_new = opacity_new - 10; recBlockYval = recBlockYval + barheight + 4; picImageSlide.Refresh(); } // transparent text Animation private void Transparent_Text_Animation() { if (opacity_new <= 0) { Start_Stop_Timer_1(true); // Start the Timer 1 // to load the next Image Start_Stop_Timer_2(false);// Stop the Timer 2 to stop // the animation till the // next image loaded. opacity_new = 100; } // picImageSlide.Refresh(); Graphics g5 = Graphics.FromImage(picImageSlide.Image); g5.DrawString("Shanu Slide Show", new Font("Arial", 86), new SolidBrush(Color.FromArgb(opacity_new, Color.FromArgb(this.rnd.Next(256), this.rnd.Next(256), this.rnd.Next(256)))), rnd.Next(600), rnd.Next(400)); opacity_new = opacity_new - 5; picImageSlide.Refresh(); } // Random Circle Animation private void Random_Circle_Animation() { if (opacity_new <= 0) { Start_Stop_Timer_1(true); // Start the Timer 1 // to load the next Image Start_Stop_Timer_2(false);// Stop the Timer 2 // to stop the animation // till the next image loaded. opacity_new = 100; } // picImageSlide.Refresh(); Graphics g6 = 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)); g6.FillRectangle(new 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(new SolidBrush(Color.FromArgb(opacity_new, Color.FromArgb(this.rnd.Next(256), this.rnd.Next(256), this.rnd.Next(256)))), rnd.Next(600), rnd.Next(400), rnd.Next(400, 800), rnd.Next(400), 0, 360); opacity_new = opacity_new - 5; recBlockYval = recBlockYval + barheight + 4; //recBlockYval = recBlockYval + 100; //barheight = barheight + 100; picImageSlide.Refresh(); } //for the Image Transparent public static Bitmap PictuerBoxFadeIn(Image img, int opacity) { Bitmap bmp = new Bitmap(img.Width, img.Height); Graphics g = Graphics.FromImage(bmp); ColorMatrix colmat = new ColorMatrix(); colmat.Matrix33 = opacity; ImageAttributes imgAttr = new ImageAttributes(); imgAttr.SetColorMatrix (colmat, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); g.DrawImage(img, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imgAttr); g.Dispose(); // return the new fade in Image return bmp; } #endregion
- 完成保存后,构建并运行项目。
- 现在我们创建一个 Windows 应用程序并添加测试我们的“SHANUImageSlideShow_Demo”用户控件。
- 创建一个新的 Windows 项目。
- 打开您的窗体,然后从工具箱 > 右键单击 > 选择项目 > 浏览选择您的用户控件 DLL 并添加。
- 将用户控件拖到您的 Windows 窗体上。
- 运行您的程序。现在,您可以看到用户控件将添加到 Windows 窗体中。您可以打开您的图像文件夹并加载所有图像并播放动画图像幻灯片放映。
结论
本文的主要目的是为 Windows 应用程序创建一个简单友好的动画图像幻灯片放映,这将对许多用户在其项目中免费使用和工作有用。我希望我的 Pareto 图表控件从现在开始对许多用户有用。
如果您喜欢我的文章和动画图像幻灯片ShowControl
,请给我留言并为我的文章投票。我希望这个控件会有所帮助。
历史
注意:默认情况下,我在演示 *bin* 文件夹中添加了一个 Wav 音乐文件。如果用户想添加自己的音乐文件,请将您的 wav 或 mp3 文件添加到 *bin* 文件夹,并在控件中更改文件名。
- 2014 年 8 月 19 日:首次发布
- 2014 年 8 月 25 日:源代码已更新,新版本 1.1
- 2014 年 8 月 26 日:在此新版本中添加了播放和停止音乐文件功能