I have a win form where i have some label,a datagridview and some textbox.I want to print all of my items in my form in exact format it shows in the form. I have tried it using taking snap of my form then print it. But in this way i can't have my full form as my form is large enough.It only shows half portion of my form.I don't know is it the best practice on not.Is there any other way of doing this please mention
My Sample code:
private void btnPrint_Click(object sender, EventArgs e)
{
CaptureScreen();
printDocument1.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);
}
private void printDocument1_PrintPage_1(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawImage(memoryImage, 0, 0);
}
As i am new in C#,it will be great help if anyone help me with some sample code.
I think you should emulate your form layout in a page or text description language like PDF or RTF. Taking a screenshot programmatically is in a way elegant but also crude at the same time, and runs into limitations of which you encountered just one.
One thing to consider is whether you should serialize your data e.g. as XML first, and only then export to data formats suitable for printing or displaying, like PDF or HTML.
Related
The single tooltip which shows different messages for different controls. Now the Problem is the background image is not fit/suitable to all messages. I supposed to call the draw event of the tooltip for custom size, Font etc.,
I able to successfully call the draw and Popup event of the tooltip for particular message but setting the generalized size for different messages(e.ToolTipText) is unknown to me.
public void tooltip_Popup(object sender, PopupEventArgs e)
{
e.ToolTipSize = new Size(100, 100);
}
Kindly let me know anybody have any idea about it.
you can set the size in the Popup event, like this:
private void toolTip1_Popup(object sender, PopupEventArgs e)
{
e.ToolTipSize = new Size(200, 200);
}
result of my test is this, hope it's helpful to you.
I found the answer for my Problem. The below code of POPUP event will change the tooltipsize according to the text size.
public void toolTip_Popup(object sender, PopupEventArgs e)
{
using (Font f = new Font("Arial", 12f))
{
e.ToolTipSize = TextRenderer.MeasureText(
toolTips.GetToolTip(e.AssociatedControl), f);
}
My project is to print a panel from form
but the resolution was so terrible
I checked a lot of posts
Knowing that the main problem is DPI difference between monitor and printer
I try to set my bitmap big and print it at right size i want
but it seems i cant set them well
the resolution still bad and the size change like uncontralable
if i set my bitmap size greater then 10k
it will disappear from print preview dialog
my code here
Bitmap MemoryImage;
public void GetPrintArea(Panel pnl)
{
MemoryImage = new Bitmap(9000,9000);
Rectangle rect = new Rectangle(0,0,9000,9000);
pnl.DrawToBitmap(MemoryImage, rect);
}
void doc_PrintPage(object sender, PrintPageEventArgs e)
{
GetPrintArea(flowLayoutPanel1);
Rectangle pagearea = new Rectangle(0,0,5000, 5000);
e.Graphics.DrawImage(MemoryImage,pagearea);
}
PrintDocument doc = new PrintDocument();
private void button1_Click(object sender, EventArgs e)
{
PrintDocument doc = new PrintDocument();
doc.PrintPage += new PrintPageEventHandler(doc_PrintPage);
PrintPreviewDialog dlg = new PrintPreviewDialog();
PaperSize psize = new PaperSize("A4 300DPI", 2480, 3508);
doc.PrinterSettings.DefaultPageSettings.PaperSize = psize;
dlg.Document = doc;
dlg.ShowDialog();
}
I want to get panel and print it on A4 with resolution like printing a word document
I spent almost a week try to solve this problem >"<
Plz save me ...
I spent almost a week try to solve this problem
You can't make it to work even if you try another week, because you are on the wrong track.
Knowing that the main problem is DPI difference between monitor and printer
You know the problem and know that there is no solution for that you still keep trying.
Solution:
If you want to render some data on report, i don't think printing of panel is the solution. you should consider printing report in the form of a report. Like, consider making an RDLC/Crystal report and put your desired data on that report. In short, use Reports for reporting purpose.
So ive tried following a load of different tutorials on Stack and the internet on how to do this, but i'm getting nowhere.
this is the closest ive gotten:
private void button1_Click(object sender, EventArgs e)
{
Image image1 = Image.FromFile("S:\\Software\\C#\\Project\\WindowsFormsApplication1\\1.png");
Image image2 = Image.FromFile("S:\\Software\\C#\\Project\\WindowsFormsApplication1\\2.png");
using (Graphics g = Graphics.FromImage(image1))
{
g.DrawImageUnscaled(image2, 0, 0);
}
}
And when i click my button, it executes the code but literally nothing happens, why is this?
You have to do something with your image object afterwards. You have at least two possibilities:
Either save it back to a file using the Image.Save method, e.g.
image1.Save("S:\\Test.jpg");
or place a PictureBox on your Form and put it in there
PictureBox1.Image = image1;
...to place it in a new Window:
Form imgForm = new Form();
imgForm.BackgroundImage = image1;
imgForm.Show();
So I've written a class that has a stores some test results info and then a control that displays that info to the user. I want to put a print function on this class to draw the control at a full page size and print it. However it always comes out blank. The code see the panel as a control because it could be some other type of value. I figure there must be something simple I'm missing.
void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
System.Drawing.Size oldSize = printData.Size;
printData.Size = new System.Drawing.Size(e.MarginBounds.Width, e.MarginBounds.Height);
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(printData.Size.Width, printData.Size.Height);
InvertZOrderOfControls(printData.Controls);
printData.DrawToBitmap(bitmap, new System.Drawing.Rectangle(0, 0, printData.Size.Width, printData.Size.Height));
InvertZOrderOfControls(printData.Controls);
e.Graphics.DrawImage(bitmap, e.MarginBounds.Location);
bitmap.Save(#"C:\Users\jdudley\Documents\File.bmp");
printData.Size = oldSize;
}
Following this advice of this thread inverted the Z-Order of the controls but it didn't change anything.
The save call was added for debugging. It looks like it's actually rendering the background color of the panel without any of the controls.
Edit: This is in the context of printing but I have not issue with printing what so ever. My error is in creating the bitmap. The save line I added proves this because it creates a blank bitmap file.
Change your entire event to this
void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(printData.Width, printData.Height);
printData.DrawToBitmap(bitmap, new System.Drawing.Rectangle(new Point(0, 0), printData.Size));
e.Graphics.DrawImage(bitmap, e.MarginBounds.Location);
}
Edit
This is my whole Project. I created a panel named printData and I added two buttons, I attached an event to button1.
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
PrintDocument printDocument = new PrintDocument();
public Form1()
{
InitializeComponent();
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
}
void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(printData.Width, printData.Height);
printData.DrawToBitmap(bitmap, new System.Drawing.Rectangle(new Point(0, 0), printData.Size));
e.Graphics.DrawImage(bitmap, e.MarginBounds.Location);
}
private void button1_Click(object sender, EventArgs e)
{
pd.Print();
}
}
}
You have to try this and see if it works, or else I wont be able to sleep tonight!!
i have an ms chart control on the form and i would like to print the chart. what is the best way to do this?
This might be a bit convoluted for your purposes, but I've used the PrintDocument object to draw a background image on pages of a report. You could do something similar, where you use the Graphics object from the PrintPageEventArgs to "paint" your chart image.
This code would print a 1 page document with a small rectangle drawn in the upper corner. I would think you could replace the drawing there with the drawing of your chart
class Program
{
public class Document : System.Drawing.Printing.PrintDocument
{
protected override void OnBeginPrint(System.Drawing.Printing.PrintEventArgs e)
{
base.OnBeginPrint(e);
}
protected override void OnPrintPage(System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawRectangle(SystemPens.ActiveBorder, new Rectangle(0, 0, 20, 20));
}
}
static void Main(string[] args)
{
System.Drawing.Printing.PrintDocument pd = new Document();
pd.Print();
}
}
Another flexible solution is to export the chart to PDF and let the user print it out from Adobe Reader, and he/she will be able to save the chart or send it by email as well...