Tilt Shift Effect with radial mask

Tilt Shift Effect with the Sketch app

The tilt shift effect adds a nice ‘miniature’ look to photos. There’s a good explanation of how it works over at Wikipedia. We’ll skip the technical details and simply move on to explain how you can create a fake tilt shift effect using the great drawing app for Mac called Sketch. I’ve recently switched from Adobe Fireworks to using Sketch only, and I was intrigued to see if I could create the fake tilt shift effect using Sketch also. Of course you can. Here’s how:

Get a nice photo to work with. I randomly selected Taxco de Alarcón, Mexico taken by Pedro Lastra.

1 Start Sketch, and drag and drop the photo in Sketch. You can give the photo a better name by renaming it in the Layer List on the left.

Tilt Shift with Sketch, step 1

Tilt Shift with Sketch, step 1

2 Select the photo in the Layer List (left hand pane), and hit Cmd+D (duplicate) to create a copy of it. I renamed the duplicate for clarification.

Tilt Shift with Sketch, step 2

Tilt Shift with Sketch, step 2

3 Apply a Gaussian blur to the top image, set it to 5px. This is done by selecting the image in the Layer List (left panel), then ticking the checkbox labelled ‘Gaussian Blur’ in the Inspector on the right.

4 Create a rectangle that has the same size and position as the original photo. Make sure it is on top of the two photos. Remove the border color (untick the checkbox), and change the fill color to be a radial gradient. Modify the location and size of the radial gradient (circle) so its center is in the center of the photo, and its outer edge roughly matches the size of the rectangle. The start (center) color should be white with an opacity set to 0, and the end (outer) color should be white with an opacity set to 100. You can choose any color you want, as long as both start and end color is the same.

Tilt Shift with Sketch, step 4

Tilt Shift with Sketch, step 4

5 Select both the rectangle and the blurred photo in the Layer List on the left and click on the menu “Layer” -> “Mask with Selected Shape”.

Tilt Shift with Sketch, step 5

Tilt Shift with Sketch, step 5

6 Voilá! You’re done. That was easy wasn’t it? Now for some pro tips: thanks to Sketch you can select the mask by expanding the newly created group in the Layer List and clicking on the layer named “mask”. Now you can change the gradient from radial to linear, to achieve a different tilt shift effect. Really smooth. Compare the two images below and see for yourself.

Tilt Shift Effect with radial mask

Tilt Shift Effect with radial mask (click to see full size image)

Tilt Shift Effect with linear mask

Tilt Shift Effect with linear mask (click to see full size image)

Azure AD

Get group members with Azure AD Graph Client Library

When using the latest (2.1.0) version of the Azure AD Graph Client Library, it is not trivial to figure out how to get a group’s members. Even with the code samples there is still some trickery needed. This post shows you how. It assumes you’ve got the authentication part figured out already, of course.

A few comments are in place.

  1. The creation of the ActiveDirectoryClient on line 2 is ‘hidden’ in the helper method CreateClient(). Not relevant here.
  2. We retrieve a matching user by doing comparison on the Email field. Change this to anything you like.
  3. We use the very convenient ExecuteSingleAsync() since we know we only want one, and don’t care about paging results.
  4. IMPORTANT we cast the user (on line 6) to an IUserFetcher, this enables us to retrieve (paged) results of all group members.
  5. On line 9 we use a simple LINQ filter (OfType()) to get only users. Group members can be of type groups and principals too, but we don’t care about them.

If you need other samples, take a look at this page on Github, it contains a lot of relevant sample code.


Multithreading in CRM

For anyone doing batch processing in Microsoft Dynamics CRM; you’ve most likely experienced how painfully slow it is compared to custom-built solutions. At least that’s how I approached it when I first started working with CRM 2011. Always being performance centric I was curious to see exactly how fast I could get it to run. Said and done, I put together a few samples.

But first a few words…

There are several issues one needs to be aware of. For one, every object written to the database needs to be serialized to SOAP and then deserialized, before being stored in the database. That means the following steps has to happen under the hood:

  1. Establish http connection
  2. Serialize object to SOAP
  3. Send the object(s) over the wire
  4. Deserialize object to SOAP
  5. Establish database connection
  6. Store data in database.

As you can see, there are many steps, which means many opportunities for optimization.

First attempt

This simply uses the ExecuteMultipleRequest, which is a built-in feature. You simply write many objects to the database at once. Done deal, here’s some sample code:

Second attempt

According to the CRM documentation you can have multiple threads running at once. I figured I’d give it a try. But, in order to not make the http connection back to the CRM server act as a bottle neck I figured I should create a unique context per thread. That way I can parallelize all the way back to the CRM server. Note the use of the ServiceConfigurationInstanceMode property on the context. Here’s the sample code:

Some code pointers:

  • Line 10: create a bunch of threads in a for-loop
  • Line 12: create a CRM context per thread
  • Line 18: create an ExecuteMultipleRequest for use by one thread
  • Line 28: Add items to the ExecuteMultipleRequest
  • Line 34: Insert all items in the ExecuteMultipleRequest
  • Line 38: Add the task to a list
  • Line 40: wait for all tasks to finish.

The sample code above uses:

  • The ExecuteMultipleRequest inside each thread loop
  • Multiple threads
  • One CRM context per thread

By utilizing all three concepts above I have managed to do 100 000 inserts in just under 7 minutes.

.NetCodeReactive Extensions

Using Rx in ASP.NET

I recently started an ASP.NET MVC 5 project, and having used Reactive Extensions (Rx) for five years in other projects I was really keen on using Rx in my ASP.NET code also. The question was how? Well, after some tinkering I figured it all out.

Asynchronous action methods

The first thing we’ll take advantage of is the fact that action methods in controllers can be asynchronous. Instead of declaring your action method like this:

You type:

That alone frees up the web server by asynchronously waiting for the response to ‘come back’. This means it can serve other requests in the meantime. The next step is of course converting an observable Rx chain like this:

into an ActionResult . Well, to do that we need an extension method. Basically, we want to convert an IObservable<T> to a  Task<T> . The reason is of course to match the action method’s return type.

Error handling

Also, we need to incorporate error handling, so that any exceptions are handled and the appropriate return value is used. In general I always prefer to have generic (“catch-all”) error handling in place, and then, if needed, we can customize it for specific cases. That way no errors slip through.

How to use it

I decided to use optional parameters for the error handling. Here’s what the default use of my extension method looks like:

You could also write:

And if you want to do specific error handling, you could do:

The extension method

As you can see from the code above you can specify an optional scheduler, as well as a custom timeout period.

Source code

The full source code is available on Github.