读图片到图片框

时间:2014-06-15 03:16:30

标签: c# image

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

            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");

2 个答案:

答案 0 :(得分:1)

在调用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);
   }
}

答案 1 :(得分:0)

看起来你正试图做某种幻灯片放映。我认为你的问题是刷新图像。您可能想尝试将图像添加到不同的图片持有者,堆叠它们然后将它们发送回去。这将创建幻灯片放映。如果这不是您的意图,请澄清您的问题或需求。

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();
    }