Printing form gives me blank page - c#

Using the code example from MSDN on how to print a windows form, I have the altered mine to bring up the printer options first and then print, but I keep receiving a blank page. Using CopyFromScreen, I am giving the coordinates of the forms source X & Y, but for the destination I have tried 0 as well as this.Location.X & Y. Is there another way to capture the image?
private void printButton_Click(object sender, EventArgs e)
{
CaptureScreen();
printDialog1.AllowSomePages = true;
printDialog1.ShowHelp = true;
printDialog1.Document = printDoc1;
DialogResult result = printDialog1.ShowDialog();
if (result == DialogResult.OK)
{
printDoc1.Print();
}
}
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);
}
void printDoc1_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawImage(memoryImage, 0, 0);
}

Related

How can i remove the button of my form when i go to the print preview

Hello guys goodevening to you... how can i remove the button 6 to my print preview ? i dont have any idea even when i false the visible of the button6
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);
}
private void button6_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();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(bitmap, 0, 0);
}
}
}
//CODE
private void button6_Click(object sender, EventArgs e)
{
button6.Visible = false;//add here
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();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(bitmap, 0, 0);
}
private void printPreviewDialog1_FormClosed(object sender, FormClosedEventArgs e)
{
button6.Visible = true;//this is to enable button after closing print screen
}
In order for the button to actually become invisible, you have to force the events to actually run.
In your button6_Click event handler you begin with the lines:
button6.Visible = false;
Application.DoEvents();
The DoEvents() method forces the events in the event queue to run. This is because you run on the UI thread. I would not recomend you move the copying operation to another thread, but another way would be to start an UI timer (found under Components in the tools menu) and have that timer timout at a few ms. You can then have the copying operation there. But the easiest solution is to simply add the Application.DoEvents() so that is my recommendation.

C# paint program won't create any lines on the screen since I tried to draw using a bitmap

Here is my source code. I can't seem to get the bitmap to show the lines drawn on the panel when I move the mouse with the button pressed. Frustrated and looking for someone to help me finish the code so I can complete the app for my 9-yo daughter. Thank you in advance...
namespace TV_PAINT
{
public partial class ALANA_PAINT : Form
{
Graphics g;
Pen p = new Pen(Color.Black, 7);
Point sp = new Point(0, 0);
Point ep = new Point(0, 0);
int m = 0;
Bitmap BP;
public ALANA_PAINT()
{
InitializeComponent();
tb1.Text = p.Width.ToString();
BP = new Bitmap(pnl1.ClientSize.Width, pnl1.ClientSize.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
}
private void closeButton_Click(object sender, EventArgs e)
{
pnl1.Dispose();
p.Dispose();
this.Close();
}
private void clearButton_Click(object sender, EventArgs e)
{
//pnl1.Invalidate();
p.Color = System.Drawing.Color.Black;
p.Width = 7;
tb1.Text = p.Width.ToString();
//pnl1.Invalidate();
}
private void pnl1_MouseDown(object sender, MouseEventArgs e)
{
sp = e.Location;
if (e.Button == MouseButtons.Left)
m = 1;
if (e.Button == MouseButtons.Right)
m = 1;
}
private void pnl1_MouseMove(object sender, MouseEventArgs e)
{
if (m == 1)
{
ep = e.Location;
//g = pnl1.CreateGraphics();
Graphics g = Graphics.FromImage(BP);
g.DrawLine(p, sp, ep);
}
sp = ep;
}
private void pnl1_MouseUp(object sender, MouseEventArgs e)
{
m = 0;
}
BP is just a variable in the form. As I can see, it is not displayed anywhere in your form. Why do you need a bitmap for it.
You can do something like this, just get the graphics of your form, and draw using that graphic. https://msdn.microsoft.com/en-us/library/ztxk24yx(v=vs.110).aspx
Noted: you need to do it on PaintEvent of the form, otherwise your drawing will be removed after the next repaint, so you need some variables to store all of your lines, then draw all of them in the paint event.
System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics;
formGraphics = this.CreateGraphics();
formGraphics.FillRectangle(myBrush, new Rectangle(0, 0, 200, 300));
myBrush.Dispose();
formGraphics.Dispose();
Updated:
If you want to save your change to a bitmap. You can use Form.DrawToBitmap to save your drawing in the form to a bitmap, then call bitmap.Save() to a file in directory.

C# Windows Form printing - prints wrong area of screen [duplicate]

This question already has answers here:
How to configure an app to run correctly on a machine with a high DPI setting (e.g. 150%)?
(6 answers)
Closed 6 years ago.
I'm trying to print my Windows Form. My solution is:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(bmp, 0, 0);
}
Bitmap bmp;
private void button1_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
bmp = new Bitmap(this.Size.Width, this.Size.Height, g);
Graphics mg = Graphics.FromImage(bmp);
mg.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, this.Size);
printDialog1.ShowDialog();
printDocument1.Print();
printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
}
}
But it gives me this result:
(frame is made by me to show the borders of the printed paper)
How can i solve this problem?
try this
private void button1_Click(object sender, EventArgs e)
{
//Add a Panel control.
Panel panel = new Panel();
this.Controls.Add(panel);
//Create a Bitmap of size same as that of the Form.
Graphics grp = panel.CreateGraphics();
Size formSize = this.ClientSize;
bitmap = new Bitmap(formSize.Width, formSize.Height, grp);
grp = Graphics.FromImage(bitmap);
//Copy screen area that that the Panel covers.
Point panelLocation = PointToScreen(panel.Location);
grp.CopyFromScreen(panelLocation.X, panelLocation.Y, 0, 0, formSize);
//Show the Print Preview Dialog.
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.PrintPreviewControl.Zoom = 1;
printPreviewDialog1.ShowDialog();
}
Ref : http://www.aspsnippets.com/Articles/Print-contents-of-Form-in-Windows-Forms-WinForms-Application-using-C-and-VBNet.aspx

How to print using multiple screenshots captured with copyfromscreen

I'm pretty new to C# but I finally have my first program up and running and I need to make it print. Its a window form with information and calculations on different tab controls, somewhat like Excel. The page that is currently being looked at prints fine with the copyfromscreen method, but I cannot get additional pages to print correctly. I have about 20 tabs I would like to be able to print at one time. I found a way to print the contents of controls into a textfile, but I would much prefer to be able to print what the form looks like. Thanks.
Bitmap memoryImage;
Bitmap memoryImage2;
private void CaptureScreen()
{
Graphics myGraphics = this.CreateGraphics();
Size s = tabControlMain.Size;
s.Width = s.Width + 20;
memoryImage = new Bitmap(s.Width, s.Height, myGraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
memoryGraphics.CopyFromScreen(this.Location.X+15, this.Location.Y+80, 0, 0, s);
tabControlMain.SelectedIndex = 1;
memoryImage2 = new Bitmap(s.Width, s.Height, myGraphics);
Graphics memoryGraphics2 = Graphics.FromImage(memoryImage2);
memoryGraphics2.CopyFromScreen(this.Location.X + 15, this.Location.Y + 80, 0, 0, s);
}
private void printDocumentReal_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(memoryImage, 0, 0);
e.Graphics.DrawImage(memoryImage2, 0, 550);
}
private void printToolStripButton_Click(object sender, EventArgs e)
{
CaptureScreen();
printDocumentReal.Print();
}
First, you should use PrintDocument and PrintPreviewDialog objects for print related tasks and an event handler for printing.
Second, you need to preform some optimization for your code, here's the solution:
private void printToolStripButton_Click(object sender, EventArgs e)
{
PrintDocument document = new PrintDocument();
document.PrintPage += new PrintPageEventHandler(document_PrintPage);
PrintPreviewDialog preview = new PrintPreviewDialog() { Document = document };
// you will be able to preview all pages before print it ;)
try
{
preview.ShowDialog();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\nYou need to install a printer to preform print-related tasks!", "Print Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Boolean firstPage = true;
private void document_PrintPage(object sender, PrintPageEventArgs e)
{
if (firstPage)
{
tabControlMain.SelectTab(0);
firstPage = false;
}
Graphics g = e.Graphics;
TabPage tab = tabControlMain.SelectedTab;
using (Bitmap img = new Bitmap(tab.Width, tab.Height))
{
tab.DrawToBitmap(img, tab.ClientRectangle);
g.DrawImage(img, new Point(e.MarginBounds.X, e.MarginBounds.Y)); // MarginBounds means the margins of the page
}
if (tabControlMain.SelectedIndex + 1 < tabControlMain.TabCount)
{
tabControlMain.SelectedIndex++;
e.HasMorePages = true;//If you set e.HasMorePages to true, the Document object will call this event handler again to print the next page.
}
else
{
e.HasMorePages = false;
firstPage = true;
}
}
I hope it's working fine with you And here's an additional one if you need to save all tabs as set of images on your hard disk:
public void RenderAllTabs()
{
foreach (TabPage tab in tabControlMain.TabPages)
{
tabControlMain.SelectTab(tab);
using (Bitmap img = new Bitmap(tab.Width, tab.Height))
{
tab.DrawToBitmap(img, tab.ClientRectangle);
img.Save(string.Format(#"C:\Tabs\{0}.png", tab.Text));
}
}
}
Try using DrawToBitmap method of TabPage instead:
private void CaptureScreen()
{
memoryImage = new Bitmap(tabControlMain.SelectedTab.Width, tabControlMain.SelectedTab.Height);
tabControlMain.SelectedTab.DrawToBitmap(memoryImage, tabControlMain.SelectedTab.ClientRectangle);
tabControlMain.SelectedIndex = 1;
memoryImage2 = new Bitmap(tabControlMain.SelectedTab.Width, tabControlMain.SelectedTab.Height);
tabControlMain.SelectedTab.DrawToBitmap(memoryImage2, tabControlMain.SelectedTab.ClientRectangle);
}
To get all the images of your TabPages, you can make a loop like this:
List<Bitmap> images = new List<Bitmap>();
private void CaptureScreen(){
foreach(TabPage page in tabControlMain.TabPages){
Bitmap bm = new Bitmap(page.Width, page.Height);
tabControlMain.SelectedTab = page;
page.DrawToBitmap(bm, page.ClientRectangle);
images.Add(bm);
}
}
//Then you can access the images of your TabPages in the list images
//the index of TabPage is corresponding to its image index in the list images

Print with BufferedGraphics using PrintDialog

I have a BufferedGraphics called backbufferGraphics and I want to print out the content in that buffered by using a PrintDialog, this is my code but it's not work:
private print()
{
printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);
printDialog1.Document = printDocument1;
printDialog1.AllowSelection = true;
printDialog1.AllowSomePages = true;
printDialog1.ShowDialog();
}
void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
DrawToPrint(e.Graphics);
}
private void DrawToPrint(Graphics _Gd)
{
backbufferGraphics.Render(_Gd);
}
Thanks!
you have first to set your buffergraphics in a image and after you will print image:
Bitmap bmp = new Bitmap();
int X = e.X - bmp.Width/2;
int Y = e.Y - bmp.Height/2;
// créer et initialiser le BufferedGraphics
BufferedGraphics bg =
BufferedGraphicsManager.Current.Allocate(this.CreateGraphics(),
ClientRectangle);
Graphics g = bg.Graphics;
g.Clear(SystemColors.Control);
g.DrawImage(bmp, new Point(X, Y));
bg.Render();
g.Dispose();
bg.Dispose();
//save bmp as file

Categories

Resources