簡體   English   中英

創建WriteableBitmap表單圖表問題

[英]Creating WriteableBitmap form chart issue

嘗試創建可寫位圖表單Silverlight工具包圖時出現問題。

使用textBlock時,一切都很好,但是嘗試使用Chart之后,生成的位圖為空:(。

var data = new List<Point>(100);
for (int i = 0; i < 100; i++)
{
    data.Add(new Point(i, Math.Sin(i * Math.PI / 50)));
}

Chart chart_ = new Chart()
{
    Name = "Chart",
    Width = 512,
    Height = 512
};
LineSeries line = new LineSeries()
{
    Name = "Line",
    Title = "test",
    IndependentValuePath = "X",
    DependentValuePath = "Y",
    ItemsSource = data
};
chart_.Series.Add(line);

此代碼創建包含正弦曲線的圖表。 然后我試圖從中創建位圖。

LayoutRoot.Children.Add(chart_); // I tried to add chart_ to visual tree, It doesn't help 
//creates bitmap
ScaleTransform t = new ScaleTransform() { ScaleX = 1.0, ScaleY = 1.0 };
//bitmap = new WriteableBitmap(chart_, t); Tried it also with this way
bitmap = new WriteableBitmap(512, 512);
bitmap.Render(chart_, t);
texture = new Texture2D(GraphicsDeviceManager.Current.GraphicsDevice, bitmap.PixelWidth, bitmap.PixelHeight, false, SurfaceFormat.Color);
bitmap.CopyTo(texture);

所有這些代碼都創建了Empty Bitmap,但是當我使用TextBlock或諸如Ellipse之類的原語時,一切正常。 我確定代碼生成圖表很好,因為在Silverlight控件中生成圖表很好。

編輯:我試圖以這種方式創建位圖,但沒有幫助。

chart_.InvalidateMeasure();
bitmap = new WriteableBitmap(512, 512);
bitmap.Render(chart_, null);
bitmap.Invalidate();

編輯2:我不希望圖形在視覺樹中。 我只需要生成它的映像即可,而不是在我的應用程序的XNA部分中使用它。

只是幾個小的更改,但是我懷疑最大的更改是在使用NuGet添加Charting程序包之后手動添加對System.Windows.Controls的引用。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.DataVisualization.Charting;
using System.Windows.Media.Imaging;

namespace SilverlightApplication1
{
    public partial class MainPage : UserControl
    {
        Chart chart_;

        public MainPage()
        {
            InitializeComponent();

            var data = new List<Point>(100);
            for (int i = 0; i < 100; i++)
            {
                data.Add(new Point(i, Math.Sin(i * Math.PI / 50)));
            }

            chart_ = new Chart()
            {
                Name = "Chart",
                Width = 512,
                Height = 512
            };
            LineSeries line = new LineSeries()
            {
                Name = "Line",
                Title = "test",
                IndependentValuePath = "X",
                DependentValuePath = "Y",
                ItemsSource = data
            };
            chart_.Series.Add(line);
            LayoutRoot.Children.Add(chart_); // I tried to add chart_ to visual tree, It doesn't help  
        }

        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
        {
            //creates bitmap 
            WriteableBitmap bitmap;
            ScaleTransform t = new ScaleTransform() { ScaleX = 1.0, ScaleY = 1.0 };
            //bitmap = new WriteableBitmap(chart_, t); Tried it also with this way 
            bitmap = new WriteableBitmap(512, 512);
            bitmap.Render(chart_, t);
            //texture = new Texture2D(GraphicsDeviceManager.Current.GraphicsDevice, bitmap.PixelWidth, bitmap.PixelHeight, false, SurfaceFormat.Color);
            //bitmap.CopyTo(texture); 
            image1.Source = bitmap;
        }
    }
}

<UserControl x:Class="SilverlightApplication1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400" 
    xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit">

    <Grid x:Name="LayoutRoot" Background="White" Loaded="LayoutRoot_Loaded">
        <Image Height="150" HorizontalAlignment="Left" Margin="97,109,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="200" />
    </Grid>
</UserControl>

那應該讓你走...

不要忘記刪除所有嘗試的解決方法-我認為沒有任何必要。 我在您的示例中沒有使用的唯一代碼與紋理有關-我不知道該怎么做,這也不是您遇到的問題...

大衛

我相信,當您創建WriteableBitmap對象並調用Render方法時,Chart尚未呈現。 您可以通過將這些代碼行移至其他事件(如Button_Click等)來進行檢查。在創建WriteableBitmap並將其作為源傳遞給圖像控件時,請查看以下代碼。

XAML代碼.....

<UserControl x:Class="TryBitmap.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400"
xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit">
<Grid x:Name="LayoutRoot" Background="White">
    <Grid.RowDefinitions>
        <RowDefinition Height="35"/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Button Grid.Row="0" Height="25" Width="100" Content="Capture" Click="Button_Click"/>
    <Grid x:Name="grdGraphs" Grid.Row="1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Image x:Name="img" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center"/>
    </Grid>
</Grid>

代碼背后...

public partial class MainPage : UserControl
{
    private Chart chart_;

    public MainPage()
    {
        InitializeComponent();
        Loaded += new RoutedEventHandler(MainPage_Loaded);
    }

    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        var data = new List<Point>(100);
        for (int i = 0; i < 100; i++)
        {
            data.Add(new Point(i, Math.Sin(i * Math.PI / 50)));
        }

        chart_ = new Chart()
        {
            Name = "Chart",
            Width = 512,
            Height = 512
        };
        LineSeries line = new LineSeries()
        {
            Name = "Line",
            Title = "test",
            IndependentValuePath = "X",
            DependentValuePath = "Y",
            ItemsSource = data
        };
        chart_.Series.Add(line);

        chart_.SetValue(Grid.ColumnProperty, 0);
        grdGraphs.Children.Add(chart_);
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var bitmap = new WriteableBitmap((int)(chart_.RenderSize.Width), (int)(chart_.RenderSize.Height));
        bitmap.Render(chart_, new MatrixTransform());
        bitmap.Invalidate();

        img.Source = bitmap;
    }
}

恐怕這只能解決一半。 我可以解決您的迫在眉睫的問題,但是恐怕您會遇到另一個我沒有解決的問題。

在調用Dispatcher.BeginInvoke之后創建位圖應確保您的位圖不是完全空白,即:

// do stuff with chart_ ...

Dispatcher.BeginInvoke(() =>
{
    bitmap = new WriteableBitmap(512, 512);
    bitmap.Render(chart_, null);
    bitmap.Invalidate();

    // At this point, the bitmap shouldn't be blank.
});

但是,這樣做的結果可能不太令人滿意。 我運行了您的代碼,發現圖表圖像中缺少LineSeries,盡管圖表的其余部分在那里。 即使我將LineSeriesControlTemplate中的各種動畫的所有Duration都設置為0,並在圖表上另外設置了以下屬性,也仍然如此:

    chart_.AnimationSequence = AnimationSequence.Simultaneous;
    chart_.TransitionDuration = new TimeSpan(0);

我嘗試將WriteableBitmap操作包裝在對Dispatcher.BeginInvoke()進一步調用中,然后完成后,數據點開始顯得更加清晰。 但是,我無法相信這樣的方法是解決問題的正確方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM