Print image with specific width & height - c#

I want to upload an image to print with some string values when I try to print I can't specify the width & height of the image. I want to display it like a header of the page and give it constant width & height.
Below is the code of uploading the image:
private void button2_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog dialog = new OpenFileDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
imageLocation = dialog.FileName;
pictureBox1.ImageLocation = imageLocation;
//pictureBox1.Image.Width(100);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
The printing code:
e.Graphics.DrawImage(System.Drawing.Image.FromFile(imageLocation), e.PageBounds);
e.Graphics.DrawString(labelProjectNum.Text,
new Font("Times New Roman", 12, FontStyle.Underline),
Brushes.Black,
new Point(700, 200));
e.Graphics.DrawString(projectNumber.Text,
new Font("Times New Roman", 12, FontStyle.Regular),
Brushes.Black,
new Point(650, 200));

Related

Print barcode from C# on IIS

I have the code working from Visual Studio. But when I put it onto the IIS it doesn't print the barcode, it just gives me the numbers on the label.
I've tried looking up examples but couldn't find anything. This is my first time using barcodes and printing from IIS. Below is the code I have that currently works on Visual Studio but just prints text when I print from my website hosted on IIS.
protected void grdList_RowCommand(object sender,
GridViewCommandEventArgs e)
{
oDL.UpdateCookieExpiry();
try
{
if (e.CommandName == "View")
{
int rowIndex = Convert.ToInt32(e.CommandArgument);
int id =
Convert.ToInt32(grdList.DataKeys[rowIndex].Values[0]);
string url = "/Stock.aspx?ob_RecordID=" + id;
Response.Redirect(url, false);
Context.ApplicationInstance.CompleteRequest();
}
else if (e.CommandName == "Print")
{
int rowIndex = Convert.ToInt32(e.CommandArgument);
int id =
Convert.ToInt32(grdList.DataKeys[rowIndex].Values[0]);
try
{
outerboxbc o = new outerboxbc();
o = oDL.Get_Outerboxbc(id);
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
this.ob_Barcode.Text = o.barcode.bc_Barcode.ToString();
// Set the printer name.
// pd.PrinterSettings.PrinterName = "\\NS5\hpoffice
pd.PrinterSettings.PrinterName = "ZDesigner GK420d (Copy
1)";
pd.Print();
}
catch (Exception ex)
{
Response.Write("Error: " + ex.ToString());
}
}
}
catch (Exception ex)
{
Console.Write("Error: " + ex.Message);
}
}
void pd_PrintPage(object print, PrintPageEventArgs ev)
{
long de_RecordID = Convert.ToInt32(this.de_RecordID.Value);
cDelivery d = new cDelivery();
d = oDL.get_cDelivery(de_RecordID);
System.Drawing.Image img =
System.Drawing.Image.FromFile(#"C:AlphaLogo.png");
int printHeight = 100;
int printWidth = 250;
int leftMargin = 0;
int rightMargin = 0;
ev.Graphics.DrawImage(img, new Rectangle(leftMargin, rightMargin,
printWidth, printHeight));
Font printFont = new Font("3 of 9 Barcode", 24);
Font printFont1 = new Font("Times New Roman", 14, FontStyle.Bold);
SolidBrush br = new SolidBrush(Color.Black);
ev.Graphics.DrawString("*" + this.ob_Barcode.Text + "*", printFont,
br, 10, 130);
ev.Graphics.DrawString(d.de_ProjectNumber, printFont1, br, 110,
170);
}
So an example of the barcode.text is "AAC12342313". This prints as a barcode when run using Visual Studio but looks like this on the label "AAC12342313" when ran from the website.

How to Print with Margins

I am making a Wordpad program. I am making this feature in which you click this button, and it prints to your default printer. I have done some research, and I found some functional code that prints to my printer:
private void buttonPrint_Click(object sender, EventArgs e)
{
string print = "" + textBody.Text;
PrintDocument p = new PrintDocument();
p.PrintPage += delegate(object sender1, PrintPageEventArgs e1)
{
e1.Graphics.DrawString(print, 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();
}
catch (Exception ex)
{
throw new Exception("Exception Occured While Printing", ex);
}
}
This works currently, but I was wondering if I could make it WITH margins, which it does not have now. All it does is this:
<Top of Page>
<Message>
There are no margins on the top, sides (left,right), and bottom. How can I modify my code to have margins?
On the PrintDocument you can set the Margins on the DefaultPageSettings object:
Margins margins = new Margins(100,100,100,100);
p.DefaultPageSettings.Margins = margins;

Printing a textbot text

I'm trying to make a program that prints a textbox full text. When I'm generating it to a pdf to see the page before I print it some of the text is missing.
This is my code:
private void Print_Click(object sender, EventArgs e)
{
PrintDialog PD = new PrintDialog();
PrintDocument PDoc = new PrintDocument();
PD.Document = PDoc;
PDoc.PrintPage += new PrintPageEventHandler(PDoc_PrintPage);
DialogResult result = PD.ShowDialog();
if (result == DialogResult.OK)
{
PDoc.Print();
}
}
void PDoc_PrintPage(object sender, PrintPageEventArgs e)
{
Graphics graph = e.Graphics;
graph.DrawString(textBox1.Text,
new Font("Arial", 12), new SolidBrush(Color.Black), 10, 10);
}
This is the text (Just for a test):
hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello
hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello
hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello
this is the textbox screenshot:
This is what I get:
as you can see some of the text is missing. I'm not really familiar with printing so I have no Idea how to solve this problem.
It would appear your problem is that you want the text to wrap when printing. Have a look at this previous thread: Automatically Word-Wrapping Text To A Print Page?

Print .rtf or .txt file from PRINT button?

I am making a simple work note pad test program. I want to be able to click on a menu item in the menutool bar (Print) and have the document print to my printer. The following code is what I use but I am not sure if this is all that I need for a simple print. I am new to C# and thus not complete familiar with the printDocument class.
private void printToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
StreamReader streamToPrint = new StreamReader
("C:\\My Documents\\MyFile.txt");
try
{
Font printFont = new Font("Arial", 10);
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(myFileName);
pd.Print();
}
finally
{
streamToPrint.Close();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
Problem : You are not handling the PrintPagEvent properly.
Solution : to print the document you need to handle the PrintPageEvent properly by writing the PrintPageEvent Handler.
String content="";
Font printFont = new Font("Arial", 10);
private void printToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
content= File.ReadAllText("C:\\My Documents\\MyFile.txt");
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
pd.Print();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
ev.Graphics.DrawString(content,printFont , Brushes.Black,
ev.MarginBounds.Left, 0, new StringFormat());
}

How do I get an image to save after rotation at runtime?

I am working with WPF and I have an application that the user loads an image file into a RichTextBox and they can rotate the image and print it. I am not sure as to why the image after it has been rotated will not print as it is displayed on the screen. Instead it prints the original. I am new to this so any help would be greatly appreciated!
The following is the code for my application. Code when the retrieve file Button is clicked:
private void retrieve_button_Click(object sender, RoutedEventArgs e)
{
//Retrieve the file or image you are looking for
OpenFileDialog of = new OpenFileDialog();
of.Filter = "Formats|*.jpg;*.png;*.bmp;*.gif;*.ico;*.txt|JPG Image|*.jpg|BMP image|*.bmp|PNG image|*.png|GIF Image|*.gif|Icon|*.ico|Text File|*.txt";
var dialogResult = of.ShowDialog();
if (dialogResult == System.Windows.Forms.DialogResult.OK)
{
try
{
System.Windows.Controls.RichTextBox myRTB = new System.Windows.Controls.RichTextBox();
{
Run myRun = new Run();
System.Windows.Controls.Image MyImage = new System.Windows.Controls.Image();
MyImage.Source = new BitmapImage(new Uri(of.FileName, UriKind.RelativeOrAbsolute));
InlineUIContainer MyUI = new InlineUIContainer();
MyUI.Child = MyImage;
rotateright_button.IsEnabled = true;
print_button.IsEnabled = true;
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(myRun);
paragraph.Inlines.Add(MyUI);
FlowDocument document = new FlowDocument(paragraph);
richTextBox.Document = document;
}
}
catch (ArgumentException)
{
System.Windows.Forms.MessageBox.Show("Invalid File");
}
}
}
When the rotate right button is clicked the following code is executed:
RotateTransform cwRotateTransform;
private void rotateright_button_Click(object sender, RoutedEventArgs e)
{
richTextBox.LayoutTransform = cwRotateTransform;
if (cwRotateTransform == null)
{
cwRotateTransform = new RotateTransform();
}
if (cwRotateTransform.Angle == 360)
{
cwRotateTransform.Angle = 0;
}
else
{
cwRotateTransform.Angle += 90;
}
}
After the Image has been loaded and rotated the user can use the following code to print:
private void InvokePrint(object sender, RoutedEventArgs e)
{
System.Windows.Controls.PrintDialog printDialog = new System.Windows.Controls.PrintDialog();
if ((bool)printDialog.ShowDialog().GetValueOrDefault())
{
FlowDocument flowDocument = new FlowDocument();
flowDocument = richTextBox.Document;
flowDocument.ColumnWidth = printDialog.PrintableAreaWidth;
flowDocument.PagePadding = new Thickness(65);
IDocumentPaginatorSource iDocPag = flowDocument;
printDialog.PrintDocument(iDocPag.DocumentPaginator, "Print Document");
}
}
Try this (substitute yourImageControl in the first line, specify which RotateFlipType you want and be sure to reference the System.Drawing dll):
System.Drawing.Bitmap bitmap = BitmapSourceToBitmap((BitmapSource)YourImageControl.Source);
bitmap.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipNone);
public static System.Drawing.Bitmap BitmapSourceToBitmap(BitmapSource bitmapsource)
{
System.Drawing.Bitmap bitmap;
using (MemoryStream outStream = new MemoryStream())
{
BitmapEncoder enc = new BmpBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(bitmapsource));
enc.Save(outStream);
bitmap = new System.Drawing.Bitmap(outStream);
}
return bitmap;
}
Another option for conversion...
P.S. You would get a better answer in less time if you posted some code and told us more about what you have tried.

Categories