The Dudes' Blog

Technology Solutions to Your Business Problems

Printing a WPF Form

Posted by MichaelBird on October 9, 2014

I was recently looking to add the capability to print a WPF form for a project I was working on. Like most people, I searched the internet for samples from others who had already solved this problem. The code I came upon, from multiple posts and blogs, was the same small block. When I tried it with the Xerox color copier/printer our company used however, I never printed quite right.

The code looked like the page size it should get back would change based on whether you chose landscape or portrait mode, but for my printer it didn’t. This may have been a driver issue, I don’t know, but it just wasn’t working.

I altered the code so that it seems to work the the 5 different printers I have tried it on. It’s not much longer than the original. So if you find yourself having issues scaling to the page with the original code you find out there, here is my updated method. Just add this method to the code-behind of your WPF form. You’ll probably want to add some error handling here. I’ve kept it short for posting.

  1. #region Print
  2. private void PrintForm()
  3. {
  4.     PrintDialog printDlg = new System.Windows.Controls.PrintDialog();
  5.     if (printDlg.ShowDialog() == true)
  6.     {
  7.         //get selected printer capabilities
  8.         System.Printing.PrintCapabilities capabilities = printDlg.PrintQueue.GetPrintCapabilities(
  9.             printDlg.PrintTicket);
  10.         System.Printing.PrintJobSettings settings = printDlg.PrintQueue.CurrentJobSettings;
  11.  
  12.         //get the size of the printer page
  13.         Size oldSize = new Size(ActualWidth, ActualHeight);
  14.         double pageWidth = 0, pageHeight = 0, pageYMargin = 0, pageXMargin = 0;
  15.  
  16.         switch (settings.CurrentPrintTicket.PageOrientation)
  17.         {
  18.         case System.Printing.PageOrientation.Portrait:
  19.             pageWidth = settings.CurrentPrintTicket.PageMediaSize.Width.Value;
  20.             pageHeight = settings.CurrentPrintTicket.PageMediaSize.Height.Value;
  21.             pageYMargin = capabilities.PageImageableArea.OriginWidth;
  22.             pageXMargin = capabilities.PageImageableArea.OriginHeight;
  23.             break;
  24.         case System.Printing.PageOrientation.Landscape:
  25.             pageWidth = settings.CurrentPrintTicket.PageMediaSize.Height.Value;
  26.             pageHeight = settings.CurrentPrintTicket.PageMediaSize.Width.Value;
  27.             pageYMargin = capabilities.PageImageableArea.OriginHeight;
  28.             pageXMargin = capabilities.PageImageableArea.OriginWidth;
  29.             break;
  30.         default:
  31.             System.Diagnostics.Debug.Assert(false, "Unhandled page orientation.");
  32.             break;
  33.         }
  34.  
  35.         Size sz = new Size(pageWidth – (pageYMargin * 2),
  36.             pageHeight – (pageXMargin * 2));
  37.  
  38.         //get scale of the printer to screen of WPF visual
  39.         // extents assume landscape for some reason so have to swap if portrait
  40.         double scale = Math.Min(sz.Width / this.ActualWidth,
  41.             sz.Height / this.ActualHeight);
  42.  
  43.         //Transform the Visual to scale
  44.         Transform oldTransform = LayoutTransform;
  45.         this.LayoutTransform = new ScaleTransform(scale, scale);
  46.  
  47.         //update the layout of the visual to the printer page size.
  48.         this.Measure(sz);
  49.         this.Arrange(new Rect(new Point(pageYMargin, pageXMargin), sz));
  50.  
  51.         //now print the visual to printer to fit on the one page.
  52.         printDlg.PrintVisual(this, "First Fit to Page WPF Print");
  53.  
  54.         this.LayoutTransform = oldTransform;
  55.         this.Measure(oldSize);
  56.         this.Arrange(new Rect(new Point(0, 0), oldSize));
  57.     }
  58. }
  59. #endregion

That’s it. Not a big change from the original code, but it certainly helped with my Xerox printer.

Posted in .NET, C#, Programming, XAML | Tagged: , | Leave a Comment »

Thoughts on Windows Phone 8.1

Posted by MichaelBird on April 16, 2014

I’ve been playing with WP8.1 for a few days now, and here are my thoughts, good, bad, and othewise…

Good:

  • Cortana. The speech recognition is great. Location based reminders are nice. What it does with requests could be improved but hey, it’s v1.0. I would especially like to see it categorize reminders such as bills, meetings, tasks… so I could ask what meetings are coming up in the next 2 days, or what bills are due in the next 5…
  • Start screen backgrounds are a nice addition.
  • The message center is nice but I don’t use it too much except to short cut to settings and to turn Bluetooth or Wi-Fi on and off.
  • Wi-Fi sense was cool, although a bit difficult to get into since you had to turn Wi-Fi off to see the settings for automatically turning it on.
  • New store UI seems better.
  • As a developer, the new background tasks that allow you to create app icons locally is very nice. I also like the universal apps idea.
  • The slide keyboard is nice if you’re into that sort of thing.

Bad:

  • I liked the old photos hub much better. It looked nicer and included social photos from contacts… There seems to be no replacement for the social part.
  • I don’t like my games in my app list with the other apps. When I first got a Windows Phone I didn’t like that they were under games, but now I’ve gotten used to it and I don’t like the clutter.
  • You can no longer use the messaging app to send a message to a Facebook contact.
  • If you had an app that would allow you to share something to Facebook or Twitter or LinkedIn using the built-in stuff, that no longer works, it will only send email or share to OneNote. Apparently this will be the case until you get an app for each of those that supports sharing (ala Windows 8).

Otherwise:

  • The background tasks have moved from the application settings to the battery saver settings. Took me a while to find them, but now that I know where they are it’s not big issue.
  • My email was freaked out for over a day. Always showed as syncing. Two accounts still received main but Yahoo didn’t for about a day. Since they always showed as syncing, I couldn’t even edit or deleting them in the settings.
  • My battery life seems very much diminished; hence why I now know where the background tasks list is.
  • On the confusing side, the People hub still shows content from Facebook, LinkedIn, and Twitter. Not sure why they removed social everywhere else but left it in there…

Overall, the OS seems more complete in many ways, but the loss of the built-in social network integration in favor of integration with apps that support sharing seems to be a step backwards to me.

Sending a message to a Facebook contact with the same app that sent SMS seemed right, and was easier since I could send to SMS and Facebook simultaneously. I now would have to install a Facebook app, and use two different apps to send my message. Nowhere near as efficient.

Sharing something from an app with Facebook and LinkedIn at the same time was nice. Now they’re gone and I shall miss them. And apparently I have to wait till there are apps for my social networks that support Sharing on the phone, if they ever come…, and I doubt if I’ll be able to share with multiple networks at once ever again, but that remains to be seen.

 

UPDATE:

The email issue came back this morning as well as the phone battery going to less than 50% in 4 hours! I ended up going into airplane mode, restating the phone, deleting the Yahoo, POP, and IMAP email addresses, then recreating them. So far, that seems to have solved the issue although we’ll see.

 

Posted in Windows Phone | Tagged: | Leave a Comment »

New MS Analytics Tool

Posted by MichaelBird on April 10, 2014

In case you haven’t been watching, Microsoft released a new preview edition of an analytics tool for your software called Application Insights.

It can automatically add itself into Windows Store, Windows Phone, and Web applications. You can download inside Visual Studio as an Extension. Once installed, if you create a new Windows Store, Phone, or Web project, it can inject code into your application that will record basic information about your app such as unique users, page hits, launches/sessions, client OS/browser version, application version… You can add addition calls of your own to track even more data about your app. You can watch a video about it on Channel9 here: Make Data-Driven, High-Impact Improvements to an Application with Application Insights.

It is part of Visual Studio Online and is free at present. They said that all of the automatic tracking they add will remain free but there will also be features that will have costs in the future. Certainly worth checking out if you were thinking about adding some analytics to your projects.

UPDATE: Unlike in the video for a web site, a Windows Phone 8 app is not automatically set up. You have to add a line of code. If you log into your Visual Studio Online account (at the URL you chose when you created the account, not visualstudioonline.com), click the Usage tab and tell it what app type you are creating (in this case Windows Phone 8), it will give you a link to a page with any instructions you need to set up things in your app.

Posted in .NET, Programming | Tagged: , , , , | Leave a Comment »

Beer Tracker 3.4 Released

Posted by MichaelBird on November 21, 2013

Version 3.4 of Beer Tracker is now out in the Windows Phone store. The primary feature added was tracking of the last time you drink each beer so you can sort beers based on when you last had one. There are other features and some minor bug fixes as well. Go get the latest update and don’t forget to email your suggestions or issues to us using the link on the About page of the App.

Posted in General, Programming, Windows Phone | Tagged: , | Leave a Comment »

New Windows Phone App

Posted by MichaelBird on July 25, 2012

Sharp Dudes recently put out its first Windows Phone application, it’s called Beer Tracker. I enjoy trying different kinds of beers, especially when I travel and find some local microbrewery. Over time, I’ve tried so many different kinds that I can’t always remember if I’ve tried a beer before, and if I do remember, I don’t always remember if I liked it. So, enter Beer Tracker.

Beer Tracker lets you enter the brewery and beer name, rating, information such as hoppiness, maltiness, bitterness, fruitiness… as well as comments and a description. You can also add a photo to each beer; especially handy to remember what the bottle or tap looked like. You can filter your beer list in many ways to find one you are looking for.

There are future updates planned to add SkyDrive backup as well as more information about the beers. If it’s popular enough, we’ll consider writing Android and IPhone versions. For now, if you’re a beer connoisseur with a Windows Phone, give it a try.

Beer Tracker Marketplace Link

Update. The app now includes SkyDrive backup, more beer data that can be collected, more filters, sorting… Still looking into a good search provider for fetching information on new beers you try as well as posting to facebook and/or twitter.

Posted in General, Programming, Windows Phone | Tagged: , | 3 Comments »

WP7 Panorama Background Glitch

Posted by MichaelBird on January 25, 2012

Just a heads up to all the WP7 developers out there using the Panorama control. I recently noticed that when I started my application, the panorama background would not be left-aligned. It would start with the seam between the two edges on the screen, then scroll into the correct placement moments later.

After playing around, I discovered that it had to do with the Build Action on the background image. I has it set to Resource instead of Content. Apparently both work, but only one of them works correctly. What will seem weird is that if you set the Build Action to Content, you will get a warning in the XAML where you set your panorama background telling you that your image should be set as a Resource. You have to ignore that warning or you will get the odd behavior.

Posted in WP7, XAML | Tagged: , , , | Leave a Comment »

.NET User Group Presentation

Posted by MichaelBird on November 10, 2011

I recently gave a presentation called “Introduction to Visual Studio LightSwitch” to the Pittsburgh .NET user group. Attached is a zip file of the
slides for the presentation and an example project I built.

There is some sample code in the project that shows how to override a default add/edit modal screen with your own and have the close button work correctly as “cancel” in case you’ve been trying to figure out how to do it.

The slides and example project code can be found here: Presentation Material

I had several questions during the presentation about where the data goes if you create tables in your application without connecting to a database. I looked into it further and found that it is in a sql express mdf file. The file is Bin\Data\ApplicationDatabase.mdf under your project folder. In LightSwitch, they call this the intrinsic database.

Posted in .NET, Event, LightSwitch | Leave a Comment »

Handy Unit Class Extension

Posted by MichaelBird on October 10, 2011

I was working on a web site the other day and needed to do some column manipulation on a table and found it odd that the System.Web namespace didn’t seem to give me much help for what I wanted to do. I ended up writing these small extension methods to help and thought they might be useful to someone else so here they are.

Read the rest of this entry »

Posted in .NET, ASP.NET | Tagged: , | Leave a Comment »

//BUILD/–Day of arrival

Posted by johnhidey on September 13, 2011

The day for me started about 4am.  Getting up at 4am to begin getting ready for my voyage to Anaheim to partake in the //BUILD/ conference.  Was actually pretty easy to get up that early being that I was really stoked to be doing.  Why stoked you ask, I ask the same thing since I purchased the conference along with everyone else without knowing an agenda, session list or even speakers. 

Read the rest of this entry »

Posted in .NET, HTML5, Markup, Silverlight, Windows 8, WPF, XAML | Leave a Comment »

C# Productivity

Posted by MichaelBird on April 5, 2011

Is it just me, or does it seem that C# and .NET, even after all these years, is still missing much of the productivity power of C++? I’m not talking about pointers or other unsafe code, just some productivity enhancements that seem like they are well within the purview of what C# is capable of. I’ve boiled it down to the two top things I would like to see added.

Read the rest of this entry »

Posted in .NET, C#, Programming | Tagged: | 4 Comments »

 
Follow

Get every new post delivered to your Inbox.