This question already has answers here:
How to configure an app to run correctly on a machine with a high DPI setting (e.g. 150%)?
(6 answers)
Closed 6 years ago.
I'm trying to print my Windows Form. My solution is:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(bmp, 0, 0);
}
Bitmap bmp;
private void button1_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
bmp = new Bitmap(this.Size.Width, this.Size.Height, g);
Graphics mg = Graphics.FromImage(bmp);
mg.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, this.Size);
printDialog1.ShowDialog();
printDocument1.Print();
printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
}
}
But it gives me this result:
(frame is made by me to show the borders of the printed paper)
How can i solve this problem?
try this
private void button1_Click(object sender, EventArgs e)
{
//Add a Panel control.
Panel panel = new Panel();
this.Controls.Add(panel);
//Create a Bitmap of size same as that of the Form.
Graphics grp = panel.CreateGraphics();
Size formSize = this.ClientSize;
bitmap = new Bitmap(formSize.Width, formSize.Height, grp);
grp = Graphics.FromImage(bitmap);
//Copy screen area that that the Panel covers.
Point panelLocation = PointToScreen(panel.Location);
grp.CopyFromScreen(panelLocation.X, panelLocation.Y, 0, 0, formSize);
//Show the Print Preview Dialog.
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.PrintPreviewControl.Zoom = 1;
printPreviewDialog1.ShowDialog();
}
Ref : http://www.aspsnippets.com/Articles/Print-contents-of-Form-in-Windows-Forms-WinForms-Application-using-C-and-VBNet.aspx
Related
Hello guys goodevening to you... how can i remove the button 6 to my print preview ? i dont have any idea even when i false the visible of the button6
Bitmap bitmap;
private void CaptureScreen()
{
Graphics myGraphics = this.CreateGraphics();
Size s = this.Size;
bitmap = new Bitmap(s.Width, s.Height, myGraphics);
Graphics memoryGraphics = Graphics.FromImage(bitmap);
memoryGraphics.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s);
}
private void button6_Click(object sender, EventArgs e)
{
Panel panel = new Panel();
this.Controls.Add(panel);
Graphics grp = panel.CreateGraphics();
Size formSize = this.ClientSize;
bitmap = new Bitmap(formSize.Width, formSize.Height, grp);
grp = Graphics.FromImage(bitmap);
Point panelLocation = PointToScreen(panel.Location);
grp.CopyFromScreen(panelLocation.X, panelLocation.Y, 0, 0, formSize);
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.PrintPreviewControl.Zoom = 1;
printPreviewDialog1.ShowDialog();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(bitmap, 0, 0);
}
}
}
//CODE
private void button6_Click(object sender, EventArgs e)
{
button6.Visible = false;//add here
Panel panel = new Panel();
this.Controls.Add(panel);
Graphics grp = panel.CreateGraphics();
Size formSize = this.ClientSize;
bitmap = new Bitmap(formSize.Width, formSize.Height, grp);
grp = Graphics.FromImage(bitmap);
Point panelLocation = PointToScreen(panel.Location);
grp.CopyFromScreen(panelLocation.X, panelLocation.Y, 0, 0, formSize);
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.PrintPreviewControl.Zoom = 1;
printPreviewDialog1.ShowDialog();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(bitmap, 0, 0);
}
private void printPreviewDialog1_FormClosed(object sender, FormClosedEventArgs e)
{
button6.Visible = true;//this is to enable button after closing print screen
}
In order for the button to actually become invisible, you have to force the events to actually run.
In your button6_Click event handler you begin with the lines:
button6.Visible = false;
Application.DoEvents();
The DoEvents() method forces the events in the event queue to run. This is because you run on the UI thread. I would not recomend you move the copying operation to another thread, but another way would be to start an UI timer (found under Components in the tools menu) and have that timer timout at a few ms. You can then have the copying operation there. But the easiest solution is to simply add the Application.DoEvents() so that is my recommendation.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
So i'm relatively new to C# and i want to ride an application that shows you the color of the pixel you hover over but the with this code there seems to be a big problem.
thx a lot for the help
the code:
private void timer1_Tick(object sender, EventArgs e)
{
b = null;
b = Screenshot();
Color color = b.GetPixel(Cursor.Position.X, Cursor.Position.Y);
label1.Text = color.Name;
label2.Text = Cursor.Position.Y.ToString() + Cursor.Position.X.ToString();
}
private Bitmap Screenshot()
{
Bitmap Screen = new Bitmap(SystemInformation.VirtualScreen.Width,SystemInformation.VirtualScreen.Height);
Graphics g = Graphics.FromImage(Screen);
g.CopyFromScreen(SystemInformation.VirtualScreen.X,SystemInformation.VirtualScreen.Y, 0, 0, Screen.Size);
return Screen;
}
You neither need a timer nor copy the whole screen. Just add a MouseMove handler like this:
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
using (var bitmap = new Bitmap(1, 1))
{
var graphics = Graphics.FromImage(bitmap);
var position = PointToScreen(e.Location);
graphics.CopyFromScreen(position.X, position.Y, 0, 0, new Size(1, 1));
var color = bitmap.GetPixel(0, 0);
label1.Text = color.ToString();
}
}
Or reuse the Bitmap:
private readonly Bitmap bitmap = new Bitmap(1, 1);
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
var graphics = Graphics.FromImage(bitmap);
var position = PointToScreen(e.Location);
graphics.CopyFromScreen(position.X, position.Y, 0, 0, new Size(1, 1));
var color = bitmap.GetPixel(0, 0);
label1.Text = color.ToString();
}
Your code works perfectly when you use it in a correct way: (no global 'b')
private void Timer1_Tick(object sender, EventArgs e)
{
using (var b = Screenshot())
{
label1.Text = b.GetPixel(Cursor.Position.X, Cursor.Position.Y).Name;
label2.Text = Cursor.Position.ToString();
}
}
private static Bitmap Screenshot()
{
var Screen = new Bitmap(SystemInformation.VirtualScreen.Width, SystemInformation.VirtualScreen.Height);
var g = Graphics.FromImage(Screen);
g.CopyFromScreen(SystemInformation.VirtualScreen.X, SystemInformation.VirtualScreen.Y, 0, 0, Screen.Size);
return Screen;
}
But of course, try to copy whole screen only for a single pixel is wrong....
Therefore use something like the fixed code from Clemens.
e.g.:
private readonly Bitmap screen = new Bitmap(1, 1);
private static readonly Size size = new Size(1, 1);
private void Timer1_Tick(object sender, EventArgs e)
{
using (var g = Graphics.FromImage(screen))
{
g.CopyFromScreen(Cursor.Position.X, Cursor.Position.Y, 0, 0, size);
label1.Text = screen.GetPixel(0, 0).Name;
label2.Text = Cursor.Position.ToString();
}
}
And do not forget to dispose Bitmap in the form Dispose method.
Here is my source code. I can't seem to get the bitmap to show the lines drawn on the panel when I move the mouse with the button pressed. Frustrated and looking for someone to help me finish the code so I can complete the app for my 9-yo daughter. Thank you in advance...
namespace TV_PAINT
{
public partial class ALANA_PAINT : Form
{
Graphics g;
Pen p = new Pen(Color.Black, 7);
Point sp = new Point(0, 0);
Point ep = new Point(0, 0);
int m = 0;
Bitmap BP;
public ALANA_PAINT()
{
InitializeComponent();
tb1.Text = p.Width.ToString();
BP = new Bitmap(pnl1.ClientSize.Width, pnl1.ClientSize.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
}
private void closeButton_Click(object sender, EventArgs e)
{
pnl1.Dispose();
p.Dispose();
this.Close();
}
private void clearButton_Click(object sender, EventArgs e)
{
//pnl1.Invalidate();
p.Color = System.Drawing.Color.Black;
p.Width = 7;
tb1.Text = p.Width.ToString();
//pnl1.Invalidate();
}
private void pnl1_MouseDown(object sender, MouseEventArgs e)
{
sp = e.Location;
if (e.Button == MouseButtons.Left)
m = 1;
if (e.Button == MouseButtons.Right)
m = 1;
}
private void pnl1_MouseMove(object sender, MouseEventArgs e)
{
if (m == 1)
{
ep = e.Location;
//g = pnl1.CreateGraphics();
Graphics g = Graphics.FromImage(BP);
g.DrawLine(p, sp, ep);
}
sp = ep;
}
private void pnl1_MouseUp(object sender, MouseEventArgs e)
{
m = 0;
}
BP is just a variable in the form. As I can see, it is not displayed anywhere in your form. Why do you need a bitmap for it.
You can do something like this, just get the graphics of your form, and draw using that graphic. https://msdn.microsoft.com/en-us/library/ztxk24yx(v=vs.110).aspx
Noted: you need to do it on PaintEvent of the form, otherwise your drawing will be removed after the next repaint, so you need some variables to store all of your lines, then draw all of them in the paint event.
System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics;
formGraphics = this.CreateGraphics();
formGraphics.FillRectangle(myBrush, new Rectangle(0, 0, 200, 300));
myBrush.Dispose();
formGraphics.Dispose();
Updated:
If you want to save your change to a bitmap. You can use Form.DrawToBitmap to save your drawing in the form to a bitmap, then call bitmap.Save() to a file in directory.
Im using WFA and C#, and im trying to draw an elipse on top of a picturebox containing an image. here is my code but currently its drawing behind of the PB.
private void Form1_Paint(object sender, PaintEventArgs e)
{
PictureBox pb=new PictureBox();
pb.Location=new Point(10,25);
pb.BackgroundImage = Image.FromFile("fire2_clipped_rev_1.png");
pb.Size = Image.FromFile("fire2_clipped_rev_1.png").Size;
Truck.TruckF(pb.Location, pb.CreateGraphics());
pb.Invalidate();
Controls.Add(pb);
}
static Image truckf = Image.FromFile("fire2_clipped_rev_1.png");
public static void TruckF(Point location, Graphics e)
{
Wheels(truckf.Size,location,e);
}
private static void Wheels(Size simage,Point location,Graphics e)
{
e.FillEllipse(Brushes.Black, location.X / 6F, location.Y / 1.43F, 20, 20);
}
You need to change to handle the Load event on your form. Also the PictureBox is a control and you need to draw an image and hand it to the PictureBox to get this to work. Try this:
private PictureBox pb;
private void Truck_Load(object sender, EventArgs e)
{
pb = new PictureBox();
pb.Location = new Point(10, 25);
Image bg = Image.FromFile("fire2_clipped_rev_1.png");
pb.BackgroundImage = bg;
pb.Size = bg.Size;
Bitmap img = new Bitmap(pb.Size.Width, pb.Size.Height);
pb.Image = img;
using (var g = Graphics.FromImage(img))
{
Truck.TruckF(pb.Location, g);
}
Controls.Add(pb);
}
Using the code example from MSDN on how to print a windows form, I have the altered mine to bring up the printer options first and then print, but I keep receiving a blank page. Using CopyFromScreen, I am giving the coordinates of the forms source X & Y, but for the destination I have tried 0 as well as this.Location.X & Y. Is there another way to capture the image?
private void printButton_Click(object sender, EventArgs e)
{
CaptureScreen();
printDialog1.AllowSomePages = true;
printDialog1.ShowHelp = true;
printDialog1.Document = printDoc1;
DialogResult result = printDialog1.ShowDialog();
if (result == DialogResult.OK)
{
printDoc1.Print();
}
}
private void CaptureScreen()
{
Graphics myGraphics = this.CreateGraphics();
Size s = this.Size;
memoryImage = new Bitmap(s.Width, s.Height, myGraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
memoryGraphics.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s);
}
void printDoc1_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawImage(memoryImage, 0, 0);
}