How to take the print of wpf user control using c#? - c#

I have one user control in which i displayed the map and the data in the datagrid.
I have tried following code but it does not works.
PrintDialog printDialog = new PrintDialog();
// if user clicks on cancel button of print dialog box then no need to print the map control
if (!(bool)printDialog.ShowDialog())
{
return;
}
else // this means that user click on the print button
{
// do nothing
}
this.Measure(new Size(printDialog.PrintableAreaWidth, printDialog.PrintableAreaHeight));
this.Arrange(new Rect(new Point(20, 20), new Size(this.ActualWidth, this.ActualHeight)));
// print the map control
printDialog.PrintVisual(this, "Karte drucken");
Issue : When data grid has large number of records then user control gets scroll bar, but after printing the user control, only visible part of user control is printed and not the data present which we can see after scrolling down.I want to print whole content of user control.
Is there any solution for this, also how can we see print preview in wpf ?

Please check the following link, it should be helpful. Print Preview WPF
Code
PrintDialog printDlg = new System.Windows.Controls.PrintDialog();
if (printDlg.ShowDialog() == true)
{
//get selected printer capabilities
System.Printing.PrintCapabilities capabilities = printDlg.PrintQueue.GetPrintCapabilities(printDlg.PrintTicket);
//get scale of the print wrt to screen of WPF visual
double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / this.ActualWidth, capabilities.PageImageableArea.ExtentHeight /
this.ActualHeight);
//Transform the Visual to scale
this.LayoutTransform = new ScaleTransform(scale, scale);
//get the size of the printer page
Size sz = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);
//update the layout of the visual to the printer page size.
this.Measure(sz);
this.Arrange(new Rect(new Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), sz));
//now print the visual to printer to fit on the one page.
printDlg.PrintVisual(this, "First Fit to Page WPF Print");
}

Related

Resizing and Collapsing label in winforms

first time poster, sorry if something isn't as it should be.
I'm new to Winforms and am trying to build a simple application that will display multiple features of an item (like Size, Composition, etc.). Each Characteristic has a Name, can have a Descritpion, and some can have sub-characteristics (having also a name and sometimes a descritpion).
I want to display them one under the other, with the Name of the feature on a blue background that span the whole width of the container, with the description underneath. The name will be (or have) a button (or similar) that when clicked collapse or expand the description. This must be created at run time because I don't know how many feature an object has until the user generate it.
The issues I'm running in are that either I can't span the blue background the whole width of the container (if using a FlowLayoutPanel), or I have some issue with the Description text being not the right size (either it wraps but is too big, or it doesn't wrap and then I can't see the whole text).
Some things are fixed, mainly the number of main sections (like Size, Composition, Weather, etc.), so I can prepare the skeleton before runtime.
The closest i've been to making it work gives this. This issue here is that the height of the Panel which the description label is embded in is fixed, and if I put in in Autosize, the text don't show (probably because the label is in Fill dock style). Just as information, this is what it looks like when collapsed (this is indeed what I'm looking for)
I know some library exists with collapsible panels, but I'd rather try to make it work without external libraries. Thanks in advance for any help !
This is the code that produces the results in the screenshots :
Panel SizeDescrPanel = new Panel();
SizeDescrPanel.Dock = DockStyle.Top;
//SizeDescrPanel.AutoSize = true;
SizeDescrPanel.AutoSizeMode = AutoSizeMode.GrowAndShrink;
SizeDescrPanel.BackColor = Color.Bisque;
SizePanel.Controls.Add(SizeDescrPanel);
Label SizeDescrLbl = new Label();
SizeDescrLbl.Text = Lorem;
SizeDescrLbl.AutoSize = false;
SizeDescrLbl.Dock = DockStyle.Fill;
SizeDescrLbl.BackColor = Color.BurlyWood;
SizeDescrPanel.Controls.Add(SizeDescrLbl);
/*using(Graphics g = CreateGraphics())
{
SizeF size = g.MeasureString(SizeDescrLbl.Text, SizeDescrLbl.Font, SizePanel.Width);
SizeDescrPanel.Height = (int) Math.Ceiling(size.Height);
}*/
Panel SizeNamePanel = new Panel();
SizeNamePanel.Dock = DockStyle.Top;
SizeNamePanel.BackColor = Color.Cyan;
SizeNamePanel.AutoSize = true;
SizePanel.Controls.Add(SizeNamePanel);
Button SizeNameBtn = new Button();
SizeNameBtn.Text = "<Size Name> ..." + SizeDescrLbl.Height;
SizeNameBtn.TextAlign = ContentAlignment.MiddleLeft;
SizeNameBtn.FlatStyle = FlatStyle.Flat;
SizeNameBtn.AutoSize = true;
SizeNamePanel.Controls.Add(SizeNameBtn);
SizeNameBtn.Click += delegate { HideShowPanel(SizeDescrPanel); };
It,s a test project, so later I'll put that in different methods. What isn't shown here :
I have a main panel set to Fill containing everything.
The text "SIZE" is a label set to Top
Under it is another Panel (SizePanel) that is set to Top and Autosize is at True. This is the Panel inside which I'm puttin my size name and my size description. If I had a subfeature, it would be included (ideally) inside descritpion with the same configuration (button expanding/collapsing the descritpion of the SubFeature)

C# Form Size won't not set

i have a panel in a form and i load another form in this. this new loaded form have a picture box and the picture was larger as the main form. i want to scroll in the new form and show the picture. i set the size of the new form in code but in the breakpoint i can see the size show the right settings but if i show the settings in a message it show the false settings.
here the code
Form maleTree = new maleTreeForm(maleTreePanel);
maleTree.TopLevel = false;
maleTreePanel.Controls.Add(maleTree);
Form maletreeForm = maleTreePanel.Controls.Find("maleTreeForm", true).FirstOrDefault() as Form;
PictureBox pictureBox = maleTreePanel.Controls.Find("maletreePictureBox", true).FirstOrDefault() as PictureBox;
maletreeForm.Size = new Size(pictureBox.Width, pictureBox.Height);
MessageBox.Show(maletreeForm.Size.toString());
the picturebox.width show in breakpoint 2300, but in messagebox 1300.
i want to set the full 2300 how i can solve this ?
this is the result
you see in the right the scroll bar is not completly down but the image end

WPF WebBrowser in TabItem not showing

I'm using WPF and need to populate a dynamically generated TabControl with a number of tabs.
I'm having trouble with the WebBrowser element, which seems to successfully navigate (I can see the mouse changes cursor as I hover through different elements) but the browser only displays white.
My code is below:
WebBrowser browser = new WebBrowser();
TabItem aTabItem = new TabItem() { Header = "My Tab", Width = 180, FontSize = 16, Content = (browser as WebBrowser) };
(Form.FindName("MyTabControl") as TabControl).Items.Add(aTabItem);
(Form.FindName("MyTabControl") as TabControl).SelectedItem = aTabItem;
browser.NavigateToString("http://www.google.com");
So basically I create the TabItem, add the WebBrowser to it, add the TabItem to the TabControl which will hold all tabs created.
When I try this the WebBrowser isn't displayed. If I swap it for a Label with some text, the Label will happily show up.
Could you give me some pointers?
Thanks
Found the issue. The window where I had the WebBrowser was set as AllowsTransparency="true". I should have known better...
This works for me,
WebBrowser browser = new WebBrowser();
TabItem TI = new TabItem() { Header = "My Tab", Width = 180, FontSize = 16, Content = (browser as WebBrowser) };
tabcontrol.Items.Add(TI);
browser.Source= new Uri("http://www.google.com");
I have used the Source of WebBrowser control. and i am creating the Uri.
NavigateToString(string text) works like below from MSDN
If the text parameter is null, WebBrowser navigates to a blank document ("about:blank").
If the text parameter is not in valid HTML format, it will be displayed as plain text.
After navigation, Source will be null.

Set page size for WPF PrintDialog

I'm trying to print from a WPF application, but struggling to set a default page size.
The content I'm printing is supposed to be on a C5-sized envelope, and most printers here default to A4 paper.
I'd like to show a dialog to the user to allow them to choose the printer to use - but if they just press OK to accept the default printer, it defaults to A4 paper.
How can I set the print defaults for a job to C5 envelop?
Can I still prompt the user for the printer?
private void PrintVisual_Sized(UIElement toPrint)
{
PrintDialog dlg = new PrintDialog();
PrintQueue queue = dlg.PrintQueue;
// Get C5 page size if possible from printer
var availPageSizes = queue.GetPrintCapabilities().PageMediaSizeCapability;
PageMediaSize pageSize = Utilities.GetPageSize(availPageSizes, PageMediaSizeName.ISOC5Envelope);
if (pageSize != null)
{
PrintTicket ticket = new PrintTicket
{
PageMediaSize = pageSize,
InputBin = InputBin.AutoSelect,
CopyCount = 1
};
dlg.UserPageRangeEnabled = false;
var result = dlg.PrintQueue.MergeAndValidatePrintTicket(dlg.PrintTicket, ticket);
Debug.Print(result.ConflictStatus.ToString());
// Try to get the page size honoured by someone!!!
dlg.PrintQueue.DefaultPrintTicket = result.ValidatedPrintTicket;
dlg.PrintQueue.UserPrintTicket = result.ValidatedPrintTicket;
dlg.PrintTicket = result.ValidatedPrintTicket;
// Height still seems to be A4 sized!?
Debug.Print("Height: " + dlg.PrintableAreaHeight);
}
// Ask user which printer they want...
if (dlg.ShowDialog().GetValueOrDefault(false))
{
Size printSize = new Size(dlg.PrintableAreaWidth, dlg.PrintableAreaHeight);
toPrint.Measure(printSize);
toPrint.Arrange(new Rect(new Point(), printSize));
toPrint.UpdateLayout();
dlg.PrintVisual(toPrint, "My Print Job");
}
}
In the last section printSize is A4 unless the user manually selects another paper size.
Is there any way to display the dialog, with a non-default page size preset?

Printing without ShowDialog gives blank pages

I am experiencing an odd issue with printing from a WPF project. I'm printing a screen capture of the application for reporting purposes, and all that works just fine. Currently the user presses print, the print dialog appears, and they print out the capture image.
However, I want to be able to print directly to the default printer without showing the dialog box. This should be easily done by commenting out the ShowDialog() statement and allowing the rest to just happen. The printer still prints, but the pages are always blank.
Can anyone explain this behavior?
private void PrintCurrentScreen()
{
PrintDialog PD = new PrintDialog();
PD.PrintTicket.OutputColor = OutputColor.Grayscale;
PD.PrintTicket.OutputQuality = OutputQuality.Draft;
PrintTicket PT = new PrintTicket();
PT.PageOrientation = PageOrientation.Landscape;
PT.CopyCount = 1;
PT.PageBorderless = System.Printing.PageBorderless.Borderless;
//---Blank pages print when commented out---//
//if (PD.ShowDialog() == true)
//{
PD.PrintTicket = PT;
DrawingVisual DV = new DrawingVisual();
DV.Offset = new Vector(20, 20);
DrawingContext DC = DV.RenderOpen();
DC.DrawImage(previewimage.Source, new Rect(new Size(PD.PrintableAreaWidth - 40, PD.PrintableAreaHeight - 40)));
DC.Close();
PD.PrintVisual(DV, "TEST");
//}
}
Try doing a Measure, Arrange, and UpdateLayout right before the printvisual, like this:
DV.Measure(new System.Windows.Size(PD.PrintableAreaWidth,
PD.PrintableAreaHeight));
DV.Arrange(new System.Windows.Rect(new System.Windows.Point(0, 0),
DV.DesiredSize));
DV.UpdateLayout();
PD.PrintVisual(DV, "TEST");

Categories