This is code will add Save Tool Tip in the for print preview and also saving the picturebox as a PDF format.
class SavePrint : System.Windows.Forms.PrintPreviewDialog
{
public SavePrint()
: base()
{
if (this.Controls.ContainsKey("toolstrip1"))
{
ToolStrip tStrip1 = (ToolStrip)this.Controls["toolstrip1"];
ToolStripButton button1 = new ToolStripButton();
button1.Text = "Save";
button1.Click += new EventHandler(SaveDocument);
button1.Visible = true;
tStrip1.Items.Add(button1);
}
}
public void SaveDocument(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "Save As PDF";
sfd.Filter = "PDF|*.pdf";
sfd.InitialDirectory = @"Desktop";
if (sfd.ShowDialog() == DialogResult.OK)
{
Bitmap bmp = new Bitmap(pictureBox.Image);//GETTING THE ERROR HERE
Graphics gr = Graphics.FromImage(bmp);
PdfDocument doc = new PdfDocument();
doc.Pages.Add(new PdfPage());
XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[0]);
xgr.DrawImage(bmp, 0, 0);
doc.Save(sfd.FileName);
doc.Close();
}
}
}
im getting the error at this line "Bitmap bmp = new Bitmap(pictureBox.Image)" what should i do so it can inherit my pictureBox?
Change the constructor of your class SavePrint
to have the following signature:
class SavePrint : System.Windows.Forms.PrintPreviewDialog
{
readonly Form1 parent;
public SavePrint(Form1 parent)
: base()
{
this.parent = parent;
// Remainder as before
}
}
Then when you construct your SavePrint
, pass the appropriate instance Form1
in to the constructor.
SavePrint savePrint = new SavePrint(this);
Having done that, your SavePrint
will be able to access its fields and properties:
Bitmap bmp = new Bitmap(parent.pictureBox.Image);
By the way, you should wrap all of your disposables in using
statements, like so:
using (Bitmap bmp = new Bitmap(parent.pictureBox.Image))
using (Graphics gr = Graphics.FromImage(bmp))
{
}