Я работаю над еженедельным планировщиком на 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 [ScreenShot]
Пустое изображение, снятое RenderTargetBitmap [Снятое изображение]
Редактировать: Изображение, снятое RenderTargetBitmap после ответа. [После ответа]