08-30-2012 02:41 PM
Hi all,
I have a graph with a some vertical cursors. The problem is that I want to print the graph but without the cursors so what I have to make invisible them before printing, then print the graph, and finally, after printing the graph to make visible them again. The problem by doing this is that the user see how the cursors are hidden before printing and shown after printing and I don't want this. So is there a way to print the graph without the cursors when they are currently visible?
Thx.
Solved! Go to Solution.
09-06-2012 03:04 PM
Hi there,
I found a fairly easy workaround for this.
Look at the code below.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
namespace PrintingWithoutCursors
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
xyCursor1.BeforeDraw += new NationalInstruments.UI.BeforeDrawXYCursorEventHandler(xyCursor1_BeforeDraw);
}
private bool printing = false;
void xyCursor1_BeforeDraw(object sender, NationalInstruments.UI.BeforeDrawXYCursorEventArgs e)
{
if (printCursorCheckBox.Checked == false && printing == true)
{
// Cancel the cursor drawing if
// 1. Print button is clicked, that is printing = true.
// and 2. print cursor checkbox is checked..
// Make the cursor not to be drawn by cancelling the drawing.
e.Cancel = true;
}
}
private void button1_Click(object sender, EventArgs e)
{
//Print graph here..
printing = true;
Print();
printing = false;
}
private void Print()
{
// Write code to print here
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
pd.Print();
}
Bitmap b;
void pd_PrintPage(object sender, PrintPageEventArgs e)
{
// create a bitmap
b = new Bitmap(waveformGraph1.Width, waveformGraph1.Height);
// Draw the graph to a bitmap
waveformGraph1.DrawToBitmap(b, new Rectangle(0, 0, b.Width, b.Height));
// draw the bitmap on the printer graphics object
e.Graphics.DrawImage(b, Point.Empty);
}
}
}
This is how my screen looks:
If the checkbox is checked, cursor is printed, otherwise, no cursor gets printed. But the graph on the form always shows the cursor.
Hope this helps.
Regards,
Vijet Patankar
National Instruments