WPF Printing Performance and PDF

Printing in WPF has been a big thorn for me over the last few years, it just feels like you have to learn everything the hard way through extensive trial and error… and better write it down or you’ll be re-learning it again later.

One hard lesson for me has been printing a lot of vector based graphics in a particular application. Early on the data would wind up rasterizing for various reasons and creating massive rasterized data pages, which when sent to the printer would cloe things way down and create ugly printouts (someone has a case of the jaggies). Even PDF printing suffered from this, so instead of a nice lightweight PDF with some lines and text drawn on the page, it would barf out a giant file with every page represented in ugly rasterized pixels.

The first thing to make sure of if you fighting this is to make sure any element you are printing doesnt use any form of transparency (opacity). I had an element sneak back into my app recently which had text printed with a background color with opacity set to 35%. Not sure if this is the fault of WPF, PDF, or the particular Acrobat software I’m using, but even a single element with this opacity setting on a page will cause the entire page to be rasterized… not just the element itself, as one might expect.

The other side effect of this rasterization is slow printing. Even printing to a local PDF file can take over a minute PER PAGE on my high-end laptop. So if your print times seem astronomical, you’re likely running into this same issue.

I recall a number of other weird issues encountered in the past, I’ll copy them here if I can find them again. But for now, note to self: NO opacity variations.



I found out today another item that can cause rasterization – if you have a canvas with clipping turned on, and items hanging out beyond the edge of the canvas. Doesnt seem to always do it though, it might only occur if the items clipped by the canvas extend beyond the edge of the printed page. I experienced this again today and now I’ll have to figure out yet another workaround for it.

100 grams to lbs

100 grams is 0.22046 pounds. You’re welcome!


Just curious if I make this post, what would big daddy G do with this phrase? That being 100 grams to lbs, specifically.

If you found this post by searching for this phrase, I’d be curious to know what you were trying to convert for.. add a comment below (keep it family safe!) I personally did a search for it while comparing some mountain bike parts and couldnt recall just how much weight we’re talking about when shaving some ounces off here an there. And yes, I understand the irony of my trying to save a few ggrams of bike weight when I myself could stand to drop a few… kilograms?… myself 😉


screwturn wiki on .net 4 and extensionless urls

Screwturn wiki is (pretty) good software, but unfortunately seems to be mired in some of the older asp.net technologies. There are a LOT of moving parts under the hood, so trying to modernize it might be a challenge. I wanted to update it enough to run on .Net 4.0, and specifically to enable extensionless URL’s.. why have that silly ashx hanging off the end of each url nowdays? This has proven to be a bit of challenge due to the aformentioned many moving parts, so following is my current progress on this attempt-  the target website for this will remain nameless for now, to protect it from your prying eyes 😉


1. download the source version of screwturn

2. open it is visual studio 2010 and upgrade the project to .net 4.

3. when you try to compile, there’s a number of tests that fail and prevent compilation. they appear easy to fix, theyre all just parameter names that have changed, so update them to match.

4. compile and run the app. create a couple pages. notice when you go to the pages, they end in .ashx.

5. open the Core project, the Settings.cs file, and find the PageExtension propterty. edit it to return “” instead “.ashx”

6. open Core\ReverseFormatter.cs edit the Regex named PageLinkRegex to remove the \.ashx portion (verify this is needed)

7. open core\urltools.cs, RouteCurrentRequest() – change the line-

  if(ext != “ashx” && ext != “aspx”) return;

so that “ashx” is now “”

in the same method, find if(ext.Equals(“ashx”)) and change it to “”


It makes sense to remove the /MainPage url as well, since it is the same as / .

8. remove the static link to MainPage in /public/sidebar.cs, or just edit it out via the admin panel, find and edit the sidebar. any other places it shows up in static content can be edited this way.

9. dynamic links generated for the mainpage need to be fixed too. (I have not completed this yet, will update here when done. Add a comment if you find a quick way to do this)


x. You might need to backup the web.config and rename the web.release.config file to web.config instead, seems the 4.0 upgrade mixes things up a bit.

x. To enable .net 4.0, in the web.config, make sure the compilation tag has targetframework=”4.0″ and the provdierOption name=”CompilerVersion” = v4.0

x. Comment out (or delete) all the sectiongroups at the top of the web.config… these don’t like .net 4 for some reason.

x. enable “unsafe” posts of html – editing pages will blog up on save currently, due to XSS restrictions in 4.0.  Add requestValidationMode=”2.0″ to the httpRuntime tag in web.config


Many places in the web.config still reference 3.5 control versions, so I may try to upgrade some of these in the future, but things are running for now and that’s good enough.



Please add any feedback you have regarding this, thanks.  


Next episode: how to convert screwturn to mvc 3 and razor. I kid, i kid!



MVC Razor Semicolon showing up

If you get a pesky semicolon in your page when using MVC 3 with Razor, you probably did something like this:




See the semicolon? It’s fine to put it there, but of course Razor doesn’t see it as .net code, just more inline html.

This one is obvious once you notice it, but it took me a bit to figure out where it was coming from the first time i encountered it.


FireFox 4 is Slow

I don’t use firefox all that much but I do use it when i need to use one of the many plugins avaialble for it (my biggest beef with ie- make it pluggable already).

Popped it up the other day and what’s this, version 4 is out. Sounds great, let’s upgrade.

I have a really powerful laptop, and it is just amazing to me how incredibly slow firefox has become. Opening a new instance takes.. I don’t know, I usually forget I was opening it and go on to something else, only to be surprised when it suddenly appears.

What happened to the small nimble browswer that was kicking ms’s bootay years ago?

Can we go back to FF 1 or 2?


Undelete popular tabs in IE9

Using IE9, I like the “popular sites” list of links that shows up when a new tab is opened. Of course google is one of the first ones.. except I accidently removed it a while back, and now can’t figure out how to “un-remove” it.

Some searching online found out there is no real way to do this (wooooops!) but deleteing the keys in the registry seems to fix it: wipe anything under this path-

HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\TabbedBrowsing\NewTabPage\Exclude


bam, worked for me.

ReactOS 3.13 released

The ReactOS project, which I follow pretty closely, has finally released version 3.13 as of today. It is claimed to support running Mono now, so I’m really curious to see how well it does. Eventually having an open source Windows replacement, which can also run .Net code, could be a really good thing. I will be running it inside a QEMU emulator (full qemu + reactos image can be downloaded as one) and see how it works.

More info available via www.reactos.org


EF Code-First first look

Modifying a project built using EF Code First, and so far I like what I see. The whole POCO concept seems to work well, especially for a smaller simpler project. I learned a lot about it from this link: http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx

I also like how you can eaisly define test data to use in place of the database, so that during testing and debugging you don’t have to mess around with a “real” database- you can launch from the same start data every time, or create custom test cases etc.

Phalanger PHP Compiler – new version released

I’ve been watching this project for years, and just noticed a new major release was made last month. Phalanger is a Php to .Net (CLR) conversion framework. Effectively lets you run PHP apps as .Net, inside the CLR, on an IIS server. As it matures, this should be a great option for running php code on microsoft servers, as well as a good method to assist in porting php apps over to a “real” .net language.

see http://phalanger.codeplex.com/


Groupon Clone

Doing some research into ways to get a “daily deals” website online, much like the famous groupon.com site. There are quite a few out there now, and a number of options if you want to create your own. It seems the lifecycle is still early enough that many providers are charging $1k+ for a reasonable usability, but often limited to a single instance etc.

Looking into open source options, I’ve only so far found a chinese one called “open groupon” (I’m sure the name will need to be changed ;). Testing it out now, and looks ok but a little rough in some areas. It’ll be interesting to see what comes out next around this type of app, I’m sure the OS community is hammering away at this and there will be 20 different clones out by year’s end.

Add any good options you’ve found in the comments below, I’d like any feedback I can on this.

DDOS at Codero

I’ve been hosting with Codero for a while now, and had great service. Until this morning, when a DDoS attack effectively blocked one of their data centers from the rest of the world. So, all my projects on my primary server with them were offline for… about 6 hours or so.

Last year a DDOS at my registrar, which I use for DNS, blocked access to all my sites as well.

Time to start looking into redundency options. Fun.

Installshield sucks. Thanks Microsoft.

Adding an installation project to my solution in vs2010, i notice an “installshield le” option for installation projects. ok, whats this? I do some research and apparently microsoft is abandoning their own installation projects, and will be moving everyone to use this “light edition” of installshield in the future.

I’ve used installshield in the past. Surely it’s not as bad as it used to be.

I add the new installshield le project, and am instructed I need to download it. this requires registration with “flexera”, who apparently now owns installshield, or whatever. create an account, give all my personal info. get a registration key in the mail. download installer and run it.

now, go back to visual studio, try to add the installer project again. Please enter serial number first problem – error, cant authorize. what? go through registration process again and try with new key. finally works.

now i finally have an install project in my solution. go through the setup steps using the weird navigation system (yes, it’s weird).

ok, we’re ready! build. what? errors?

get some kind of crazy “cant set codepage for lcid 1033”, as well as some other nonsense. click the error message, and am take to the “flexera” site, which lists a bunch of matches on this error message. no real solutions found, the matches listed date back to 2009 and 2007, so definitely not something new. I still haven’t resolved this, but do not look forward to dealing with it.

So, not much has changed. Same old cryptic BS dealing with installshield, and apparently microsoft has given up trying to provide something better. So this is what you get with a mult-thousand dollar license of visual studio 2010. Thanks Microsoft! for the opportunity to remember something i hate worse than crystal report.

How does ChatRoulette work – the RTMFP protocol

Disclaimer – I’ve never used chatlroulette and have no intention of starting. But I was curious about the technology under it. Has Flash finally enabled some way of direct peer-to-peer streaming of data? And it appears that this is the case.

The RTMFP protocol is a version of Adobe’s earlier streaming protocol called RTMP. RTMFP uses UDP datagrams, which are not guaranteed to be delivered, as TCP ones are. But this is fine for live streaming apps, since data from a second ago is already outdated.. so why bother trying to guaratee delivery and all that overhead.

But the real magic with using udp is – firewall transparency. Since firewalls usually allow most outbound traffic through without much harassment (at least for home users), inbound is another story. NAT enabled routers are not even able to allow inbound connections without some finagling, since it has no idea which local machine to send the connection request to.

UDP doesnt have all the extra error checking headers etc, so it’s pretty trivial to trick it into “faking” a connection across firewalls, but having computers at both ends of the connection intiate a connection about the same time. A third-party middle server is normally used to coordinate the setup and timing of the attempt to connect in this manner. The router will see outbound packets to an ip and port, and then see packets coming back from that same ip and port… and will assume they are responses to the request, thus forwarding them on to the internal computer. This can be done from both ends at the same time, thus both ends can be behind firewalls etc. You’ll definitely get a few bad packets bouncing off the firewall until both ends get their business straigntened out, but after this everything will usually just work.

TCP, on the other hand, has headers which keep track of a bunch of things, like sequence etc., so when these headers are not correct the router will discard them as being bad.

I did some research some years ago to see if it would be possible to do this same trick of “faking” a connection (called “connection splicing”) with TCP as well. The only way it *might* be possible is to have the third party server doing some illegal packet manipulation to fool the firewalls. I never decided if was truly impossible or not, but at minimum it would be tricky. I did manage to find one other discussion about this, appears to still be around: http://www.advogato.org/article/217.html

Back on topic – the reason I dug back into this today was I was curious if Silverlight has any provisions for supporting something like the RTMFP protocol, and thus allow direct p2p connections. From what I can tell, it does not currently support anything like this.

Update- apparently chatroulette went offline recently and displayed the message “chatroulette rtmp connection failed”, so there was a spike in search traffic to this page for that phrase. Short answer: yes, this means chatroulette is offline, just wait for it to come back!

Async javascript loading for reduced page load time

Many of my websites have the standard addins for integrating with the rest of the world- the addthis.com gadget, the facebook like button, etc. A number of them also use google maps. Each of these includes a javascript file from a remote source, which normally will slow down the load time of your pages. Google fixed their analytics scripts some time back to allow loading them asynchronously, so the page is not paused while waiting for it to load. Strangely, a lot of scripts don’t yet support this, and I’m seeing in google webmaster tools that the calculated load time of my pages has suffered as a result. What should be a tiny page that loads in literally milliseconds, is now a 3 to 5 second loading affair.

After some research, it looks like this is not a simple task to undertake. There are a number of ways to load a script async, but each has a set of drawbacks. This seems to be a great overview of the various methods currently avaialble: http://friendlybit.com/js/lazy-loading-asyncronous-javascript/ . I’ve seen that html5 also includes direct support for allowsing async loading of scripts, so maybe eventually this will no longer be a problem, but for now it’s a pretty big headache.

As time permits (or, when I get the chance to delegate it), I’m planning to try the solution listed on that linked page and see if addthis, facebook, google maps, perhaps even adsense, and even a wufoo form can perhaps be loaded in this async manner. I’ll report the progress here.