通过线程渲染位图

发布于 2025-02-06 20:35:56 字数 1479 浏览 0 评论 0原文

我正在从许多像素中产生镜头。问题是将其呈现为0.8秒。我想通过螺纹实现这一目标,以免冻结我的GUI。

我很笨拙。我尝试了许多方法,但我无法正常工作。

我只需要这种方法才能工作,应该没事。

 volatile DrawingVisual dv = new DrawingVisual();
    volatile RenderTargetBitmap rtb;
    volatile DrawingContext dc;
    private async Task<Image> GetSpectogram(List<int[]> Lines)
    {               
            using (dc = dv.RenderOpen())
            {                  
                int y = -1;
                int x = -1;
               
                foreach (int[] list in Lines)
                {
                    x += 1;
                    for (int i = 0; i < list.Length; i++)
                    {
                        y += 1;
                        int value = list[i];
                        dc.DrawRectangle(GetRGBFromValue(value), null, new Rect(x, y, 1, 1));
                    }
                    y = -1;
                }
                dc.Close();
            }

       await Task.Factory.StartNew(() => {
          
           rtb = new RenderTargetBitmap(TotalLines, TotalPixelsInLine, 0, 0, PixelFormats.Default);
           rtb.Render(dv);

           Image img = new Image();
           img.Source = rtb;
           img.Height = rtb.Height * 3;
           img.VerticalAlignment = VerticalAlignment.Stretch;
           img.Stretch = Stretch.Uniform;

           return img;
       });

        return null;
    }

您能告诉我这里有什么问题,我想理解吗? 它在rtb.render(dv)上扔螺纹错误;

非常感谢 :)

I'm generating spectogram out of many pixels. Problem is that it takes 0.8 second to render it. I would like to achieve that with threading so It doesn't freeze my GUI.

I'm very clueless. I tried many approaches but I can't get it working.

I need only this method to work and it should be fine.

 volatile DrawingVisual dv = new DrawingVisual();
    volatile RenderTargetBitmap rtb;
    volatile DrawingContext dc;
    private async Task<Image> GetSpectogram(List<int[]> Lines)
    {               
            using (dc = dv.RenderOpen())
            {                  
                int y = -1;
                int x = -1;
               
                foreach (int[] list in Lines)
                {
                    x += 1;
                    for (int i = 0; i < list.Length; i++)
                    {
                        y += 1;
                        int value = list[i];
                        dc.DrawRectangle(GetRGBFromValue(value), null, new Rect(x, y, 1, 1));
                    }
                    y = -1;
                }
                dc.Close();
            }

       await Task.Factory.StartNew(() => {
          
           rtb = new RenderTargetBitmap(TotalLines, TotalPixelsInLine, 0, 0, PixelFormats.Default);
           rtb.Render(dv);

           Image img = new Image();
           img.Source = rtb;
           img.Height = rtb.Height * 3;
           img.VerticalAlignment = VerticalAlignment.Stretch;
           img.Stretch = Stretch.Uniform;

           return img;
       });

        return null;
    }

Can you please tell me what is the problem here, I would like to understand it?
It is throwing threading error at rtb.Render(dv);

Thank you very much :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文