1

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);

このコードはすべて空のビットマップを作成しますが、TextBlockまたはEllipseなどのプリミティブを使用すると、すべてが機能します。確かに、コード生成チャートは問題ありません。原因チャートはSilverlightコントロールで問題なく生成されます。

編集: 私はこの方法でビットマップを作成しようとしましたが、役に立ちません。

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

編集2: グラフをビジュアルツリーに入れたくない。アプリケーションのXNA部分で使用するよりも、イメージを生成する必要があります。

4

3 に答える 3

0

私はこれが半分の答えになるのではないかと心配しています。私はあなたの当面の問題を解決することができますが、私が解決できなかった別の問題になってしまうのではないかと心配しています。

の呼び出し後にビットマップを作成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がグラフの画像から欠落していることがわかりました。これはDuration、のさまざまなアニメーションの ControlTemplateすべてLineSeriesを0に設定し、さらにチャートに次のプロパティを設定した後も当てはまりました。

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

WriteableBitmap操作をへのさらなる呼び出しでラップしようとしましたがDispatcher.BeginInvoke()、これを実行した後、データポイントがより明確に表示され始めました。しかし、このようなアプローチが問題の正しい解決策であるとは信じられません。

于 2012-01-02T12:07:53.047 に答える
0

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;
    }
}
于 2012-01-02T06:45:36.887 に答える
0

小さな変更がいくつかありますが、大きな変更は、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>

それはあなたを動かすはずです...

試行されたすべての回避策を削除することを忘れないでください - それらのどれも必要ではないと思います。あなたの例から使用しなかった唯一のコードは、テクスチャに関係していました-それをどうするかわかりませんでした。

デビッド

于 2011-12-30T01:58:57.913 に答える