Last night, just before going to bed, I ran into an interesting problem. I had this code (I did not modify it in any way, it comes from the book Applications = Code + Markup):
//----------------------------------------------------
// PrintaBunchaButtons.cs (c) 2006 by Charles Petzold
//----------------------------------------------------
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace Petzold.PrintaBunchaButtons
{
public class PrintaBunchaButtons : Window
{
[STAThread]
public static void Main()
{
Application app = new Application();
app.Run(new PrintaBunchaButtons());
}
public PrintaBunchaButtons()
{
Title = "Print a Bunch of Buttons";
SizeToContent = SizeToContent.WidthAndHeight;
ResizeMode = ResizeMode.CanMinimize;
// Create the 'Print' button.
Button button = new Button();
button.FontSize = 24;
button.Content = "Print ...";
button.Padding = new Thickness(12);
button.Margin = new Thickness(96);
button.Click += PrintOnClick;
Content = button;
}
void PrintOnClick(object sender, RoutedEventArgs args)
{
PrintDialog printDialog = new PrintDialog();
if ((bool)printDialog.ShowDialog().GetValueOrDefault())
{
// Create the Grid panel.
Grid grid = new Grid();
// Define five auto-sized rows and columns.
for (int i = 0; i < 5; i++)
{
ColumnDefinition columnDefinition = new ColumnDefinition();
columnDefinition.Width = GridLength.Auto;
grid.ColumnDefinitions.Add(columnDefinition);
RowDefinition rowDefinition = new RowDefinition();
rowDefinition.Height = GridLength.Auto;
grid.RowDefinitions.Add(rowDefinition);
}
// Give the Grid a gradient brush.
grid.Background =
new LinearGradientBrush(Colors.Gray, Colors.White,
new Point(0, 0), new Point(1, 1));
// Every program needs a little randomness.
Random random = new Random();
// Fill the Grid with 25 buttons.
for (int i = 0; i < 25; i++)
{
Button button = new Button();
button.FontSize = 12 + random.Next(8);
button.Content = "Button No. " + (i + 1);
button.HorizontalAlignment = HorizontalAlignment.Center;
button.VerticalAlignment = VerticalAlignment.Center;
button.Margin = new Thickness(6);
grid.Children.Add(button);
Grid.SetRow(button, i % 5);
Grid.SetColumn(button, i / 5);
}
// Size the Grid.
grid.Measure(new Size(Double.PositiveInfinity,
Double.PositiveInfinity));
Size gridSize = grid.DesiredSize;
// Determine the point for centering the Grid on the page.
Point gridPosition =
new Point((printDialog.PrintableAreaWidth - gridSize.Width) / 2,
(printDialog.PrintableAreaHeight - gridSize.Height) / 2);
// Layout is complete.
grid.Arrange(new Rect(gridPosition, gridSize));
// Now print it.
printDialog.PrintVisual(grid, Title);
}
}
}
}The problem was this: I ran the program, clicked Print and only a blank page came out. So I went to my mom’s PC running Windows Vista with VC# 2010 and ran the program there. This time the page did not come out of the printer blank, but printed exactly the way it should be (on the left is the output from my mom’s PC, on the right from mine):
That told me something was wrong with my PC, or rather my Windows. At first I wanted to install Visual Studio Express 2013 Preview for Windows Desktop (hereafter “Preview“), but shortly after the installation I decided that I would rather try it on a clean Windows install. Because 1) the internet was very slow here that night, and 2) on my account the installation cleanup of the Preview took forever. I had one 120GB partition with some data on it, so I moved the data elsewhere and split that partition into 3x 40GB.
I had already lost an entire partition once, maybe because it held a huge number of files, so I decided to keep source code on separate partitions. I copied the contents of the W7 installation disc to my PC to check whether it was scratched – it was not. So I booted from the DVD, but I did not install W7 after all, because I had a dynamic volume. I had split the disk using the tool built into Windows 8, which only offered me Simple volume. I am not very knowledgeable about disks and I do not know what a dynamic volume is.
So I gave up and wanted to reinstall .NET FW 4.5. But that did not work either, because .NET 4.5 is already part of Windows 8. After finding that out, I turned the PC off and went to bed. In the morning it occurred to me that it might not be so slow if I created a new account. So I did, and indeed, the installation took only about 45 minutes. On the old account it might have taken as long as 2h. I opened the project (I was pleased that VS2012 and 2013 share the same project and solution formats, since it did not require any conversion), compiled it, ran it and clicked Print.
But the printer did not even budge. I could not print anything, not even from Notepad, yet strangely the test page printed just fine. I logged out of this account and went back to the original one. There I tried printing in the Preview, but it was the same as in version 2012 – only a blank page came through. I really do not know what to do about it anymore. The previous 2 printing programs in this book worked for me, which makes the whole thing even more mysterious.
I had been really looking forward to this chapter on printing, because I had never printed anything programmatically before, and now this :/.
PS: I just tried another program and it did not turn out great on my PC either, compare for yourself (at the top is the bad output from my PC, at the bottom the correct one from my mom’s):
I think it is no longer worth trying to fix it. I will just run the programs and print what they produce (and I have already done that, everything printed wrong), but I would like to take another look once Windows 8.1 comes out, if there are also some changes in .NET – ideally a new version (even a minor one is fine). The title is a bit misleading, because all printable elements are of type Visual – including the simple ellipses, which I was still able to print.

