C# PrintDocument prints blank page. Why? - c#

I have the following picture box I populate with a barcode then try to print.
Here's the code:
private void button1_Click(object sender, EventArgs e)
{
printDocument1.OriginAtMargins = true;
printDocument1.DocumentName = "TEST IMAGE PRINTING";
printDialog1.Document = printDocument1;
printDialog1.ShowDialog();
printDocument1.Print();
}
private void printDocument1_PrintPage_1(object sender, PrintPageEventArgs e)
{
System.Drawing.Graphics formGraphics = System.Drawing.Graphics.FromImage(picpdf417.Image);
}

You need to actually draw the image:
private void printDocument1_PrintPage_1(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawImage(picpdf417.Image, Point.Empty);
}

Related

Trying to print the picturebox content but it shows there's no content

I am trying to print the content of picturebox in which the barcode is generated. Barcode is there and when I click 'print' the print preview shows there's no content.
Print button is named 'druk', picturebox 'box4'.
Code below:
private void druk_Click(object sender, EventArgs e)
{
printDocument1.OriginAtMargins = true;
printPreviewDialog1.ShowDialog();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(box4.Image, 0, 0);
}
private void box4_Click(object sender, EventArgs e)
{

Printing contents from textbox

I have a problem with the PrintPreviewDialog. I get this result:
All the contents goes into one page. I already tried \f and e.HasMorePages but with no result:
PS. I'm new to c# and stackeroverflow wont allow me to paste the whole code.
private void btnPrintReport_Click(object sender, EventArgs e)
{
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.ShowDialog();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawString(txtInvoices.Text, txtInvoices.Font, Brushes.Black, 10, 25);
//e.HasMorePages = true;
}
}

Mouse moving too fast when drawing in a graphics Object

i'm doing a C# project with my friend. We have to take a signature and save it to a JPG file. We don't have much idea of how doing this, but at least we are trying. We have 2 problems:
1.
When drawing the Graphics, if the mouse moves too fast, not all the points are caught by the mousemove Event and the result image are separated points. How can i improve this?
Here is my code:
private void ingresoFirma_Load(object sender, EventArgs e)
{
myBrush = new SolidBrush(Color.Black);
myGraphics = panel1.CreateGraphics();
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
isDrawing = true;
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (isDrawing)
{
myGraphics.FillEllipse(myBrush, e.X, e.Y, 10, 10);
}
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
isDrawing = false;
}
The second problem is that we have no idea of how to save the Graphics to a .jpg image. We used something like this:
private void saveSign_Click(object sender, EventArgs e)
{
Bitmap signature = new Bitmap(100,100,myGraphics);
signature.Save("c:\\myBitmap.bmp");
}
But it saves a blank image.
Here's something to get you started...
public partial class ingresoFirma : Form
{
private List<Point> stroke = null;
private List<List<Point>> Strokes = new List<List<Point>>();
public ingresoFirma()
{
InitializeComponent();
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
stroke = new List<Point>();
stroke.Add(new Point(e.X, e.Y));
}
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
stroke.Add(new Point(e.X, e.Y));
if (stroke.Count == 2)
{
Strokes.Add(stroke);
}
panel1.Refresh();
}
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
stroke = null;
}
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
foreach(List<Point> curStroke in Strokes)
{
e.Graphics.DrawLines(Pens.Black, curStroke.ToArray());
}
}
private void btnClear_Click(object sender, EventArgs e)
{
Strokes.Clear();
panel1.Refresh();
}
private void btnSave_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "JPG Files(*.JPG)|*.JPG";
if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Bitmap bmp = new Bitmap(panel1.Width, panel1.Height);
panel1.DrawToBitmap(bmp, panel1.ClientRectangle);
bmp.Save(sfd.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
With the DrawToBitmap() method of Control, you can save the image:
Bitmap theBitmap = new Bitmap(panel1.Width, panel1.Height)) ;
panel1.DrawToBitmap(theBitmap, new Rectangle(0, 0, bmp.Width, bmp.Height));
theBitmap.Save("c:\\myBitmap.bmp");

C# Printing A Form

I'm new to c#, so I hope this doesn't sound/look stupid.
I'm having an issue while printing. I can't seem to get my form to print on the full size of a regular piece of paper. It's printing in landscape, the height fits, but my width goes off the page. Here is the code I'm working with.
Any help would be greatly appreciated, thanks!
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawImage(memoryImage, 0, 0);
}
private void printPreviewDialog1_Load(object sender, EventArgs e)
{
PrintPreviewDialog _PrintPreview = new PrintPreviewDialog();
_PrintPreview.Document = printDocument1;
((Form)_PrintPreview).WindowState = FormWindowState.Normal;
_PrintPreview.ShowDialog();
}
private void printToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveAsBitmap(this, #"img\1.bmp");
CaptureScreen();
printDocument1.PrinterSettings.DefaultPageSettings.Landscape = true;
printDocument1.Print();
}
try setting the papersize in the printOption : http://msdn.microsoft.com/en-us/library/aa691030(v=vs.71).aspx
Hope this can help you!

How can I copy the contents of a Panel to a richTextBox?

I have a Panel which the user of the application. The panel allows the user to enter their signature digitally. I would like to take the drawing from the panel and copy it to the very end of the richTextBox.
My current code for the panel is as follows:
public partial class Signature : Form
{
SolidBrush color;
float width;
List<List<Point>> _lines;
Boolean _mouseDown;
public Signature()
{
InitializeComponent();
_lines = new List<List<Point>>();
color = new SolidBrush(Color.Black);
_mouseDown = false;
}
private void clear_Click(object sender, EventArgs e)
{
_lines.Clear();
sign.Invalidate();
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
_mouseDown = true;
_lines.Add(new List<Point>());
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (_mouseDown)
{
_lines.Last().Add(e.Location);
sign.Invalidate();
}
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
foreach (var lineSet in _lines)
{
if (lineSet.Count > 1)
{
e.Graphics.DrawLines(new Pen(Color.Black, 4.0F), lineSet.ToArray());
}
}
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
_mouseDown = false;
}
private void use_Click(object sender, EventArgs e)
{
MessageBox.Show("Signature successfully imported!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
Basic_Word_Processor.Instance.richTextBoxPrintCtrl1.SelectedText = "";
this.Close();
}
}
}
How can I take the drawing from the Panel and insert it to the end of the richTextBox?
You could first draw the signature to the Bitmap and then copy that bitmap through Clipboard into RichTextBox. You could try this but I must say that it is not tested:
Bitmap bmp = new Bitmap(panel1.Width, panel1.Height);
Rectangle rect = new Rectangle(0, 0, panel1.Width, panel1.Height);
panel1.DrawToBitmap(bmp, rect);
Clipboard.SetImage(bmp);
richTextBox1.Paste();
Or alternatively, you could draw Lines into the Bitmap

Categories