Getting Printing issue in C# windows forms - c#

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);
}

Related

Button does not want to stop being visible

So, I'm trying to make simple screenshot app in Window Forms, but i would like my button to dissapear whenever I take a shot. Unfortunately, it does not want to be invisible, even after setting Visible to false:
{
InitializeComponent();
this.BackColor = Color.Red;
this.TransparencyKey = BackColor;
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
}
Image tmp = Image.FromFile("C:/Users/Bartek/source/repos/Hadr/Hadr/Image/image.png");
private void button1_Click_1(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(tmp.Width, tmp.Height);
Graphics g = Graphics.FromImage(bmp);
button1.Visible = false;
g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size);
pictureBox1.Image = bmp;
bmp.Save("C:/Folder/image1.png",System.Drawing.Imaging.ImageFormat.Png);
}
I have set dock on picturebox to fill, and placed button in the way showed on screen:
I'm kindly asking for help
Hide the button first and then use BeginInvoke() to defer making the screenshot until the system updates controls, like this:
private void button1_Click_1(object sender, EventArgs e)
{
button1.Visible = false;
BeginInvoke(MakeScreenShot);
}
void MakeScreenshot()
{
Bitmap bmp = new Bitmap(tmp.Width, tmp.Height);
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size);
pictureBox1.Image = bmp;
bmp.Save("C:/Folder/image1.png", System.Drawing.Imaging.ImageFormat.Png);
button1.Visible = true;
}

How to transfer a zoomed image from a pictureBox to a second pictureBox? .NET Framework 4.7

I have 2 pictureBoxes on my Form. In the right pictureBox I can zoom and pan an image. The illustration on the left shows only the image. Unfortunately I can‘t copy the zoomed image from pictureBox1(right) into pictureBox2. How should i do that? I’m thankful for any hint!
public Form1()
{
InitializeComponent();
string imgFile = #"C:\Users\Pictures\Clock.jpg";
img = Image.FromFile(imgFile);
Graphics g = this.CreateGraphics();
zoom = ((float)pictureBox1.Width / (float)img.Width) * (img.HorizontalResolution / g.DpiX);
pictureBox1.Paint += new PaintEventHandler(ImageBox_Paint);
}
private void ImageBox_Paint(object sender, PaintEventArgs e)
{
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
e.Graphics.ScaleTransform(zoom, zoom);
e.Graphics.DrawImage(img, imgX, imgY);
}
private void Button1_Click(object sender, EventArgs e)
{
Bitmap newbmp= new Bitmap(pictureBox2.ClientSize.Width,pictureBox2.ClientSize.Height);
using (Bitmap oldbmp = new Bitmap(img, pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height))
{
using (Graphics g = Graphics.FromImage(newbmp))
{
g.DrawImage(oldbmp, new Rectangle(0, 0,
pictureBox2.ClientSize.Width,pictureBox2.ClientSize.Height));
}
}
pictureBox2.Image = newbmp;
}

How to print a group box using print preview?

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.

Print Preview for printing windows form not working in C#

I am trying to screen capture a windows form, provide a print preview and then print it. The form is printing on paper but the prin preview displays the message "Document does not contain any pages"/
Here are the relevant snippets from my code. Please let me know where I am going wrong.
Bitmap bmp; // Variable for Bitmap of actual page to print
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) //Click to Print
{
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);
//printPreviewDialog1.ShowDialog();
//printDocument1.Print();
printPreviewDialog1.Document = printDocument1;
// printPreviewDialog1.PrintPreviewControl.Zoom = 1;
printPreviewDialog1.ShowDialog();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(bmp, 0, 0);
}

Trying to print a panel in C#

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);
}
}

Categories