Mike Taulty's Blog
Bits and Bytes from Microsoft UK
Blend Bits 21–Importing from Photoshop & Illustrator…

Blogs

Mike Taulty's Blog

Elsewhere

Archives

If I had a penny for every time I heard someone from Microsoft say “I’m not a designer” when working with Blend then I’d be a rich man. Naturally, Microsoft does have some very fine designers but, unfortunately, it’s not me so I also end up having to say the dreaded “I am not a designer” phrase.

I say unfortunately as I’d quite like to have been a designer but it seemed that in my family 2/5 people got the artistic gene and the other 3 of us are best kept away from drawing materials Smile

So, what to do?  I think it was rumoured to be Picasso who said that “Good artists borrow, great artists steal” and so in my case stealing is really the only option even though it still won’t make an artist out of me.

That’s where Blend’s “import” capability comes in handy. Today, I wanted to make a clock application in Silverlight.

Now, I can’t draw the face of a clock to save my life. However, it’s not important because other people on the internet can and they’ve already done it so I went to my favourite search engine and typed in;

“clock face psd”

and I found this page and I downloaded the PSD file. Then I made a new Silverlight project in Blend and imported the PSD file;

image_thumb

and I picked up the .PSD file from the file dialog and then got met with;

image_thumb2

Now…I could use the Compatibility Image feature down at the bottom right of the screen and that would turn all the Photoshop layers into a single image and I’m then done.

However, when I’ve done this import I really want to be left with the clock hands as moveable objects that I can animate around the clock face in order to show the time Smile So, I need to poke into the layers a little more.

I unchecked a bunch of bits that I didn’t want like the background and so on;

image_thumb4

and then I’m happy to flattten quite a few of the layers so I de-selected those and ended up with 2 groups – one for the clock hands and the other for the face, background, dial etc which Blend had already decided to merge into one layer for me;

image_thumb6

Blend does its import and I end up with;

image_thumb8

So in this case I’ve ended up with 2 PNG files and 4 vector Paths inserted into a Canvas for me. To make life easy for myself I grouped this set into a ViewBox so that I can scale things up ( naturally, the PNGs won’t scale quite like the vectors here ).

image_thumb10

taking it back to just having 1 clock-face, I got rid of one hand that I felt was one too many Smile and changed the colour on the most likely candidate for a “seconds hand”;

image_thumb12

and then named a few items in the objects tree;

image_thumb14

then I took each hand and moved its center of rotation to the mid-point of the clock;

image_thumb16

and tried to rotate the hands to all point at 12;

image_thumb17

this causes Blend to put a RenderTransform onto each element ( a CompositeTransform ) and I chose to put the angles of those initial rotations into local resources just so I could quickly grab them from code;

image_thumb19

this is just me attempting to be very “cheap and cheerful”.

At this point, all seemed good and I figured that I need a little code to make these hands appear in their correct positions and that was bashed together very quickly and with not much “finesse”;

using System;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;

namespace SilverlightApplication11
{
	public partial class MainPage : UserControl
	{
		public MainPage()
		{
			// Required to initialize variables
			InitializeComponent();
			
			this.Loaded += (s,e) =>
			{
				DispatcherTimer timer = new DispatcherTimer();
				timer.Interval = new TimeSpan(0, 0, 0, 1);
				timer.Tick += (sender,args) =>
				{
          DateTime foo = DateTime.Now;

					OnTimeChanged(DateTime.Now.ToLocalTime());
				};
				timer.Start();
			};
		}	
		void OnTimeChanged(DateTime now)
		{
      double minuteHandAngle = (now.Minute / 60.0d) * 360.0d;

      ((CompositeTransform)this.MinuteHand.RenderTransform).Rotation =
        (double)this.Resources["initialMinuteHandAngle"] + minuteHandAngle;

      double secondHandAngle = (now.Second / 60.0d) * 360.0d;

      ((CompositeTransform)this.SecondHand.RenderTransform).Rotation =
        (double)this.Resources["initialSecondHandAngle"] + secondHandAngle;

      double hourHandAngle = ((now.Hour % 12.0d) / 12.0d) * 360.0d;

      ((CompositeTransform)this.HourHand.RenderTransform).Rotation =
        (double)this.Resources["initialHourHandAngle"] +
        hourHandAngle +
        (minuteHandAngle / 12.0d);
		}
	}
}

but, hopefully, it more or less does the right thing and the point of the post wasn’t really about code – it was about how I can import from PhotoShop/Illustrator and then make something that’s programmable pretty quickly.

And here’s the clock;

Get Microsoft Silverlight

ticking away…


Posted Thu, Feb 3 2011 4:49 PM by mtaulty

Comments

Ray wrote re: Blend Bits 21–Importing from Photoshop & Illustrator…
on Fri, Feb 4 2011 1:58 AM

Cool~

Jean Ventigrate wrote re: Blend Bits 21–Importing from Photoshop & Illustrator…
on Fri, Feb 4 2011 3:06 PM

I admit that having those files in vectors is nice, and indeed easier to scale.

BUT a serious downside that I noticed that my files were enormously big compared to the original file.

A xaml file going from 20 KB to 1 or even 2MB+ is not acceptable.

Certainly not in a web environment.

That's why I never do that.

mtaulty wrote re: Blend Bits 21–Importing from Photoshop & Illustrator…
on Fri, Feb 4 2011 3:56 PM

Jean,

Take your point - in my particular case I imported some simple paths and a flattened PNG and I ended up with a total size of a 350K PNG background image ( relatively large image at 834x834 which I could resize to make smaller ) and then a XAML file which was about 4K.

But your general point stands - got to be "careful" where/when you use vectors.

Mike.

Bigsby wrote re: Blend Bits 21–Importing from Photoshop & Illustrator…
on Mon, Feb 7 2011 10:51 AM

Nice going, Mike.

Kind of nostalgic looking at this and remembering cleaning houndereds of Canvas from the first XAML export files in AI.

BTW, Jean, I remember that the first I did was an original 300KB XAML that turned out to be only 900B of really necessary information.

racing games wrote re: Blend Bits 21–Importing from Photoshop & Illustrator…
on Fri, Feb 11 2011 2:48 AM

Very nice article to share your experience in using photo-shop and Illustrator.The tips you given will help me a lot.

Aaron wrote re: Blend Bits 21–Importing from Photoshop & Illustrator…
on Thu, Feb 24 2011 2:29 AM

Brilliant, thanks for sharing!

Aaron wrote re: Blend Bits 21–Importing from Photoshop & Illustrator…
on Thu, Feb 24 2011 3:34 AM

Any thoughts on how to apply a subtle animation to the second hand as it ticks?

mtaulty wrote re: Blend Bits 21–Importing from Photoshop & Illustrator…
on Thu, Feb 24 2011 9:00 AM

Aaron,

Sure, in the timer tick function instead of just changing the rotation of the second hand you need a Storyboard which moves it on By a certain amount and then just run that Storyboard. You need to be a bit careful that it doesn't get out of sync.

I made a screencast of this example for the www.activetuts.com site which they haven't yet published but in that version I didn't use a timer at all. I just set the hands to the correct initial position and then used storyboards to animate them forever.

Mike.

Aaron wrote re: Blend Bits 21–Importing from Photoshop & Illustrator…
on Thu, Feb 24 2011 2:03 PM

Thanks, Mike.  I'll keep a lookout for that content.