Print image after it's been generated [c#] - c#

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

Related

Printer Printing Cutting Off Strings

I'm using this code to print a collection of strings from a Rich Text Box:
private void printBtn_Click(object sender, EventArgs e)
{
PrintDocument p = new PrintDocument();
p.OriginAtMargins = true;
Margins pMargins = new Margins(100, 100, 100, 100);
p.DefaultPageSettings.Margins = pMargins;
p.PrintPage += delegate (object sender1, PrintPageEventArgs e1)
{
e1.Graphics.DrawString(summaryBox.Text, new Font("Times New Roman", 12), new SolidBrush(Color.Black), new RectangleF(0, 0, p.DefaultPageSettings.PrintableArea.Width, p.DefaultPageSettings.PrintableArea.Height));
};
try
{
p.Print();
this.Close();
}
catch (Exception ex)
{
throw new Exception("Error During Printing", ex);
}
}
However, long strings get cut off and don't automatically. Strings cut off to the right, and after the first page is filled, a second page is not printed and leftover content is simply ignored. Are the problems with the printer setup or with the rich text box or both? How can I make sure long strings wrap and second or third pages are printed if the content is too long?
You need to use PrintPageEventArgs.MarginBounds to get printable area within a print page.
Now your e1 is the parameter of type PrintPageEventArgs. and you will get MarginBounds like e1.MarginBounds.
So your code will be.
p.PrintPage += delegate (object sender1, PrintPageEventArgs e1)
{
e1.Graphics.DrawString(summaryBox.Text, new Font("Times New Roman", 12), new SolidBrush(Color.Black), new RectangleF(0, 0, e1.MarginBounds.Width, e1.MarginBounds.Height));
};

Set DocumentName of a Document from PrintPreviewDialog

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

C# Auto Generated Methods. How to add additional parameters

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

Display printDocument before printing / How to define PoinF

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.

Image upload path not uploading in folder

I am having a problem when I try to upload the image on the folder and click upload,the image is not saved in 'UploadedFiles' folder.The 'UploadedFiles' folder is still empty and does not contain any picture.
This is my project directory of the saved image that it should go if it might helps:
//C:\Users\Drake\Documents\Visual Studio 2013\Projects\DigitalWaterMarkTest\DigitalWaterMarkTest\UploadFiles
protected void btnSave_Click(object sender, EventArgs e)
{
// Here We will upload image with watermark Text
string fileName = Guid.NewGuid() + Path.GetExtension(FU1.PostedFile.FileName);
Image upImage = Image.FromStream(FU1.PostedFile.InputStream);
upImage = ResizeBitmap((Bitmap)upImage, 400, 400);//new width
using (Graphics g = Graphics.FromImage(upImage))
{
// For Transparent Watermark Text
int opacity = 128; // range from 0 to 255
//SolidBrush brush = new SolidBrush(Color.Red);
SolidBrush brush = new SolidBrush(Color.FromArgb(opacity, Color.Black));
SolidBrush brush2 = new SolidBrush(Color.FromArgb(opacity, Color.Red));
Font font = new Font("Arial", 15);
g.DrawString(txtWatermarkText.Text.Trim(), font, brush, new PointF(80,160));
g.DrawString(txtWatermarkText.Text.Trim(), font, brush2, new PointF(200, 250));
upImage.Save(Path.Combine(Server.MapPath("~/UploadFiles"), fileName));
Image1.ImageUrl = "~/UploadFiles" + "//" + fileName;
}
}

Categories

Resources