循环显示图片的两种方法

循环显示图片的两种方法 

方法一,用Timer控件 


public partial class Form1 : Form
{

static int picIndex = 0;
static IList<string> picList = new List<string>();
static System.Windows.Forms.Timer picTimer = new System.Windows.Forms.Timer();

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

InitPicList();
picTimer.Tick += new EventHandler(picTimer_Tick);
等待0.5秒
picTimer.Interval = 500;
picTimer.Start();
Application.DoEvents();  //慎重使用此语句
}
public void InitPicList()
{
picList.Add(@"D:\szf\tupian\001.jpg");
picList.Add(@"D:\szf\tupian\002.jpg");
picList.Add(@"D:\szf\tupian\003.jpg");
picList.Add(@"D:\szf\tupian\004.jpg");
picList.Add(@"D:\szf\tupian\005.jpg");
picList.Add(@"D:\szf\tupian\006.jpg");
}

private void picTimer_Tick(object sender, EventArgs e)
{
picIndex++;
if (picIndex > (picList.Count - 1))
{
picIndex = 0;
}
string picLocation = picList[picIndex];
pictureBox1.Load(picLocation);
}
}

方法二,用自己编写的延时函数 Sleep(int mm);

 private void button1_Click(object sender, EventArgs e)
        {
          
                string folderpath =@"D:\szf\tupian\";  //图片文件夹
               
                string p1 = "";
             for (int i = 1; i <= 7; i++)
                {
                    p1=folderpath + "00" + i.ToString() + ".jpg";
                    pictureBox1.ImageLocation = p1.Trim();
                   Sleep(500) ;
                   }
                p1 = folderpath + "001.jpg";
                pictureBox1.ImageLocation = p1.Trim();
                MessageBox.Show ("结束了");
        }

      

        private void Sleep(int mm)
        {
            DateTime current = DateTime.Now;

            while (current.AddMilliseconds(mm) > DateTime.Now)
            {
               Application.DoEvents();
            }
            return;
        }
  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果在循环显示多张图片时出现了闪烁问题,可以尝试以下两种解决方案: 1. 双缓冲技术 在双缓冲技术中,我们创建一个额外的QPixmap来缓存下一张要显示图片,等待下一次切换时再将其显示出来。这样,在切换图片时,可以先在缓冲区中绘制下一张图片,然后再将缓冲区中的图片显示到QLabel上,从而避免了闪烁。 以下是示例代码: ```cpp #include <QApplication> #include <QLabel> #include <QPixmap> #include <QTimer> int main(int argc, char *argv[]) { QApplication a(argc, argv); // 加载多张图片 QPixmap images[5]; images[0].load("image1.png"); images[1].load("image2.png"); images[2].load("image3.png"); images[3].load("image4.png"); images[4].load("image5.png"); // 创建QLabel来显示图片 QLabel label; label.setPixmap(images[0]); label.show(); // 双缓冲技术 QPixmap buffer(images[0].size()); buffer.fill(Qt::transparent); // 创建定时器,每隔100毫秒切换图片 QTimer timer; int index = 0; QObject::connect(&timer, &QTimer::timeout, [&](){ QPainter painter(&buffer); painter.drawPixmap(0, 0, images[(index + 1) % 5]); label.setPixmap(buffer); index = (index + 1) % 5; }); timer.start(100); return a.exec(); } ``` 在上面的示例代码中,我们创建了一个额外的QPixmap来缓存下一张要显示图片,使用QPainter在缓冲区中绘制下一张图片,然后将缓冲区中的图片显示到QLabel上。 2. OpenGL渲染 如果双缓冲技术无法解决闪烁问题,可以尝试使用OpenGL来进行渲染。OpenGL是一种跨平台的图形库,可以在多种操作系统上实现高效的图形渲染。 使用OpenGL渲染需要在Qt中使用QOpenGLWidget类来创建OpenGL窗口,然后在OpenGL中绘制图片。在切换图片时,只需要更新OpenGL中的纹理即可。 以下是示例代码: ```cpp #include <QApplication> #include <QOpenGLWidget> #include <QOpenGLFunctions> #include <QOpenGLTexture> #include <QTimer> class GLWidget : public QOpenGLWidget, protected QOpenGLFunctions { public: GLWidget(QWidget *parent = nullptr) : QOpenGLWidget(parent) { setFixedSize(640, 480); setAutoFillBackground(false); } ~GLWidget() { makeCurrent(); m_texture->destroy(); doneCurrent(); } void initializeGL() override { initializeOpenGLFunctions(); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // 创建纹理对象 m_texture = new QOpenGLTexture(QImage("image1.png")); m_texture->setMinificationFilter(QOpenGLTexture::Linear); m_texture->setMagnificationFilter(QOpenGLTexture::Linear); m_texture->setWrapMode(QOpenGLTexture::ClampToBorder); } void paintGL() override { glClear(GL_COLOR_BUFFER_BIT); // 绘制纹理 m_texture->bind(); glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, -1.0f); glTexCoord2f(1.0f, 0.0f); glVertex2f(1.0f, -1.0f); glTexCoord2f(1.0f, 1.0f); glVertex2f(1.0f, 1.0f); glTexCoord2f(0.0f, 1.0f); glVertex2f(-1.0f, 1.0f); glEnd(); m_texture->release(); } void updateTexture(int index) { // 更新纹理 m_texture->setData(QImage(QString("image%1.png").arg(index + 1))); update(); } private: QOpenGLTexture *m_texture; }; int main(int argc, char *argv[]) { QApplication a(argc, argv); // 创建OpenGL窗口 GLWidget widget; widget.show(); // 创建定时器,每隔100毫秒切换图片 QTimer timer; int index = 0; QObject::connect(&timer, &QTimer::timeout, [&](){ widget.updateTexture(index); index = (index + 1) % 5; }); timer.start(100); return a.exec(); } ``` 在上面的示例代码中,我们使用了QOpenGLTexture来创建纹理对象,然后在OpenGL中绘制纹理。在切换图片时,我们只需要更新纹理即可。这种方式可以避免闪烁问题,同时还能实现更流畅的视频效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值