如何在c#中混合两种不同方法的输出图像?
本文关键字:方法 输出 图像 两种 混合 | 更新日期: 2023-09-27 17:50:55
我有一个问题,
我有一个基于c#的应用程序,它有两行图像制作:
Line-1根据一些数学计算每秒生成一系列帧。每一帧(图像)由黑白像素组成,它们形成一个图案。
Line-2正在生成另一系列基于随机噪声发生器生成的帧。所以它们只是不同的帧,只包含噪声!现在,我的问题是我需要把这两条线的帧随机地混合在一起。这意味着,例如,我需要从第一行选择5帧,从第二行选择3帧,然后将它们随机混合在一起。此混合程序将随机更改。
我自己的解决方案是,如果我将第一行生产的图像存储在一个单独的列表中,并将第2行生产的图像存储在另一个列表中,那么我可以从这两个列表中随机选择一个标志。但事实上,这些图像是实时生成的,我不知道这个解是否有效。谁有任何替代解决方案,我的问题?;)
我将使用队列(Queue(T)
或ConcurrentQueue(T)
)来存储图像。假设您使用一个线程来填充每个队列,并使用一个线程来从两个队列中消费。
的例子:
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;
}