How to read image byte while rendering a WPF Chart (SfChart)?
This article explains how to read a WPF Chart (SfChart) as a stream of bytes and save it in an image format during rendering. A FileStream supports both read and write operations. In this example, a chart is captured as a stream of bytes and saved as a PNG image. The RenderTargetBitmap class specifies the target chart's width, height, and pixel format. The PngBitmapEncoder is used to encode the image to PNG format. When the button is clicked, the rendered chart image is generated in the bin\Debug folder.
C#
private void ImageExport_Click(object sender, RoutedEventArgs e)
{
const string path = "Chart.png";
RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)ScatterChart.ActualWidth, (int)ScatterChart.ActualHeight,70d, 70d, PixelFormats.Pbgra32);
renderBitmap.Render(ScatterChart);
using (FileStream ostream = new FileStream(path, FileMode.Create))
{
PngBitmapEncoder Bitmapencoder = new PngBitmapEncoder();
Bitmapencoder.Frames.Add(BitmapFrame.Create(renderBitmap));
// encoder.Save(outStream);
ScatterChart.Save(ostream, Bitmapencoder);
}
}
Output
Conclusion
I hope you enjoyed learning about how to read image byte while rendering a WPF Chart(SfChart).
You can refer to our WPF Chart’s feature tour page to know about its other groundbreaking feature representations. You can also explore our WPF Chart documentation to understand how to present and manipulate data.
For current customers, you can check out our WPF components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our WPF Chart and other WPF components.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!