如何在1个图片框中随机运行大量图片? [英] how to run lots of pictures in 1 picture box randomly?

查看:59
本文介绍了如何在1个图片框中随机运行大量图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发一款教育游戏,我需要创建一个随机的图片框和一个进度条,10秒后图片框会变成另一张图片。有谁知道如何编码?

解决方案

这里的关键是:不要使用 PictureBox 。我的意思是,你可以使用它,如果你已经有可用的图像,但如果你想生成任何随机的,或任何其他动态行为,这个控件根本没有帮助,只会增加麻烦,额外浪费你的开发时间和一些资源。我会告诉你该怎么做:

在图片框中附加图片 [ ^ ],

在C#中绘制一个矩形 [ ^ ],

如何从旧图纸中清除面板 [ ^ ]。



有关详细信息,请参阅图形渲染,请看我过去的答案:

什么样的俏皮方法是Paint? (DataGridViewImageCell.Paint(...)) [ ^ ],

在面板上捕获绘图 [ ^ ],

mdi子表单之间的绘制线 [ ^ ],

如何加快我的vb.net应用程序? [ ^ ]。



注意最后的答案。您可能需要更改非UI线程上的视图(即使您使用计时器),因此您需要UI线程调用机制。请查看我过去的答案:

Control.Invoke( )与Control.BeginInvoke() [ ^ ],

Treeview扫描仪和MD5的问题 [ ^ ]。



如果您使用一个特定的计时器, System.Windows .Forms.Timer ,您不需要使用调用。该计时器的问题是精度非常低。但它的准确性,对任何周期性的东西都是不可接受的,可以适合你的一次性使用。延迟10秒(但为什么?)可能并不需要任何准确性。



-SA


您好,

可能会对您有所帮助。

根据您的需要更改代码......

我使用的是后台工作者(不是计时器控制名称:picturebox1

进度条控件名称:progressbar1





首先

创建变量。



 DirectoryInfo迪; 
List< FileInfo> images = new List< FileInfo>();
string dirpath = @ C:\\ \\Images; // 设置图像目录路径
int timeTemp = 0 ;
BackgroundWorker backgroundWorker = new BackgroundWorker
{
WorkerReportsProgress = true
WorkerSupportsCancellation = true
};





在表单加载事件中

 di =  new  DirectoryInfo(dirpath); 

GetImagesOfType( *。jpg); // 将.jpg文件添加到列表中 - 添加以下其他文件类型
// GetImagesOfType(*。png);

backgroundWorker.DoWork + = BackgroundWorkerOnDoWork;
backgroundWorker.ProgressChanged + = BackgroundWorkerOnProgressChanged;
ChangePicture();
if (!backgroundWorker.IsBusy)
backgroundWorker.RunWorkerAsync();





为后台工作者添加以下活动



<前一行=c#> 私人 void BackgroundWorkerOnProgressChanged( object sender,ProgressChangedEventArgs e)
{
progressBar1 .Value = e.ProgressPercentage;
}

private void BackgroundWorkerOnDoWork( object sender,DoWorkEventArgs e)
{
BackgroundWorker worker =(BackgroundWorker)sender;
while (!worker.CancellationPending)
{
Thread.Sleep( 1000 < /跨度>);
if (timeTemp < 100 )timeTemp + = 10 ;
else
{
timeTemp = 0 ;
ChangePicture();
}
worker.ReportProgress(timeTemp);
}
}





更改图片方法

  public   void  ChangePicture() //  更改图片 
{
随机d = Random();
int index = d.Next( 0 ,images.Count);
FileInfo item = images.ElementAt(index);
pictureBox1.Image = Image.FromFile(item.FullName);
}





获取图像方法

  public   void  GetImagesOfType( string  type) //  将从源读取指定TYPE的所有图像 
{
FileInfo [] Img = di.GetFiles(type);
foreach (FileInfo im in Img)
images.Add(im);
}


请按照以下链接查找您的问题



http://www.jqueryscript.net/other/jQuery-Plugin-For-Random-Background-Image-randomBackground.html [ ^ ]



http://www.markinns.com/articles/full/simple_two_line_image_randomiser_script_with_jquery [ ^ ]



http:// stackoverflow.com/questions/19369426/random-background-image-on-refresh [ ^ ]



谢谢

AARIF SHAIKH


I want to develop a education game, I need to create a random picture box and a progress bar, after 10 seconds then picture box will change to another picture. Anybody knows how to code?

解决方案

The key here is: don't use PictureBox. I mean, you can use it if you have already available images, but if you want to generate anything random, or any other dynamic behavior, this control won't help at all, will only add troubles, extra waste of your development time and some resources. I'll tell you what to do instead:
Append a picture within picturebox[^],
draw a rectangle in C#[^],
How do I clear a panel from old drawing[^].

For more detail on graphics rendering, please see my past answer:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
capture the drawing on a panel[^],
Drawing Lines between mdi child forms[^],
How to speed up my vb.net application?[^].

Pay attention for the last answer. You may need to change the view on a non-UI thread (even if you use a timer), so you need UI thread invocation mechanism. Please see my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

If you use one specific timer, System.Windows.Forms.Timer, you won't need to use invocation. The problem with this timer is very low accuracy. But its accuracy, unacceptable for anything periodic, can suit your for a single use. 10 seconds delay (but why?) may not really require any accuracy.

—SA


Hello,
May be this will help you.
change the code as per your need...
I used Background worker (not timer)

PictureBox control name: picturebox1
Progress Bar Control name: progressbar1


first of all
create variables.

DirectoryInfo di;
List<FileInfo> images= new List<FileInfo>();
string dirpath = @"C:\Images"; // set your image directory path
int timeTemp = 0;
BackgroundWorker backgroundWorker = new BackgroundWorker
{
    WorkerReportsProgress = true,
    WorkerSupportsCancellation = true
};



In Form Load event

di = new DirectoryInfo(dirpath);

GetImagesOfType("*.jpg");//will add .jpg files to list - add other file types below
//GetImagesOfType("*.png"); 

backgroundWorker.DoWork += BackgroundWorkerOnDoWork;
backgroundWorker.ProgressChanged += BackgroundWorkerOnProgressChanged;
ChangePicture();
if (!backgroundWorker.IsBusy)
   backgroundWorker.RunWorkerAsync();



Add below events for Background Worker

private void BackgroundWorkerOnProgressChanged(object sender, ProgressChangedEventArgs e)
{
   progressBar1.Value = e.ProgressPercentage;
}

private void BackgroundWorkerOnDoWork(object sender, DoWorkEventArgs e)
{
   BackgroundWorker worker = (BackgroundWorker)sender;
   while (!worker.CancellationPending)
   {
    Thread.Sleep(1000);
    if (timeTemp < 100) timeTemp += 10;
    else
    {
       timeTemp = 0;
       ChangePicture();
    }
    worker.ReportProgress(timeTemp);
   }
}



Change Picture Method

public void ChangePicture()//to change pics
        {
            Random d = new Random();
            int index = d.Next(0, images.Count);
            FileInfo item = images.ElementAt(index);
            pictureBox1.Image = Image.FromFile(item.FullName);
        }



Get Images Method

public void GetImagesOfType(string type)// will read all the images of specified TYPE from the source
       {
           FileInfo[] Img = di.GetFiles(type);
           foreach (FileInfo im in Img)
               images.Add(im);
       }


Please Follow below links for your problem

http://www.jqueryscript.net/other/jQuery-Plugin-For-Random-Background-Image-randomBackground.html[^]

http://www.markinns.com/articles/full/simple_two_line_image_randomiser_script_with_jquery[^]

http://stackoverflow.com/questions/19369426/random-background-image-on-refresh[^]

Thanks
AARIF SHAIKH


这篇关于如何在1个图片框中随机运行大量图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆