RenderTargetBitmap 获取我的 UserControl 的空图像

RenderTargetBitmap takes an empty image of my UserControl

我正在 WPF 上开发每周调度程序。 我处理了一切,但我有一个问题。 RenderTargetBitmap 拍摄了我的 UserControl 的空白图像,我也尝试过 element.ApplyTmeplate(); 它对我不起作用。

正在从数据库获取数据

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        sqlConnection = new SqlConnection(connectionString);
        sqlConnection.Open();

        query = "SELECT dp.ID, d.DersAdi, a.Ad + ' ' + a.Soyad AS [Akademisyen], b.BolumAdi, dp.BaslangicSaati, dp.BitisSaati, g.GunAdi FROM DerslikProgrami dp INNER JOIN Dersler d ON d.ID = dp.DersID INNER JOIN Akademisyenler a ON a.ID = dp.AkademisyenID INNER JOIN Gunler g ON g.ID = dp.GunID INNER JOIN Derslikler dl ON dl.ID = dp.DerslikID INNER JOIN Bolumler b ON b.ID = dp.BolumID INNER JOIN Fakulteler f ON f.ID = b.FakulteID INNER JOIN Binalar bi ON bi.ID = dl.BinaID WHERE d.Sınıf = 1";
        sqlCommand = new SqlCommand(query, sqlConnection);
        dataRead = sqlCommand.ExecuteReader();
        while (dataRead.Read()) //Filling my scheduler with Task objects (Task is another usercontrol)
        {
            int ID = dataRead.GetInt32(0);
            string lesson = dataRead.GetString(1);
            string academician = dataRead.GetString(2);
            string department = dataRead.GetString(3);
            TimeSpan startTime = dataRead.GetTimeSpan(4);
            TimeSpan finishTime = dataRead.GetTimeSpan(5);
            string day = dataRead.GetString(6);
            string type = "Teorik";

            LessonTask lessonTask = new LessonTask()
            {
                ID = ID,
                Academician = academician,
                LessonName = lesson,
                LessonDay = day,
                LessonType = type,
                Department = department,
                StartTime = startTime,
                FinishTime = finishTime
            };
            LessonTask.LessonTasks.Add(lessonTask);

            Task task = new Task(lessonTask, this.pnlSchedulerSinif); //Usercontrol takes Lesson task for accessing data and takes the SchedulerPanel as a Parent.
            LessonTask.Tasks.Add(task);
        }

        foreach (Canvas cnv in pnlSchedulerSinif.pnlAna.Children.OfType<Canvas>())
        {
            foreach (Task t1 in cnv.Children.OfType<Task>())
            {
                foreach (Task t2 in cnv.Children.OfType<Task>())
                {
                    if (t1.ID != t2.ID)
                    {
                        if (t2.StartTime >= t1.StartTime && t2.StartTime <= t1.FinishTime)
                        {
                            Canvas.SetLeft(t2, 100);
                            t2.Width = 100;
                            Canvas.SetLeft(t1, 0);
                            t1.Width = 100;
                        }
                    }
                }
            }
        }

        sqlConnection.Close();

        //Taking image of my scheduler.
        RenderTargetBitmap rtb = new RenderTargetBitmap((int)pnlSchedulerSinif.Width, (int)pnlSchedulerSinif.Height, 96, 96, PixelFormats.Pbgra32);
        rtb.Render(pnlSchedulerSinif);

        PngBitmapEncoder png = new PngBitmapEncoder();
        png.Frames.Add(BitmapFrame.Create(rtb));
        MemoryStream stream = new MemoryStream();
        png.Save(stream);
        System.Drawing.Image image = System.Drawing.Image.FromStream(stream);
        image.Save(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\test.png", System.Drawing.Imaging.ImageFormat.Png);

    }

TaskUserControl 使用此方法中构造函数发送的数据加载组件

    private void LoadComponents()
    {
        txtTime.Text = this.StartTime.ToString(@"hh\:mm") + " - " + this.FinishTime.ToString(@"hh\:mm");
        txtLesson.Text = this.LessonName;
        txtLessonType.Text = this.LessonType;
        txtDepartment.Text = this.Department;
        txtAcademician.Text = this.Academician;

        TimeSpan difference = this.FinishTime - this.StartTime;

        Canvas.SetTop(this, this.StartTime.TotalMinutes - DayStartTime.TotalMinutes);
        this.Height = difference.TotalMinutes;

        switch (this.LessonDay)
        {
            case "Pazartesi":
                this.PanelScheduler.cnvMonday.Children.Add(this); //adding to the canvas.
                break;
            case "Salı":
                this.PanelScheduler.cnvTuesday.Children.Add(this); //adding to the canvas.
                break;
            case "Çarşamba":
                this.PanelScheduler.cnvWednesday.Children.Add(this); //adding to the canvas.
                break;
            case "Perşembe":
                this.PanelScheduler.cnvThursday.Children.Add(this); //adding to the canvas.
                break;
            case "Cuma":
                this.PanelScheduler.cnvFriday.Children.Add(this); //adding to the canvas.
                break;
        }
        Canvas.SetZIndex(this, 2);
    }

截图 [ScreenShot]

RenderTargetBitmap 拍摄的空图像 [Taken Image]

编辑:回答后由 RenderTargetBitmap 拍摄的图像。 [After The Answer]

您只需使用该方法即可。

element.UpdateLayout();

还有

element.ApplyTemplate();

当我处理这个问题时,我也没有用。我不知道,但我以某种方式找到了这个答案。