Bad resolution when print panel from form - c#

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.

Related

Print All the items in a windows form in C#

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.

Tooltip background alternation

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

Saving Location for next form

Alright so I'm doing something with next buttons that open new forms, annoying thing is that new forms pop up somewhere I don't want to on the desktop.
I'm trying to get the new form to spawn on the location of the old form with the code below, unfortunately for whatever reason it's not working at all, they still pop up the same way as before. And yes I have registered the events.
Form1:
System.Drawing.Point LocationPoint = new System.Drawing.Point(200,200);
private void Installer_template_LocationChanged(object sender, EventArgs e)
{
// Save the window location to the installer arts
LocationPoint = this.Location;
}
private void NextButton_Click(object sender, EventArgs e)
{
var NextForm = new Form2(LocationPoint);
NextForm.Show();
this.Hide();
}
Form2
public Form2(System.Drawing.Point LocationPoint)
{
InitializeComponent();
this.Location = LocationPoint;
}
The code is something along those lines
Have you tried setting the StartPosition of the new forms, i.e.
this.StartPosition = FormStartPosition.Manual;
or
this.StartPosition = FormStartPosition.CenterParent;
Alrighty I fixed it, it was a bunch of problems.
Wrong property, gotta use DesktopLocation instead of Location property
Second I had some issues with static member that couldn't be modified or whatever the error is, I just used the settings file to save my location instead
Having that done, it still didn't work because you can't just do this.DesktopLocation = something, you have to use this.SetDesktopLocation(X, Y)
Still didn't work, because it got overwritten by other code when loading the form so you had to use the Shown even of the form and run it in there..

How can i show an image while my application is loading

i have and application windows form .net and my form1 takes a lot of time to appear because in it's event form1_Load does a lot of operation.
My goal is to show an image while the operation are being done.
private void form1_Load(object sender, EventArgs e)
{
methode1();
}
While my methode1() is working, my form doesnt show, i want to show an image on the screen while my methode1() is working because while methode1() is working, there is nothing on the screen.
All the visual things in .net is done on form. You can do it by creating an small form which contains an image load it before module1() and after completing module1() close it. Just below..
private void form1_Load(object sender, EventArgs e)
{
Form f = new Form();
f.Size = new Size(400, 10);
f.FormBorderStyle = FormBorderStyle.None;
f.MinimizeBox = false;
f.MaximizeBox = false;
Image im = Image.FromFile(path);
PictureBox pb = new PictureBox();
pb.Dock = DockStyle.Fill;
pb.Image = im;
pb.Location = new Point(5, 5);
f.Controls.Add(pb);
f.Show();
methode1();
f.Close();
}
Create another form, just for loading, with a static image, and display it before your application starts to load, and destroy it afterwards. Always on top, and with no border is the usual setup for such things.
Try this code
using System.Reactive.Linq;
private void RealForm_Load(object sender, EventArgs e)
{
var g = new Splash();
// place in this delegate the call to your time consuming operation
var timeConsumingOperation = Observable.Start(() => Thread.Sleep(5000));
timeConsumingOperation.ObserveOn(this).Subscribe(x =>
{
g.Close();
this.Visible = true;
});
this.Visible = false;
g.ShowDialog();
}
This code uses Microsoft Rx to execute operations in background threads among other cool features
http://msdn.microsoft.com/en-us/data/gg577609.aspx
In order for this code to work you need to reference two nuget packages: Rx and Rx windows forms
https://nuget.org/packages/Rx-Main/1.0.11226
https://nuget.org/packages/Rx-WinForms/1.0.11226
(splash screen c# -- google it)
Here's what I just found:
http://msdn.microsoft.com/en-us/library/aa446493.aspx
How about using the built in SplashScreen class?
http://msdn.microsoft.com/en-us/library/system.windows.splashscreen.aspx

.NET PrintDocument - Text gets cut off

I have an application (C#, .NET 3.5) that writes receipts. They are usually printed with a small receipt printer with the .NET PrintDocument. The problem is the exception cases where I want to print with a regular printer. In these cases the text gets cut off. I wish to have a check or a switch to prevent this, but still keep the tight margins on the small printer.
What would be the best way of dealing with this? Can I do this without touching the graphics generation?
Once you have set the Printer in PrintDocument, you might take a look at the present PaperSize using the value of:
PrintDocument.PrinterSettings.DefaultPageSettings.PaperSize
Or maybe:
PrintDocument.PrinterSettings.PaperSizes
My issue was that I was setting the printerDocument.DefaultPageSettings.PaperSize.
Instead, I recommend this
private PrintDocument _printDocument = new PrintDocument();
private int _checkPrint;
public Form1()
{
InitializeComponent();
_printDocument.BeginPrint += _printDocument_BeginPrint;
_printDocument.PrintPage += _printDocument_PrintPage;
}
private void btnPrint_Click(object sender, EventArgs e)
{
PrintDialog printDialog=new PrintDialog();
if (printDialog.ShowDialog() == DialogResult.OK)
_printDocument.Print();
}
private void _printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
// Print the content of RichTextBox. Store the last character printed.
_checkPrint = rchEditor.Print(_checkPrint, rchEditor.TextLength, e);
// Check for more pages
e.HasMorePages = _checkPrint < rchEditor.TextLength;
}
private void _printDocument_BeginPrint(object sender, PrintEventArgs e)
{
_checkPrint = 0;
}

Categories