Saturday, April 23, 2011

Full Screen Image Viewer using WPF

I recently created a Full screen image viewer which can be used to see image in full screen, view them in full size and rotate while in full screen mode. This works on Windows7, I have not tested on other OSs.

It can be downloaded from here.
http://www.4shared.com/file/grwH4BH1/FullShot.html

To use this, just save it at any location at you computer. Go to any image you want to see in full screen and set this as default program (as shown below), Now when ever you double click on an image file, It will be opened using this program in full screen.




















Here are Shortcuts to use it:
Esc : Exit
Right Arrow : Next image in same folder
Left Arrow : Previuos  image in same folder
Up Arrow : Rotate Counter-clock wise
Down Arrow : Rotate Clock wise
Z : View image in its full size
X: Fit image back to screen width/height

Please, let me know what you think of it. Cheers!

Passing arguments to WPF application

Recentely I was working on a WPF application that can be used to open a file when the file is double clicked , so I had to pass the file name to the application. Here is how I did it by overridding OnStartup method in App.xaml.cs.

public partial class App : Application
{
   protected override void OnStartup(StartupEventArgs e)
   {
       if (e.Args != null && e.Args.Length > 0)
      {
           this.Properties["InputArgName"] = e.Args[0];
       }
       base.OnStartup(e);
   }
}
 
This can be access anywhere in the application using below code:
if (Application.Current.Properties["InputArgName"] != null)
{
   string fileName = Application.Current.Properties["InputArgName"].ToString();
}

Thursday, April 7, 2011

Loading different versions of same .Net assembly

In my previous post I described how to load assemblies from a sub folder. You may run into a scenario when assemblies in root folder are referring a particular version of assembly while assemblies in subfolder are referring to a different version of same assembly. Assume this assembly (Lets call it SomeAssembly.dll) is placed in root folder and version no of this is 1.0, now if assemblies in subfolder need to refer 2.0 version of this assembly. So put 2.0 version of this assembly in the subfolder and instruct assembly resolve mechanism to load the assemlies from subfolders if it failed to load automatically. Below code shows how to do it:


static void Main(string[] args)
{
   AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
}

private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
   string locFormat = Environment.CurrentDirectory + "\\{0}\\" + args.Name.Split(',')[0] + ".dll";
   foreach (string subPath in AppDomain.CurrentDomain.RelativeSearchPath.Split(';'))
  {
     string filePath = string.Format(locFormat, subPath);
     if (File.Exists(filePath) && AssemblyName.GetAssemblyName(filePath).FullName == args.Name)
     {
        return Assembly.LoadFile(filePath);
     }
   }
 return null; // Failed to load
}

Note that this also applicable when using binding redirect is not a solution and you want different components with in you application to use a different version of a private assembly, you can put each of those different version in to seperate subfolders(as assembly file name is same only one assembly can be put in a single folder). Use above code and add these subfolders into probing path as described here.

Type based templating

            <DataTemplate DataType="{x:Type vm:SampleViewModel}">                 <DataTemplate.Resources>       ...