PrintDocument coming up blank - c#

I have this method that should generate what I need to print:
private void myPrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.PageUnit = GraphicsUnit.Inch;
e.Graphics.DrawImage(makeBarcode(), 500, 1000);
e.Graphics.DrawString("Do Not Seperate", makeFont(), Brushes.Black, 100, 2);
e.Graphics.DrawString("Choking Hazard", makeFont(), Brushes.Black, 200, 2);
}
How ever when I print it, it comes up blanks and when I look at it in PrintPreviewDialog it comes up blanket. What I am missing here?
This is my constructor by the way:
public Form1()
{
InitializeComponent();
this.myPrintDocument.PrintPage += new
System.Drawing.Printing.PrintPageEventHandler
(this.myPrintDocument_PrintPage);
}
For Mark
private Image makeBarcode()
{
InventoryBLL ibll = new InventoryBLL();
Barcode b = new Barcode();
b.IncludeLabel = true;
b.Height = 35;
b.Width = 200;
b.BackColor = Color.White;
b.ForeColor = Color.Black;
b.Alignment = BarcodeLib.AlignmentPositions.CENTER;
return b.Encode(TYPE.CODE128, ibll.getFNSKU(txtASIN.Text));
}
private Font makeFont()
{
Font myFont = new Font("arial", 10);
return myFont;
}

e.Graphics.PageUnit = GraphicsUnit.Inch;
...
e.Graphics.DrawString("Choking Hazard", ..., 200, 2);
That string is going to print at 200 inches. Somewhat safe to assume that your paper isn't that big, 8 inch is about typical. You cannot see what's off the paper. Consider changing the PageUnit or using much smaller floating point print positions.

Related

how can i add page number to each copy from printDocument in c#

Please can you help me on how I can add the page number when printing several copies of a document?
I am using Windows Forms, and printdocument, in my printpage I insert a counter but it only respects it if I generate an impression at the same time, and if I put 5 copies it sends me the same page number in the 5 pages and I want the page number to appear consecutively in the copies
This is my code:
private int pagenum;
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
using (var g = e.Graphics)
{
using (var fnt = new Font("Courier New", 10, FontStyle.Bold))
{
string content = Contenido.Text;
Barcode codigo = new Barcode();
codigo.Alignment = AlignmentPositions.CENTER;
codigo.LabelFont = new Font(FontFamily.GenericMonospace, 8, FontStyle.Bold);
codigo.IncludeLabel = true;
Image img = codigo.Encode(TYPE.CODE128, content, Color.Black, Color.White, 250, 70);
pictureBox.Image = img;
var f = new Font("Courier New", 8, FontStyle.Bold);
g.DrawString("SEMayr", fnt, Brushes.Black, 147, 10);
g.DrawString("1er. Turno", fnt, Brushes.Black, 150, 40);
g.DrawImage(pictureBox.Image, 98, 168, 201, 55);
e.Graphics.DrawString("Pg " + pagenum, f, Brushes.Black, 215, 230);
pagenum++;
var caption = textBox1.Text;
g.DrawString(caption, fnt, Brushes.Black, 118, 70);
}
}
}
private void button2_Click(object sender, EventArgs e){ try
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
// Especifica que impresora se utilizara!!
pd.PrinterSettings.PrinterName = "Datamax";
PageSettings pa = new PageSettings();
pa.Margins = new Margins(0, 0, 0, 0);
pd.DefaultPageSettings.Margins = pa.Margins;
PaperSize ps = new PaperSize("Custom", 403, 244);
pd.DefaultPageSettings.PaperSize = ps;
pd.PrinterSettings.Copies = (short)Convert.ToInt32(label4.Text);
pd.Print();
}
catch (Exception exp)
{
MessageBox.Show("Printing " + exp.Message);
}
}
Well, copies are supposed to be identical. So when you set PrinterSettings.Copies = n you instruct the printer to output n copies of what you have in the PrintPage event handler, including the page number, it won't increment for each copy.
To keep the counter, don't set the PrinterSettings.Copies property and use the e.HasMorePages to print the contents n times.
Therefore (read the comments):
// +
using System.Drawing.Printing;
//
int copies;
int pageNum;
private void button2_Click(object sender, EventArgs e)
{
pageNum = 0;
copies = int.Parse(label4.Text);
// If this does not change, then no need
// to have it in the PrintPage event.
// Otherwise keep it as is.
Barcode codigo = new Barcode
{
Alignment = AlignmentPositions.CENTER,
LabelFont = new Font(FontFamily.GenericMonospace, 8, FontStyle.Bold),
IncludeLabel = true
};
Image img = codigo.Encode(TYPE.CODE128, Contenido.Text, Color.Black, Color.White, 250, 70);
pictureBox.Image = img;
using (PrintDocument pd = new PrintDocument())
{
pd.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
pd.PrinterSettings.PrinterName = "Datamax";
pd.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
pd.DefaultPageSettings.PaperSize = new PaperSize("Custom", 403, 244);
pd.Print();
}
// If the Barcode implements IDisposable interface...
// codigo.Dispose();
}
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
copies--;
pageNum++;
// Don't dispose of the e.Graphics object.
// You didn't create it then its not your call.
var g = e.Graphics;
using (var fnt = new Font("Courier New", 10, FontStyle.Bold))
using (var f = new Font("Courier New", 8, FontStyle.Bold))
{
g.DrawString("SEMayr", fnt, Brushes.Black, 147, 10);
g.DrawString("1er. Turno", fnt, Brushes.Black, 150, 40);
g.DrawImage(pictureBox.Image, 98, 168, 201, 55);
g.DrawString($"Pg {pageNum}", f, Brushes.Black, 215, 230);
g.DrawString(textBox1.Text, fnt, Brushes.Black, 118, 70);
e.HasMorePages = copies > 0;
}
}
Further, consider drawing the strings in rectangles with the StringFormat class. See the Graphics.DrawString method overloads.

mono C# print persian text ubuntu

I am using mono-develop under Ubuntu 14.04.
In my project I need to print Persian documents.
We are using native .net package system.drawing.
I ran the following code under Windows (mono which was installed on Windows) and
I had absolutely NO PROBLEM, but here under Ubuntu letters are not going to be joined together to create words. The output is something like this:
I know somehow it is related to my OS. Please help me to solve this.
Here is my code:
namespace printPage{
public class PrintClass
{
PrintDialog printDialog;
PrintDocument printDocument;
PageSetupDialog psd;
public PrintClass()
{
printDialog = new PrintDialog ();
printDocument = new PrintDocument();
printDialog.Document = printDocument;
printDocument.PrintPage +=
new System.Drawing.Printing.PrintPageEventHandler(CreateReceipt);
DialogResult result = printDialog.ShowDialog();
if (result == DialogResult.OK)
printDocument.Print();
}
public void CreateReceipt(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//string to be printed out
string txt = "با سلام و خسته نباشید";
Graphics graphic = e.Graphics;
Font font = new Font("Arial unicode MS", 20, FontStyle.Regular); //must use a mono spaced font as the spaces need to line up
float fontHeight = font.GetHeight();
int startX = 100;
int startY = 100;
StringFormat sf = new StringFormat();
sf.FormatFlags = StringFormatFlags.DirectionRightToLeft;
sf.FormatFlags = StringFormatFlags.DisplayFormatControl;
int faLCID = new System.Globalization.CultureInfo("fa-IR").LCID;
sf.SetDigitSubstitution(faLCID, StringDigitSubstitute.National);
graphic.DrawString( txt, font, new SolidBrush(Color.Black), startX, startY, sf);
e.HasMorePages = false;
}
}
class MainClass
{
static void Main()
{
PrintClass p = new PrintClass ();
}
}
}

PowerPoint Add-in Write on Slide in Slideshow

While writing a PowerPoint add-in, I need to draw something on the screen on top of the slideshow.
I am able to draw lines and images, but they disappear almost immediately.
Example code:
private void Application_SlideShowBegin(PowerPoint.SlideShowWindow Wn)
{
using (var g = Graphics.FromHwnd((IntPtr)Wn.HWND))
{
g.DrawLine(new Pen(Color.Red, 10), new System.Drawing.Point(100, 100), new System.Drawing.Point(200, 300));
Image img = Properties.Resources.img;
g.DrawImageUnscaled(img, new Rectangle(250, 250, img.Width, img.Height));
}
}
Any idea how I can keep the drawn lines / images on the screen?
I should have seen it earlier - this is about refreshes. The code needs to be run a bit later, so if the paint is delayed, the drawing stays on the window.
The following example now works as expected:
private Timer _Ticker = new Timer();
private PowerPoint.SlideShowWindow _SlideshowWindow = null;
private void Application_SlideShowBegin(PowerPoint.SlideShowWindow Wn)
{
_SlideshowWindow = Wn;
_Ticker.Interval = 30;
_Ticker.AutoReset = true;
_Ticker.Elapsed += Ticker_Elapsed;
_Ticker.Enabled = true;
}
private void Ticker_Elapsed(object sender, ElapsedEventArgs e)
{
if (_Ticker.Enabled)
{
using (var g = Graphics.FromHwnd((IntPtr)_SlideshowWindow.HWND))
{
g.DrawLine(new Pen(Color.Red, 10), new System.Drawing.Point(100, 100), new System.Drawing.Point(200, 300));
Image img = Properties.Resources.img;
g.DrawImageUnscaled(img, new Rectangle(250, 250, img.Width, img.Height));
}
_Ticker.Enabled = false;
_Ticker.Elapsed -= Ticker_Elapsed;
}
}

Print the entire area of the control with C#

This is not a duplicat question- the diffenece beetween my question an the others one is my Controler contail a scroller, so they are more informations can't be printed.
I have a C# application that contains a main form name MainForms. This MainForms has a control mainDisplay. I want to print the entire information what we found on the mainDisplay to the printer.
The problem is the information on the the control is too big, and I have to scroll to see all information.
Someone have any function that allow me to print this control MainDisplay with entire information in it?
This the printscreen of the area of my MainDisplay at the right you see the scrollbar:
I use this Function (Source : Printing a control)
private static void PrintControl(Control control)
{
var bitmap = new Bitmap(control.Width, control.Height);
control.DrawToBitmap(bitmap, new Rectangle(0, 0, control.Width, control.Height));
var pd = new PrintDocument();
pd.PrintPage += (s, e) => e.Graphics.DrawImage(bitmap, 100, 100);
pd.Print();
}
But my problem still can't print all the informations contain in my control, it's just print a small erea, still need print more informations which are not printed.
I find the solution. This is the steps i do :
1 - We have a mainForm, and this main form contain a control mainDisplay with a specific dimension and area, let's say this dimensions is smaller and we get scroll.
2- What i do is i make this mainDisplay Empty.
3- i create an other Control myControlToDisplay. I draw and i put all fields i want without scroll, so this one myControlToDisplay will have a big dimension.
4- on the star-up of my application, i tell to the mainDisplay to load myControlToDisplay. This time all the content of myControlToDisplay will be display on mainDisplay with a scroll. Because mainDisplay have a small area.
5- I write this functions :
Bitmap MemoryImage;
PrintDocument printDoc = new PrintDocument();
PrintDialog printDialog = new PrintDialog();
PrintPreviewDialog printDialogPreview = new PrintPreviewDialog();
Control panel = null;
public void Print(Control pnl)
{
DateTime saveNow = DateTime.Now;
string datePatt = #"yyyy-M-d_hh-mm-ss tt";
panel = pnl;
GetPrintArea(pnl);
printDialog.AllowSomePages = true;
printDoc.PrintPage += new PrintPageEventHandler(Print_Details);
printDialog.Document = printDoc;
printDialog.Document.DocumentName = "Document Name";
//printDialog.ShowDialog();
if (printDialog.ShowDialog() == DialogResult.OK)
{
printDoc.Print();
}
}
public void PrintPreview(Control pnl)
{
DateTime saveNow = DateTime.Now;
string datePatt = #"yyyy-M-d_hh-mm-ss tt";
panel = pnl;
GetPrintArea(pnl);
printDoc.PrintPage += new PrintPageEventHandler(Print_Details);
printDialogPreview.Document = printDoc;
printDialogPreview.Document.DocumentName = "Document Name";
//printDialog.ShowDialog();
if (printDialogPreview.ShowDialog() == DialogResult.OK)
{
printDoc.Print();
}
}
private void Print_Details(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
RectangleF marginBounds = e.MarginBounds;
DateTime saveNow = DateTime.Now;
string datePatt = #"M/d/yyyy hh:mm:ss tt";
//String dtString = saveNow.ToString(datePatt);
// create header and footer
string header = "Put all information you need to display on the Header";
string footer = "Print date : " + saveNow.ToString(datePatt);
Font font = new Font("times new roman", 10, System.Drawing.FontStyle.Regular);
Brush brush = new SolidBrush(Color.Black);
// measure them
SizeF headerSize = e.Graphics.MeasureString(header, font);
SizeF footerSize = e.Graphics.MeasureString(footer, font);
// draw header
RectangleF headerBounds = new RectangleF(marginBounds.Left-80, marginBounds.Top-80, marginBounds.Width, headerSize.Height);
e.Graphics.DrawString(header, font, brush, headerBounds);
// draw footer
RectangleF footerBounds = new RectangleF(marginBounds.Left-80, marginBounds.Bottom - footerSize.Height+80, marginBounds.Width, footerSize.Height);
e.Graphics.DrawString(footer, font, brush, footerBounds);
// dispose objects
font.Dispose();
brush.Dispose();
}
public void GetPrintArea(Control 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));
}
protected override void OnPaint(PaintEventArgs e)
{
if (MemoryImage != null)
{
e.Graphics.DrawImage(MemoryImage, 0, 0);
base.OnPaint(e);
}
}
void PrintDoc_PrintPage(object sender, PrintPageEventArgs e)
{
Rectangle pageArea = e.PageBounds;
Rectangle m = e.MarginBounds;
if ((double)MemoryImage.Width / (double)MemoryImage.Height > (double)m.Width / (double)m.Height) // image is wider
{
m.Height = (int)((double)MemoryImage.Height / (double)MemoryImage.Width * (double)m.Width);
}
else
{
m.Width = (int)((double)MemoryImage.Width / (double)MemoryImage.Height * (double)m.Height);
}
e.Graphics.DrawImage(MemoryImage, m);
}
6 -And finally we suppose that we have two buttons, one to Print and the other one to print preview.
You just have to call this function :
private void PrintButton_Click(object sender, EventArgs e)
{
try
{
Print(mainDisplay.getCurentPanel());
}
catch (Exception exp)
{
MessageBox.Show("Error: \n" + exp.Message);
}
}
private void PrintPreviewButton_Click(object sender, EventArgs e)
{
try
{
PrintPreview(mainDisplay.getCurentPanel());
}
catch (Exception exp)
{
MessageBox.Show("Error: \n" + exp.Message);
}
}
Hope it will help someone :)
good luck

Crosshair cursor with additional lines in C#

how to make a crosshair cursor with help lines like this on screenshots:
I know how to make crosshire cursor:
this.Cursor = System.Windows.Forms.Cursors.Cross;
can be also something like that:
like in CAD software.
This is the code I use. x and y are the dimensions. In my case I can have some text on the cursor and this is name. If you want dots or dashes then you need to do that with the pen.
private Cursor crossCursor(Pen pen, Brush brush, string name, int x, int y) {
var pic = new Bitmap(x, y);
Graphics gr = Graphics.FromImage(pic);
var pathX = new GraphicsPath();
var pathY = new GraphicsPath();
pathX.AddLine(0, y / 2, x, y / 2);
pathY.AddLine(x / 2, 0, x / 2, y);
gr.DrawPath(pen, pathX);
gr.DrawPath(pen, pathY);
gr.DrawString(name, Font, brush, x / 2 + 5, y - 35);
IntPtr ptr = pic.GetHicon();
var c = new Cursor(ptr);
return c;
}
Just create two label box as lab_X_Axis and lab_Y_Axis.
In chart mousemove function code as shown below..
private void chart1_MouseMove(object sender, MouseEventArgs e)
{
lab_X_Axis.Location = new Point((e.X), 21);
lab_Y_Axis.Location = new Point(76, e.Y);
}
private void Form1_Load(object sender, EventArgs e)
{
lab_X_Axis.AutoSize = false;
lab_Y_Axis.AutoSize = false;
lab_X_Axis.Text="";
lab_Y_Axis.Text="";
lab_X_Axes.Size = new Size(1, 300);
lab_Y_Axes.Size = new Size(300, 1);
}

Categories

Resources