Working on a Receipt Printer app for a local business. It's technically a re-write, and I'm approaching this as a complete refactor.
The original code looks like this:
private void btn_print_Click(object sender, EventArgs e)
{
PrintDialog pd = new PrintDialog();
PrintDocument doc = new PrintDocument();
pd.Document = doc;
doc.PrintPage += new PrintPageEventHandler(pd_printpage);
DialogResult res = pd.ShowDialog();
if (res == DialogResult.OK)
{
doc.DefaultPageSettings.Landscape = false;
doc.PrinterSettings.DefaultPageSettings.PaperSize = new PaperSize("Env10", 4, 8);
doc.Print();
}
}
private void pd_printpage(object sender, PrintPageEventArgs e)
{
Graphics gfx = e.Graphics;
SolidBrush blk = new SolidBrush(Color.Black);
Font courier = new Font("Courier New", 12);
Font lucidia = new Font("Lucida Sans", 12);
Font lucidaplus = new Font("Lucida Sans", 18);
StringFormat fmt = new StringFormat(StringFormatFlags.DirectionVertical);
/***** Create Object for Reciept *****/
Recpt rcpt = new Recpt();
rcpt.Contrib = new ContInfo();
rcpt.Pri = new Address();
rcpt.Alt = new Address();
//--- Remainder of function omitted.
Here is a better factored version that I'm currently working on.
static class Printality
{
private static SolidBrush Blk = new SolidBrush(Color.Black);
private static Font CourierBody = new Font("Courier New", 12);
private static Font LucidaBody = new Font("Lucida Sans", 12);
private static Font LucidaHeader = new Font("Lucida Sans", 18);
private static StringFormat fmt;
public static void PrintReciept(Receipt _rec)
{
PrintDialog pd = new PrintDialog();
PrintDocument doc = new PrintDocument();
doc.PrintPage += new PrintPageEventHandler(pd_printPage);
}
private static void pd_printPage(object sender, PrintPageEventArgs e)
{
Graphics gfx = e.Graphics;
fmt = new StringFormat(StringFormatFlags.DirectionVertical);
}
}
the big question is this: I'm passing a Receipt object to the function printReceipt() how should I also pass it to pd_printPage()?
If I understood your question clearly, you want to pass parameter to event.
doc.PrintPage += (sender, e) => pd_printPage(sender, e, _rec);
private static void pd_printPage(object sender, PrintPageEventArgs e, Receipt rec)
{
Graphics gfx = e.Graphics;
fmt = new StringFormat(StringFormatFlags.DirectionVertical);
}
Related
Here is my small software.. We add images clicking "ADD YOUR QR CODE" and it will be displayed on pictureBoxLoad picturebox, After that i will crop the image. Then it shows on picturebox2.
But my problem is ,it is only shows on picturebox2, But picturebox2.image is null...how to fix that error. I want to add cropping image to picturebox2 without null..
here after clicking the crop button
here is the code i tried
{
int xDown = 0;
int yDown = 0;
int xUp = 0;
int yUp = 0;
Rectangle rectCropArea = new Rectangle();
System.IO.MemoryStream ms = new System.IO.MemoryStream();
Task timeout;
string fn = "";
public Main()
{
InitializeComponent();
}
private void Main_Load(object sender, EventArgs e)
{
}
private void selectIm_Click(object sender, EventArgs e)
{
OpenFileDialog opf = new OpenFileDialog();
if (opf.ShowDialog() == DialogResult.OK)
{
PictureBoxLoad.Image = Image.FromFile(opf.FileName);
fn = opf.FileName;
}
PictureBoxLoad.Cursor = Cursors.Cross;
}
private void PictureBoxLoad_MouseUp(object sender, MouseEventArgs e)
{
}
private void PictureBoxLoad_MouseDown(object sender, MouseEventArgs e)
{
}
private void crop_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog opf = new OpenFileDialog();
opf.Filter= "Image Files(*.jpg; *.jpeg; *.gif;*.png; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp;*.png";
if (opf.ShowDialog() == DialogResult.OK)
{
PictureBoxLoad.Image = Image.FromFile(opf.FileName);
fn = opf.FileName;
}
PictureBoxLoad.Cursor = Cursors.Cross;
}
private void button2_Click(object sender, EventArgs e)
{
string root = #"C:\FuePass";
// If directory does not exist, create it.
if (!System.IO.Directory.Exists(root))
{
System.IO.Directory.CreateDirectory(root);
}
Bitmap bmp = new Bitmap(previewimg.Width, previewimg.Height);
previewimg.DrawToBitmap(bmp, new Rectangle(0, 0, previewimg.Width, previewimg.Height));
pictureBox2.DrawToBitmap(bmp, new Rectangle(pictureBox2.Location.X - previewimg.Location.X, pictureBox2.Location.Y - previewimg.Location.Y, pictureBox2.Width, pictureBox2.Height));
bmp.Save(#"C:\Fuepass\output.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
MessageBox.Show("Saved,Locaiton : C:\\Fuepass\\");
// SaveFileDialog dialog = new SaveFileDialog();
// dialog.Filter = "JPG(*.JPG)|*.jpg";
// if (dialog.ShowDialog() == DialogResult.OK)
// {
// previewimg.Image.Save(dialog.FileName);
// pictureBox2.Image.Save(dialog.FileName);
// }
// }
}
private void PictureBoxLoad_Click(object sender, EventArgs e)
{
}
private void PictureBoxLoad_MouseDown_1(object sender, MouseEventArgs e)
{
PictureBoxLoad.Invalidate();
xDown = e.X;
yDown = e.Y;
crop.Enabled = true;
}
private void PictureBoxLoad_MouseUp_1(object sender, MouseEventArgs e)
{
//pictureBox1.Image.Clone();
xUp = e.X;
yUp = e.Y;
Rectangle rec = new Rectangle(xDown, yDown, Math.Abs(xUp - xDown), Math.Abs(yUp - yDown));
using (Pen pen = new Pen(Color.YellowGreen, 3))
{
PictureBoxLoad.CreateGraphics().DrawRectangle(pen, rec);
}
rectCropArea = rec;
crop.Enabled = true;
}
private void crop_Click_1(object sender, EventArgs e)
{
try
{
pictureBox2.Refresh();
//Prepare a new Bitmap on which the cropped image will be drawn
Bitmap sourceBitmap = new Bitmap(PictureBoxLoad.Image, PictureBoxLoad.Width, PictureBoxLoad.Height);
Graphics g = pictureBox2.CreateGraphics();
//Draw the image on the Graphics object with the new dimesions
g.DrawImage(sourceBitmap, new Rectangle(0, 0, pictureBox2.Width, pictureBox2.Height), rectCropArea, GraphicsUnit.Pixel);
sourceBitmap.Dispose();
crop.Enabled = false;
var path = Environment.CurrentDirectory.ToString();
ms = new System.IO.MemoryStream();
//pictureBox2.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] ar = new byte[ms.Length];
var timeout = ms.WriteAsync(ar, 0, ar.Length);
}
catch (Exception ex)
{
}
if (pictureBox2.Image == null)
{
MessageBox.Show("no image");
}
}
}
}
plz tell me the error
You should be able to set the Image to a Bitmap. But do not dispose the Bitmap before you do the save. Hope this helps.
private void crop_Click_1(object sender, EventArgs e)
{
try
{
pictureBox2.Refresh();
//Prepare a new Bitmap on which the cropped image will be drawn
Bitmap sourceBitmap = new Bitmap(PictureBoxLoad.Image, PictureBoxLoad.Width, PictureBoxLoad.Height);
Graphics g = pictureBox2.CreateGraphics();
//Draw the image on the Graphics object with the new dimesions
g.DrawImage(sourceBitmap, new Rectangle(0, 0, pictureBox2.Width, pictureBox2.Height), rectCropArea, GraphicsUnit.Pixel);
crop.Enabled = false;
var path = Environment.CurrentDirectory.ToString();
ms = new System.IO.MemoryStream();
pictureBox2.Image = sourceBitmap;
pictureBox2.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] ar = new byte[ms.Length];
var timeout = ms.WriteAsync(ar, 0, ar.Length);
sourceBitmap.Dispose();
}
catch (Exception ex)
{
}
if (pictureBox2.Image == null)
{
MessageBox.Show("no image");
}
}
I want to set the title to the document I use in PrintPreviewDialog in Windows Forms. I tried something, but for sure this isn't the way to set the TITLE to this type of Document. Can you help me do this?
private void pd_print(object sender, PrintPageEventArgs e)
{
Graphics gr = e.Graphics;
gr.DrawString("Sales", new Font(FontFamily.GenericMonospace, 12, FontStyle.Bold), new SolidBrush(Color.Black), new Point(40, 40));
}
private void tiparireToolStripMenuItem_Click(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(this.pd_print);
PrintPreviewDialog dlg = new PrintPreviewDialog();
dlg.Document = pd;
dlg.ShowDialog();
}
The Preview Dialog is just a form where they hid the Text property.
PrintPreviewDialog dlg = new PrintPreviewDialog();
((Form)dlg).Text = "My Title";
dlg.Document = pd;
dlg.ShowDialog();
I've got a form with a combo to pick up a customer and a button.
When a customer is selected and you move the mouse over the button a ToolTip is shown with information about this customer.
I have customized the tooltip using ToolTip_Draw.
All of these works fine.
The problem is that when I change de selected customer and then move the mouse over the button several ToolTip texts are shown. One for each customer I've previously selected.
I've tried to somehow empty de ToolTip but nothing seems to work.
private void bttCitas_MouseHover(object sender, EventArgs e)
{
string mSQL = #" SELECT one, two, three
FROM customers
WHERE id = " + comboCliente.SelectedValue + ";";
DataTable tablaTemp = retrieveData(mSQL);
string customerText = ConvertDataTableToString(tablaTemp);
System.Windows.Forms.ToolTip Emergente = new System.Windows.Forms.ToolTip();
Emergente.OwnerDraw = true;
Emergente.Draw += new DrawToolTipEventHandler(ToolTip_Draw);
Emergente.AutoPopDelay = 150000;
Emergente.InitialDelay = 500;
Emergente.ReshowDelay = 500;
Emergente.SetToolTip(this.bttCitas, customerText);
}
void ToolTip_Draw(object sender, DrawToolTipEventArgs e)
{
using (e.Graphics)
{
Font f = new Font("Courier New", 9.0f);
e.DrawBackground();
e.DrawBorder();
SolidBrush myBrush = new SolidBrush(GLOBALToolTipFontColor);
e.Graphics.DrawString(e.ToolTipText, f, myBrush, new PointF(2, 2));
}
}
I've finally figured out what happens.
I need to declare Emergente out of the MouseOver event and then dispose it in the MouseLeave:
private ToolTip Emergente;
private void bttCitas_MouseHover(object sender, EventArgs e)
{
string mSQL = #" SELECT one, two, three
FROM customers
WHERE id = " + comboCliente.SelectedValue + ";";
DataTable tablaTemp = retrieveData(mSQL);
string customerText = ConvertDataTableToString(tablaTemp);
Emergente = new System.Windows.Forms.ToolTip();
Emergente.OwnerDraw = true;
Emergente.Draw += new DrawToolTipEventHandler(ToolTip_Draw);
Emergente.AutoPopDelay = 150000;
Emergente.InitialDelay = 500;
Emergente.ReshowDelay = 500;
Emergente.SetToolTip(this.bttCitas, customerText);
}
void ToolTip_Draw(object sender, DrawToolTipEventArgs e)
{
using (e.Graphics)
{
Font f = new Font("Courier New", 9.0f);
e.DrawBackground();
e.DrawBorder();
SolidBrush myBrush = new SolidBrush(GLOBALToolTipFontColor);
e.Graphics.DrawString(e.ToolTipText, f, myBrush, new PointF(2, 2));
}
}
private void bttCitas_MouseLeave(object sender, EventArgs e)
{
Emergente.Dispose();
}
I am using a PrintDialog and PrintDocument in c# windows application, WinForms, to print a document.
Printing working fine but I want to display the document before printing, so that I can check if PointF for DrawString is like I want it.How I can do that? Is there any tool that make it easy to define a pointF on A4 document?
private void buttonPrintShows_Click(object sender, EventArgs e)
{
PrintDialog pd = new PrintDialog();
pd.Document = printDocumentStatistic;
if (pd.ShowDialog() == DialogResult.OK)
{
printDocumentStatistic.Print();
}
}
private void printDocumentStatistic_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawString("Shows:",new System.Drawing.Font("Arial", 20f), new SolidBrush(Color.Red), new PointF(35, 50));
e.Graphics.DrawString("Act:", new System.Drawing.Font("Arial", 20f), new SolidBrush(Color.Red), new PointF(35, 75));
e.Graphics.DrawString(show_name, new System.Drawing.Font("Arial", 20f), new SolidBrush(Color.Red), new PointF(100, 50));
e.Graphics.DrawString(akt_name, new System.Drawing.Font("Arial", 20f), new SolidBrush(Color.Red), new PointF(100, 75));
}
There is a simple way. Add a printPreviewDialog and give your document path to it.
printPreviewDialog1.Document = printDocument1;
You will find more info here.
I have the following method that loads in a blank template image, draws the relevant information on it and saves it to another file. I want to change this slightly to achieve the following:
load in the template image
draw the relevant information on it
print it
I don't want to save it, just print it out. Here's my existing method:
public static void GenerateCard(string recipient, string nominee, string reason, out string filename)
{
// Get a bitmap.
Bitmap bmp1 = new Bitmap("template.jpg");
Graphics graphicImage;
// Wrapped in a using statement to automatically take care of IDisposable and cleanup
using (graphicImage = Graphics.FromImage(bmp1))
{
ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);
// Create an Encoder object based on the GUID
// for the Quality parameter category.
Encoder myEncoder = Encoder.Quality;
graphicImage.DrawString(recipient, new Font("Arial", 10, FontStyle.Regular), SystemBrushes.WindowText, new Point(480, 33));
graphicImage.DrawString(WordWrap(reason, 35), new Font("Arial", 10, FontStyle.Regular), SystemBrushes.WindowText, new Point(566, 53));
graphicImage.DrawString(nominee, new Font("Arial", 10, FontStyle.Regular), SystemBrushes.WindowText, new Point(492, 405));
graphicImage.DrawString(DateTime.Now.ToShortDateString(), new Font("Arial", 10, FontStyle.Regular), SystemBrushes.WindowText, new Point(490, 425));
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 100L);
myEncoderParameters.Param[0] = myEncoderParameter;
filename = recipient + " - " + DateTime.Now.ToShortDateString().Replace("/", "-") + ".jpg";
bmp1.Save(filename, jgpEncoder, myEncoderParameters);
}
}
Hope you can help,
Brett
Just print it to the printer without saving
This is the most simple example I could come up with.
Bitmap b = new Bitmap(100, 100);
using (var g = Graphics.FromImage(b))
{
g.DrawString("Hello", this.Font, Brushes.Black, new PointF(0, 0));
}
PrintDocument pd = new PrintDocument();
pd.PrintPage += (object printSender, PrintPageEventArgs printE) =>
{
printE.Graphics.DrawImageUnscaled(b, new Point(0, 0));
};
PrintDialog dialog = new PrintDialog();
dialog.ShowDialog();
pd.PrinterSettings = dialog.PrinterSettings;
pd.Print();
When you use the PrintDocument class, you can print without needing to save the image.
var pd = new PrintDocument();
pd.PrintPage += pd_PrintPage;
pd.Print()
And in the pd_PrintPage eventhandler:
void pd_PrintPage(object sender, PrintPageEventArgs e)
{
Graphics gr = e.Graphics;
//now you can draw on the gr object you received using some of the code you posted.
}
NOTE: Don't dispose the Graphics object you received in the eventhandler. This is done by the PrintDocument object itself...
Use the PrintPage event;
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) {
e.Graphics.DrawImage(image, 0, 0);
}