如何在C#中混合来自两种不同方法的输出图像? [英] How can I mix output images from two different methods in C#?
问题描述
我遇到如下问题,
我有一个基于C#的应用程序,它有两行图像生成:
I have an application based on C# which has two lines of image production:
Line-1基于某些数学计算每秒生成一系列帧。每个帧(图像)由黑色和白色像素组成,它们形成一个模式。
Line-1 generates a series of frames per second based on some mathematical calculation. Each frame (image) is consist of black and white pixels which they form a pattern.
Line-2生成另一系列基于随机噪声发生器的帧。所以它们只是不同的帧,只包含噪音!
现在,我的问题是我需要将这两行中的帧随机混合在一起。通过这意味着,例如,我需要从第1行选择5帧,从第2行选择3帧,依此类推,将它们随机混合在一起。这种混合程序将随机改变。
Line-2 is generating another series of frames which are made based on random noise generator. So they are just different frames which contain only noise! Now, my question is I need to mix this frames from these two lines together randomly. By meaning that, for example I need to select 5 frames from line-1 and 3 frames from line-2 and so on mixing them together randomly. This mixing procedure will change randomly.
我自己的解决方案是,如果我将第1行生产中的图像存储在单独的列表中,而将第2行生成存储在另一个列表中,那么我可以有一个标志从这两个列表中随机选择。但是,由于这些图像实时生成,我不知道该解决方案是否有效。有没有人对我的问题有任何替代解决方案?;)
My own solution is if I store images from line-1 production in a separate list, and line-2 production in another list so I can have a flag to select from these two lists randomly. But as a fact that this images are generating real time I don’t know if that solution works or not. Does anyone have any alternative solution for my question?;)
推荐答案
我会使用队列(队列( T)
或 ConcurrentQueue(T)
)来存储图像。假设您使用一个线程来填充每个队列,并使用一个线程从两个队列中消耗。
I would use queues (Queue(T)
or ConcurrentQueue(T)
) to store the images. Assuming you use a thread to fill each queue and one to consume from both queues.
示例:
private ConcurrentQueue<Bitmap> line1 = new ConcurrentQueue<Bitmap>();
private ConcurrentQueue<Bitmap> line2 = new ConcurrentQueue<Bitmap>();
private Random randomGenerator = new Random();
//thread 1
private void FillLine1()
{
//your line 1 image producation code
Bitmap yourCalculatedBitmap = new Bitmap(100,100);
line1.Enqueue(yourCalculatedBitmap);
}
//thread 2
private void FillLine2()
{
//your line 2 image production code
Bitmap yourCalculatedBitmap = new Bitmap(100,100);
line1.Enqueue(yourCalculatedBitmap);
}
//thread 3
private Bitmap RandomImageSelection()
{
Bitmap image;
if (randomGenerator.Next(2) == 0 && line1.TryDequeue(out image))
{
return image;
}
if (line2.TryDequeue(out image))
{
return image;
}
return null;
}
这篇关于如何在C#中混合来自两种不同方法的输出图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!