简体   繁体   中英

Read image to picture box

I am trying to read image to file. And I am writing below code;

            for ( int i = 0; i < filePaths.Length; i++ )
            {
                pictureBox1.Image = imList.Images[i];
                pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
                Application.DoEvents();
                Thread.Sleep(1000);
            }

But images resolution is very bad. When I am only write below code;

pictureBox1.Image = imList.Images[i];
pictureBox1.SizeMode = PictureBoxSizeMode.Normal;

Not problem. Resolution is good. I tried different size mode but not changed. What is my problem? Thanks in advance.


Code added from the comments.

ImageList imList = new ImageList(); 

In loop here

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

You are sleeping the UI thread right after calling do events, which might not have finished the rendering the picture completely in full resolution. After it wakes up, time to change the picture again!

Better approach would be not to load pictures from UI thread; instead run a separate thread and sleep there as long as needed. Following example assume you are starting the process from a button click event:

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

It looks like you are trying to do some sort of slide show. I think your issue is with refreshing the image. You might want to try and add the images to different picture holders, stack them and then send them to back. This will create the slide show. If that's not your intention please clarify your question, or needs.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM