This is a migrated thread and some comments may be shown as answers.

PngBitmapEncoder render Empty Image when use WindowsService project

0 Answers 80 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Ahmad
Top achievements
Rank 1
Ahmad asked on 06 Mar 2018, 04:42 PM
I Have a WindowsService project.In this service when I render any UserControl, the png file is empty.

for test i write the following code to export a TextBlock but result is an empty png file.

protected override void OnCustomCommand(int command)
{
    Export();

    base.OnCustomCommand(command);
}

private void Export()
{
    var thread = new Thread(() =>
    {

        var grid = new Grid();
        grid.Children.Add(new TextBlock() { Text = "ExportTest", FontSize = 60, Background = Brushes.Red, Foreground = Brushes.Blue });
        double widthg = grid.Width > 0 ? grid.Width : 1024;
        double heightg = grid.Height > 0 ? grid.Height : 768;
        grid.Measure(Size.Empty);
        grid.Measure(new Size(widthg, heightg));
        grid.Arrange(new Rect(0, 0, widthg, heightg));
        grid.UpdateLayout();

        var bitmap = new PngBitmapEncoder();
        ExportExtensions.ExportToImage(grid, @"..\ExportTest.png", bitmap);
    });

    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();
    thread.Join();
}

Note: when I run the export function in wpf project, it works perfect.

there is my project:
http://www.mediafire.com/file/aij5sg4zx80yc73/ExportProject.rar
any body can help me?

No answers yet. Maybe you can help?

Tags
General Discussions
Asked by
Ahmad
Top achievements
Rank 1
Share this question
or