【问题标题】:How can I mix output images from two different methods in C#?如何在 C# 中混合来自两种不同方法的输出图像?
【发布时间】:2014-03-18 12:34:08
【问题描述】:

我有以下问题,

我有一个基于 C# 的应用程序,它有两行图像生成:

Line-1 根据一些数学计算每秒生成一系列帧。每一帧(图像)由黑白像素组成,它们形成一个图案。

第 2 行正在生成另一系列基于随机噪声生成器制作的帧。所以它们只是不同的帧,只包含噪声! 现在,我的问题是我需要将这两行中的这些帧随机混合在一起。这意味着,例如,我需要从第 1 行中选择 5 帧,从第 2 行中选择 3 帧,依此类推,将它们随机混合在一起。这个混合过程会随机变化。

我自己的解决方案是,如果我将生产线 1 的图像存储在单独的列表中,将生产线 2 的图像存储在另一个列表中,这样我就可以从这两个列表中随机选择一个标志。但事实上,这些图像是实时生成的,我不知道该解决方案是否有效。有人对我的问题有任何替代解决方案吗?;)

【问题讨论】:

    标签: c# image list


    【解决方案1】:

    我会使用队列(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;
      }
    

    【讨论】:

    • 谢谢你的评论,我可以通过“line1.Enqueue(/*bitmap*/);”中的“/*bitmap*/”问你吗?
    • 还有为什么你把 2 作为括号内的值?
    • /*bitmap*/ 只是一个占位符。在那里,您将创建的位图添加到队列中。 randomGenerator.Next(2) 生成 0 或 1。因此,此代码从 line1 获取大约 50% 的图像,其余的从 line2 获取。
    • “图像”是我的最终输出吗?你的意思是如果我把“pictureBox1.Image = image;”它会随机显示两行的图像吗?
    • “图像”是我的最终输出吗?你来定义。但我认为它应该可以工作,但我并不是 Windows 窗体中的真正出口。补充: -> 当你设置图片时,你必须在 UI 线程上。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多