I am trying to print a form using this code:
private void btnPrint_Click(object sender, EventArgs e)
{
Graphics g1 = this.CreateGraphics();
Image MyImage = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height, g1);
Graphics g2 = Graphics.FromImage(MyImage);
IntPtr dc1 = g1.GetHdc();
IntPtr dc2 = g2.GetHdc();
BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
g1.ReleaseHdc(dc1);
g2.ReleaseHdc(dc2);
MyImage.Save(#"c:\PrintPage.jpg", ImageFormat.Jpeg);
FileStream fileStream = new FileStream(#"c:\PrintPage.jpg", FileMode.Open, FileAccess.Read);
StartPrint(fileStream, "Image");
fileStream.Close();
if (System.IO.File.Exists(#"c:\PrintPage.jpg"))
{
System.IO.File.Delete(#"c:\PrintPage.jpg");
}
}
But is gives me an error at: MyImage.Save.
The error:
ExternalException was Unhandled: A generic error occurred in GDI+.
Can someone give me a fix for this problem,and explain, why I'am getting this error?
Thanks in Advance!
private void btn_print_Click(object sender, EventArgs e)
{
CaptureScreen();
printDocument1.Print();
printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
}
Bitmap memoryImage;
private void CaptureScreen()
{
Graphics myGraphics = this.CreateGraphics();
Size s = this.Size;
memoryImage = new Bitmap(s.Width, s.Height, myGraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
memoryGraphics.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s);
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
}
private void printDocument1_PrintPage_1(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawImage(memoryImage, 0, 0);
}
Add a print_document by dragging it from the toolbox to form. Execute this code, it will work fine.
http://csharpprobsandsoln.blogspot.in/2013/04/print-windows-form-in-c.html
Add:
using System.Drawing.Printing;
and in your code, add:
PrintDocument printDocument1 = new PrintDocument();
More info here
I had the same task and I have created a class (like printable form).
All you have to do is to inherit your Form from that class and call PrintForm(); method.
Here is the class:
public class CustomForm : Form
{
protected Bitmap _prnImage;
protected PrintPreviewDialog _prnpreviewdlg = new PrintPreviewDialog();
protected PrintDocument _printdoc = new PrintDocument();
public CustomForm()
{
_printdoc.PrintPage += new PrintPageEventHandler(printdoc_PrintPage);
}
protected void PrintForm()
{
Form form = this;
_prnImage = new Bitmap(form.Width, form.Height);
form.DrawToBitmap(_prnImage, new Rectangle(0, 0, form.Width, form.Height));
_printdoc.DefaultPageSettings.Landscape = true;
_printdoc.DefaultPageSettings.Margins = new Margins(10, 10, 10, 10);
_prnpreviewdlg.Document = _printdoc;
_prnpreviewdlg.ShowDialog();
}
protected void printdoc_PrintPage(object sender, PrintPageEventArgs e)
{
Rectangle pagearea = e.PageBounds;
e.Graphics.DrawImage(_prnImage, e.MarginBounds);
}
protected override void OnPaint(PaintEventArgs e)
{
if (_prnImage != null)
{
e.Graphics.DrawImage(_prnImage, 0, 0);
base.OnPaint(e);
}
}
}
I understand that interface is a more academic and correct way to do that but in my particular case that solution satisfied me.
Related
I'm trying to print windows forms and it is printing well but the issue is I want to print the form to the whole page. As you guys can see the the end of the page is empty. Is there a way to stretch is or something as solution. I'm new to C# and windows forms and don't know much, kindly anyone help !!. Explanation which is easy to understand.
here is my code:
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(bitmap, 0, 0);
}
private void Print_Click(object sender, EventArgs e)
{
Panel panel = new Panel();
this.Controls.Add(panel);
Graphics grp = panel.CreateGraphics();
Size formSize = this.ClientSize;
bitmap = new Bitmap(formSize.Width, formSize.Height, grp);
grp = Graphics.FromImage(bitmap);
Point panelLocation = PointToScreen(panel.Location);
grp.CopyFromScreen(panelLocation.X, panelLocation.Y, 0, 0, formSize);
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.PrintPreviewControl.Zoom = 1;
printPreviewDialog1.ShowDialog();
}
Bitmap bitmap;
private void CaptureScreen()
{
Graphics myGraphics = this.CreateGraphics();
Size s = this.Size;
bitmap = new Bitmap(s.Width, s.Height, myGraphics);
Graphics memoryGraphics = Graphics.FromImage(bitmap);
memoryGraphics.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s);
}
I have a Panel that has exactly 4 picture boxes, and labels inside. I want to print all the contents inside the Panel but it is only showing the two picture boxes - in the preview.
I have this:
But it is only showing this (when I click the print button):
Here is my code for printing:
private void BtnPrint_Click(object sender, EventArgs e)
{
Print(this.pnlID);
}
public void Print(Panel pnl)
{
PrinterSettings ps = new PrinterSettings();
pnlID = pnl;
GetPrintArea(pnl);
prntprvw.Document = printdoc;
printdoc.PrintPage += new PrintPageEventHandler(printdoc_printpage);
prntprvw.ShowDialog();
}
public void printdoc_printpage(Object sender, PrintPageEventArgs e)
{
Rectangle pagearea = e.PageBounds;
e.Graphics.DrawImage(MemoryImage, (pagearea.Width / 2) - (this.pnlID.Width / 2), this.pnlID.Location.Y);
}
Bitmap MemoryImage;
public void GetPrintArea(Panel pnl)
{
MemoryImage = new Bitmap(pnl.Width, pnl.Height);
//Rectangle rect = new Rectangle(0, 0, pnl.Width, pnl.Height);
pnl.DrawToBitmap(MemoryImage, new Rectangle(0, 0, pnl.Width, pnl.Height));
}
How can I print all the contents inside the panel?
The code below is showing me half part of the group box in print preview:
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) {
// Bitmap bmp = new Bitmap(groupBox1.ClientRectangle.Width, groupBox1.ClientRectangle.Height);
// groupBox1.DrawToBitmap(bmp, groupBox1.ClientRectangle);
// e.Graphics.DrawImage(bmp, 0, 0);
Bitmap bmp = new Bitmap(groupBox1.Width, groupBox1.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
groupBox1.DrawToBitmap(bmp, new Rectangle(0, 0, groupBox1.Width, groupBox1.Height));
e.Graphics.DrawImage((Image) bmp, 0, 0);
}
private void button1_Click(object sender, EventArgs e) {
PrintPreviewDialog ppd = new PrintPreviewDialog();
PrintDocument Pd = new PrintDocument();
Pd.PrintPage += this.printDocument1_PrintPage;
ppd.Document = Pd;
ppd.ShowDialog();
}
You need to draw within the bounds of the page.
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
var g = e.Graphics;
var srcRect = new Rectangle(0, 0, groupBox1.Width, groupBox1.Height);
var desRect = new Rectangle(e.PageBounds.X, e.PageBounds.Y, e.PageBounds.Width, srcRect.Height);
//Or to draw within the margin
//var desRect = new Rectangle(e.MarginBounds.X, e.MarginBounds.Y, e.MarginBounds.Width, srcRect.Height);
using (var bmp = new Bitmap(srcRect.Width, srcRect.Height))
{
groupBox1.DrawToBitmap(bmp, srcRect);
g.DrawImage(bmp, desRect, srcRect, GraphicsUnit.Pixel);
}
}
Also, create the PrintDocument object and register its PrintPage event in the Form's constructor or Load event to avoid repeating that again and again in the button1_Click event. And don't forget to dispose the disposable objects.
I am using BufferedGraphics to redraw my form. I wish to redraw the form's controls all by myself, so I can draw lines wherever I want while I can still use the Form Designer to align my controls.
I've tried SuspendLayout but it make no use here.
Code:
using System;
using System.Drawing;
using System.Windows.Forms;
public partial class Form1 : Form
{
BufferedGraphicsContext context;
BufferedGraphics grafx;
PictureBox pic1, pic2;
public Form1()
{
SetStyle((ControlStyles)(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint), true);
InitializeComponent();
//Add picturebox
pic1 = new PictureBox();
pic1.BackColor = Color.Black;
pic1.SetBounds(50, 50, 100, 50);
Controls.Add(pic1);
pic2 = new PictureBox();
pic2.BackColor = Color.Gray;
pic2.SetBounds(75, 50, 50, 100);
Controls.Add(pic2);
}
private void Form1_Load(object sender, EventArgs e)
{
context = BufferedGraphicsManager.Current;
context.MaximumBuffer = new Size(Width, Height);
grafx = context.Allocate(CreateGraphics(), new Rectangle(0, 0, Width, Height));
}
public override void Refresh() { }
protected override void OnPaintBackground(PaintEventArgs e) { }
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = grafx.Graphics;
g.Clear(BackColor);
Bitmap bmp = new Bitmap(Width, Height);
foreach (Control c in Controls)
c.DrawToBitmap(bmp, new Rectangle(c.Left, c.Top, c.Width, c.Height));
g.DrawImage(bmp, 0, 0);
g.DrawLine(new Pen(Color.Red, 4), 0, 0, ClientSize.Width, ClientSize.Height);
grafx.Render(Graphics.FromHwnd(Handle));
}
private void Form1_Resize(object sender, EventArgs e)
{
context.MaximumBuffer = new Size(this.Width, this.Height);
if (grafx != null)
{
grafx.Dispose();
grafx = null;
}
grafx = context.Allocate(this.CreateGraphics(), new Rectangle(0, 0, Width, Height));
base.Refresh();
}
}
I need the line to be drawn at the top of the controls (picturebox), but the code turns out to be drawn below the controls.
I'm trying to print a panel that has pictureboxes, labels and textboxes. I want to print it on A4 paper so the panel has 595x842.
I have tried:
private void print_Click(object sender, EventArgs e)
{
PrintPanel(panel1);
}
private void PrintPanel(Panel pnl)
{
PrintDialog myPrintDialog = new PrintDialog();
System.Drawing.Bitmap memoryImage = new System.Drawing.Bitmap(pnl.Width, pnl.Height);
pnl.DrawToBitmap(memoryImage, pnl.ClientRectangle);
if (myPrintDialog.ShowDialog() == DialogResult.OK)
{
System.Drawing.Printing.PrinterSettings values;
values = myPrintDialog.PrinterSettings;
myPrintDialog.Document = printDocument1;
printDocument1.PrintController = new StandardPrintController();
printDocument1.Print();
}
printDocument1.Dispose();
}
When I try to print it I go to the One Note and all I get is a white page. What seems to be the problem?
I modified the code a bit. Now I get some output but it's still not good...
This is the code I used:
private void print_Click(object sender, EventArgs e)
{
System.Drawing.Printing.PrintDocument doc = new System.Drawing.Printing.PrintDocument();
doc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(doc_PrintPage);
doc.Print();
}
private void doc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Panel grd = new Panel();
Bitmap bmp = new Bitmap(grd.Width, grd.Height, grd.CreateGraphics());
grd.DrawToBitmap(bmp, new Rectangle(0, 0, grd.Width, grd.Height));
RectangleF bounds = e.PageSettings.PrintableArea;
float factor = ((float)bmp.Height / (float)bmp.Width);
e.Graphics.DrawImage(bmp, bounds.Left, bounds.Top, bounds.Width, factor * bounds.Width);
}
And I get this:
http://i.stack.imgur.com/HB1EH.png
Can someone help?
You are printing the wrong panel. Your code is creating a new empty panel and trying to print that (which it successfully does). You need to reference the existing panel you want to print:
void doc_PrintPage(object sender, PrintPageEventArgs e) {
using (Bitmap bmp = new Bitmap(panel1.ClientSize.Width, panel1.ClientSize.Height)) {
panel1.DrawToBitmap(bmp, panel1.ClientRectangle);
RectangleF bounds = e.PageSettings.PrintableArea;
float factor = ((float)bmp.Height / (float)bmp.Width);
e.Graphics.DrawImage(bmp, bounds.Left, bounds.Top,
bounds.Width, factor * bounds.Width);
}
}