I'm currently trying to learn how to use the print functions in C#, and I have a problem when I'm trying to print out labels in my windows forms application.
What I want to do, is when I click button 1, I want to put the text from the labels (or draw an image of them) into a document, that can be printed.
I'm still new to programming, so this print function is very confusing for me.
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawString(label1.Text, label2.Text, label3.Text, label4.Text, label5.Text, label6.Text, label7.Text, label8.Text, label9.Text);
}
private void button1_Click(object sender, EventArgs e)
{
this.printDocument1.PrintPage += new
System.Drawing.Printing.PrintPageEventHandler
(this.printDocument1_PrintPage);
}
private void PrintPage(object o, PrintPageEventArgs e)
{
System.Drawing.Image img = System.Drawing.Image.FromFile("C://gul.PNG");
Point loc = new Point(10, 10);
e.Graphics.DrawImage(img, loc);
}
What do I need to do different, to be able to do this?
Use the Form.DrawToBitmap() method.
For example a form like this:
When the print button is pressed:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
var pd = new PrintDocument();
pd.PrintPage+=(s,ev)=>
{
var bmp = new Bitmap(Width, Height);
this.DrawToBitmap(bmp, new Rectangle(Point.Empty, this.Size));
ev.Graphics.DrawImageUnscaled(bmp, ev.MarginBounds.Location);
ev.HasMorePages=false;
};
var dlg = new PrintPreviewDialog();
dlg.Document=pd;
dlg.ShowDialog();
}
}
With the result:
Related
I have the following subroutine to create a label, located in form 1:
private void MakeLabelForGameStyle()
{
Label SelectedMode = new Label();
SelectedMode.Text = form2obj.GameStyle;
SelectedMode.Location = new Point(507, 92);
SelectedMode.AutoSize = true;
SelectedMode.Font = new Font("Calibri", 12);
Controls.Add(SelectedMode);
}
In form 2, the relevant code to determine 'GameStyle' is as follows:
private string gameStyle;
private void button3_Click(object sender, EventArgs e)
{
gameStyle = "Best Of 3";
Close();
}
private void button4_Click(object sender, EventArgs e)
{
gameStyle = "Best of 5";
Close();
}
public string GameStyle
{
get { return gameStyle; }
}
But when I run the program, the label is blank after clicking either of the buttons.
I have a problem about System.Windows.Forms.PictureBox. I want to show a image on monitor and capture it on the camera. So I use Winform which include a picturebox. The picture box is:
PictureBox pb = new PictureBox();
pb.WaitOnLoad = true;
When I set a bitmap to PictureBox and capture the image from camera,
// Show bmp1
this.image.Image = bmp1;
this.image.Invalidate();
this.image.Refresh();
// Delay 1s
UseTimerToDelay1s();
// Show bmp2
this.image.Image = bmp2;
this.image.Invalidate();
this.image.Refresh();
// Capture
CaptureImageFromCamera();
It only capture the bmp1.
If I add a small delay like this,
this.image.Image = bmp2;
this.image.Invalidate();
this.image.Refresh();
UseTimerToDelay100ms();
CaptureImageFromCamera();
It capture bmp2. The Image set method seem to be a async method. Does any method to confirm the image is set? Thanks.
I'd use the first Paint event after assigning the new Image.
You can give it a try using a very large image url from this site.
The example uses google logo image url. Copy the following code and make sure you assign event handlers to the events:
bool newImageInstalled = true;
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.WaitOnLoad = true;
}
private void PictureBox1_Paint(object sender, PaintEventArgs e)
{
if (!newImageInstalled)
{
newImageInstalled = true;
BeginInvoke(new Action(() =>
{
//Capturing the new image
using (var bm = new Bitmap(pictureBox1.ClientSize.Width,
pictureBox1.ClientSize.Height))
{
pictureBox1.DrawToBitmap(bm, pictureBox1.ClientRectangle);
var tempFile = System.IO.Path.GetTempFileName() + ".png";
bm.Save(tempFile, System.Drawing.Imaging.ImageFormat.Png);
System.Diagnostics.Process.Start(tempFile);
}
}));
}
}
private void button1_Click(object sender, EventArgs e)
{
newImageInstalled = false;
pictureBox1.ImageLocation =
"https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png";
}
private void button2_Click(object sender, EventArgs e)
{
newImageInstalled = false;
pictureBox1.Image = null;
}
I'm making a program that saves the Mouse X and Mouse Y coordinates from a NumericUpDown-Box into Settings.settings so the program launches with the last used values.
Both Input Boxes call the method "saveXY" when "ValueChanged" as seen here
My Problem is: the X coordinates get saved without problems, the Y coordinates don't get saved at all - but the code is the same:
private void Form1_Load(object sender, EventArgs e)
{
movetoX.Value = Settings.Default.mouseX;
movetoY.Value = Settings.Default.mouseY;
}
-
private void saveXY(object sender, EventArgs e)
{
Settings.Default.mouseX = (int)movetoX.Value;
Settings.Default.mouseY = (int)movetoY.Value;
Settings.Default.Save();
}
Theese are my Settings.settings.
The .exe is availeble here.
This article may be useful to you. http://msdn.microsoft.com/en-us/library/aa730869%28v=vs.80%29.aspx
Update 1:
Had to perform a Properties.Settings.Default.Upgrade() and then your saved settings get loaded.
Sample
public Form1()
{
InitializeComponent();
//Load saved settings
this.Location = Properties.Settings.Default.Form1Location;
this.Size = Properties.Settings.Default.Form1Size;
//Allow changes to be implemented
this.StartPosition = FormStartPosition.Manual;
//capture changes
this.LocationChanged += new EventHandler(Form1_LocationChanged);
this.SizeChanged += new EventHandler(Form1_SizeChanged);
//capture the closing form event to save your new settings
this.FormClosed += new FormClosedEventHandler(Form1_FormClosed);
}
void Form1_LocationChanged(object sender, EventArgs e)
{
//Capture the new values
Properties.Settings.Default.Form1Location = this.Location;
}
void Form1_SizeChanged(object sender, EventArgs e)
{
//Capture the new values
Properties.Settings.Default.Form1Size = this.Size;
}
void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
//you can capture the new values here as well
//save the new values
Properties.Settings.Default.Save();
}
thanks to hamix, its working now
i deleted saveXY and wrote this:
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
Settings.Default.mouseX = (int)movetoX.Value;
Settings.Default.mouseY = (int)movetoY.Value;
Settings.Default.Save();
}
it now saves X and Y
I've made a TextBox that retains what you type, and when you click the button associated it gives you a messagebox. When people want to click no, I want the button to change location so people cannot click it so they are forced to click yes. Can you help me? Here is the code:
{
MsgBox = new CustomMsgBox();
MsgBox.label1.Text = Text;
MsgBox.button1.Text = btnOK;
MsgBox.button2.Text = btnCancel;
MsgBox.Text = Caption;
result = DialogResult.No;
MsgBox.ShowDialog();
return result;
}
private void button2_Click(object sender, EventArgs e)
{
button2.Location = new Point(25, 25);
}
private void button2_MouseHover(object sender, EventArgs e)
{
button2.Location = new Point(+50, +50);
}
private void button2_MouseLeave(object sender, EventArgs e)
{
button2.Location = new Point(+100, +100);
}
You will need to create your own form and make it act like a messagebox. Instead of creating a MessageBox, you will instantiate your own form and so that you can handle the buttons on it.
I have this code for PrintPreview and print.
private void button2_Click_1(object sender, EventArgs e)
{
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.ShowDialog();
}
private void printDocument1_PrintPage_1(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(Logo.Image, 800, 100);
e.Graphics.DrawString(label20.Text, label20.Font, Brushes.Black, 134, 100);
e.Graphics.DrawString(label22.Text, label22.Font, Brushes.Black, 462, 100);
e.Graphics.DrawString(textBox101.Text, textBox101.Font, Brushes.Black, 134, 230);
e.Graphics.DrawString(textBox104.Text, textBox104.Font, Brushes.Black, 134, 270);
Now how can I save the same output as the printPreview as a PDF file with another buttonClick action or in the print Preview window.
If you already using the printing features of WinForms it would be the simplest solution install a PDF printer program, e.g. PDFCreator. After installing it can be used like a real printer, but saves a PDF file.
If you want to build in the feature into your application, you should check out this question.
If you are intresteed in creating your own,You can use this .
To add a button in PrintPreviewDialougue ;
class CustomPrintPreviewDialog : System.Windows.Forms.PrintPreviewDialog
{
public CustomPrintPreviewDialog()
: base()
{
if(this.Controls.ContainsKey("toolstrip1"))
{
ToolStrip tStrip1 = (ToolStrip)this.Controls["toolstrip1"];
ToolStripButton button1 = new ToolStripButton();
button1.Text = "Save";
button1.Click += new EventHandler(SaveDocument);
button1.Visible = true;
tStrip1.Items.Add(button1);
}
}
protected void SaveDocument(object sender, EventArgs e)
{
// code for save the document
MessageBox.Show("OK");
}
}
From :Codeproject