Category / Section
How to read image byte while rendering WPF Chart (SfChart)?
1 min read
Reading a WPF Chart (SfChart) as a Stream of byte, and save it in image format while rendering can be achieved by using the following codes. An FileStream supports both read and write operations. In this example, the chart is read as a Stream of bytes and saved as an image. The RenderTargetBitmap class specifies the target chart width, height, and pixel of image in pixel format. The PngBitmapEncoder encodes the image to PNG Format. After the chart has been renderd, if we click the button, the chart image will be 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); } }