using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Mindscape.WpfElements.Charting;
namespace WpfApplication2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Image myImage = new Image();
myImage.Source = GetBitmap();
Grid myStackPanel = new Grid();
myStackPanel.Children.Add(myImage);
this.Content = myStackPanel;
}
public RenderTargetBitmap GetBitmap()
{
int height, width;
height = 800;
width = 800;
var size = new Size(height, width);
Grid mainContainer = new Grid();
mainContainer.HorizontalAlignment = HorizontalAlignment.Stretch;
mainContainer.VerticalAlignment = VerticalAlignment.Stretch;
Chart ch = new Chart();
ch.HorizontalAlignment = HorizontalAlignment.Stretch;
ch.VerticalAlignment = VerticalAlignment.Stretch;
var d = new LineSeries();
var ps = new List<Point>();
ps.Add(new Point(0, 0));
ps.Add(new Point(10, 10));
ps.Add(new Point(20, 20));
d.ItemsSource = ps;
ch.Series.Add(d);
ch.Title = "Test";
mainContainer.Children.Add(ch);
mainContainer.Measure(size);
mainContainer.Arrange(new Rect(size));
PngBitmapEncoder encoder = new PngBitmapEncoder();
RenderTargetBitmap render = new RenderTargetBitmap(width,height,96,96,PixelFormats.Pbgra32);
render.Render(mainContainer);
return render;
}
}
}
------解决方案--------------------
你不妨把位图保存到文件里,看看到底是不是一个正常的图片。