Reputation: 33
I need to save and print an image from cartesian live chart, I've searched in docs and tutorials but I can't find anything. How can I do this? I'm using WinForms and C# with Visual Studio 2012 Express.
Upvotes: 3
Views: 7041
Reputation: 49
You can simply convert the graph control to a bitmap as shown in this answer and then save it to a file. Using the graph control (cartesianChart1
) from the LiveCharts example:
Bitmap bmp = new Bitmap(cartesianChart1.Width, cartesianChart1.Height);
cartesianChart1.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
bmp.Save("C:\\graph.png", ImageFormat.Png);
This works even if your graph is behind another window.
Use Tatranskymedved's answer to print the image.
Upvotes: 3
Reputation: 4371
As it seems that the graph itself does not support printing / taking screens to image, I would suggest creating this functionality Yourself.
What are You going to need?
-
ScreenCapture sc = new ScreenCapture();
// capture entire screen, and save it to a file
Image img = sc.CaptureScreen();
// display image in a Picture control named imageDisplay
this.imageDisplay.Image = img;
// capture this window, and save it
sc.CaptureWindowToFile(this.Handle,"C:\\temp2.gif",ImageFormat.Gif);
In case You need just printscreen of control: C# Take ScreenShot of .net control within application and attach to Outlook Email
-
public static void TakeCroppedScreenShot( string fileName, int x, int y, int width, int height, ImageFormat format)
{
Rectangle r = new Rectangle(x, y, width, height);
Bitmap bmp = new Bitmap(r.Width, r.Height, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(r.Left, r.Top, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy);
bmp.Save(fileName, format);
}
-
using System.Drawing.Printing;
...
protected void btnPrint_Click(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += PrintPage;
pd.Print();
}
private void PrintPage(object o, PrintPageEventArgs e)
{
System.Drawing.Image img = System.Drawing.Image.FromFile("D:\\Foto.jpg");
Point loc = new Point(100, 100);
e.Graphics.DrawImage(img, loc);
}
Upvotes: 1