C# “将图像读取到图片”框

C# “将图像读取到图片”框,c#,image,C#,Image,我正在尝试将图像读取到文件。我正在写下面的代码 for ( int i = 0; i < filePaths.Length; i++ ) { pictureBox1.Image = imList.Images[i]; pictureBox1.SizeMode = PictureBoxSizeMode.Normal; Application.DoEv

我正在尝试将图像读取到文件。我正在写下面的代码

            for ( int i = 0; i < filePaths.Length; i++ )
            {
                pictureBox1.Image = imList.Images[i];
                pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
                Application.DoEvents();
                Thread.Sleep(1000);
            }
pictureBox1.Image = imList.Images[i];
pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
没问题。决心是好的。我尝试了不同的尺寸模式,但没有改变。我有什么问题?提前谢谢


从注释中添加的代码

ImageList imList = new ImageList(); 
在这里循环

filePath = @"C:\Users\OSMAN\documents\visual studio 2013\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Yaprak\" + j ; string[] filePaths = Directory.GetFiles(filePath,"*.jpg");

看起来你想做一些幻灯片放映。我认为你的问题在于刷新图像。您可能希望尝试将图像添加到不同的图片夹中,将其堆叠,然后将其发送回。这将创建幻灯片放映。如果这不是你的意图,请澄清你的问题或需求

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

private void Form1_Load(object sender, EventArgs e)
    {
        var left = pictureBox1.Left;
        var top = pictureBox1.Top;
        var width = pictureBox1.Width;
        var height = pictureBox1.Height;
        var form = this;
        var currPictureBox = pictureBox1;

        new Thread(new ThreadStart(() =>
            {
                for (var x = 1; x < 3; x++)
                {
                    ExecuteSecure(() =>
                    {
                        var pictureBox = new PictureBox
                        {
                            Width = width,
                            Height = height,
                            Top = top,
                            Left = left,
                            ImageLocation = @"C:\filelocation" + x + ".jpg"
                        };

                        form.Controls.Remove(currPictureBox);
                        form.Controls.Add(pictureBox);
                        currPictureBox = pictureBox;
                        form.Refresh();
                    });

                    Thread.Sleep(1000);
                }
            })).Start();
    }

    private void ExecuteSecure(Action a)
    {
        if (InvokeRequired)  BeginInvoke(a);
        else a();
    }
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用系统线程;
使用System.Threading.Tasks;
使用System.Windows.Forms;
私有void Form1\u加载(对象发送方、事件参数e)
{
var left=pictureBox1.左;
var top=pictureBox1.top;
变量宽度=pictureBox1.宽度;
变量高度=pictureBox1.高度;
var form=此;
var currPictureBox=pictureBox1;
新线程(新线程开始(()=>
{
对于(变量x=1;x<3;x++)
{
执行安全(()=>
{
var pictureBox=新的pictureBox
{
宽度=宽度,
高度=高度,
顶部=顶部,
左=左,
ImageLocation=@“C:\filelocation”+x+“.jpg”
};
窗体.控件.删除(currPictureBox);
表单.控件.添加(pictureBox);
currPictureBox=pictureBox;
form.Refresh();
});
睡眠(1000);
}
})).Start();
}
私有void ExecuteSecure(操作a)
{
如果(需要调用)开始调用(a);
else a();
}

在调用do事件之后,您正在休眠UI线程,这可能还没有完成完全以全分辨率渲染图片。当它醒来后,是时候再次更改图片了

更好的方法是不从UI线程加载图片;相反,运行一个单独的线程,并根据需要在那里睡眠。下面的示例假设您从按钮单击事件启动流程:

private void button1_Click(object sender, EventArgs e)
{

  Task.Factory.StartNew(() => LoadPics());

  // if TPL not available
  // use Action delegate
  // Not showing endinvoke here
  // var action = new Action(LoadPics);
  // action.BeginInvoke(null, null);
}

private void SetImage(Image img)
{
    pictureBox1.Image = img;
    pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
}

private void LoadPics()
{       
   for ( int i = 0; i < filePaths.Length; i++ )
   {
        // Invoke UI thread for changing picture
        pictureBox1.Invoke(new Action(() => SetImage(imList.Images[i])));
        Thread.Sleep(1000);
   }
}
private void按钮1\u单击(对象发送者,事件参数e)
{
Task.Factory.StartNew(()=>LoadPics());
//如果第三方物流不可用
//使用动作委托
//这里不显示endinvoke
//var动作=新动作(LoadPics);
//action.BeginInvoke(null,null);
}
私有void SetImage(图像img)
{
pictureBox1.Image=img;
pictureBox1.SizeMode=PictureBoxSizeMode.Normal;
}
私有void LoadPics()
{       
for(int i=0;iSetImage(imList.Images[i]));
睡眠(1000);
}
}

在您的第二段代码中,我等于什么?如果没有循环?没有循环没有解决问题。什么是
imList
,什么是
filepath
?循环在这里是错误的。您应该清楚地说明您的要求。ImageList imList=新ImageList();在此处循环文件路径=@“C:\Users\OSMAN\documents\visual studio 2013\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Yaprak\”+j;字符串[]filePath=Directory.GetFiles(filePath,*.jpg”);您为图像列表指定的图像大小是多少?嗯,我想要一个幻灯片放映,快速幻灯片放映。大约0.2秒或小于。我怎样才能在c#上成功。谢谢