My C# Implementation of Basic Linear Algebra Concepts

Overview

Photo Credit: Wikipedia

Today, I will be sharing with you my C# implementation of the basic linear algebra concepts. This code has been posted to GitHub under a MIT license, so feel free to modify and deal with code without any restrictions or limitations (no guarantees of any kind too.) And please let me know your feedback, comments, suggestions, and corrections.

Continue reading

Matrix Multiplication in C#; Applying Transformations to Images

Overview

Today I will show you my implementation of matrix multiplication C# and how to use it to apply basic transformations to images like rotation, stretching, flipping, and modifying color density.

Please note that this is not an image processing class. Rather, this article demonstrates in C# three of the core linear algebra concepts, matrix multiplication, dot product, and transformation matrices.

Continue reading

Accessing Protrack API (an Object-Oriented Approach)

Overview

Protrack is one of the well-known web-based GPS tracking software and today we will learn how to access its API using C# and .NET Framework. We will create a little wrapper around the API and use it to track GPS devices in a target account. This lesson will show you some of object-oriented concepts in action.

Continue reading

Using Video Markers in Silverlight

هذه المقالة متوفرة أيضا باللغة العربية، اقرأها هنا.


Definition

Markers are text annotations embedded at certain points in a media file. They help you in several cases. For example, you might use markers to set captions or subtitles for a video file, or maybe you use it to identify particular points in a media file so you can play the media starting at particular points.

 

Marker Support

Silverlight has native support for video markers, you can subscribe to the MarkerReached event of MediaElement control to get notified whenever you reach a marker. However, in order to use video markers you need to encode them in the video file. The best application that you can use to encode markers in a video file for Silverlight is Microsoft Expression Encoder.

 

Marker Encoding

After you have your video file added to Expression Encoder, you can use the timeline to set the markers at the desired locations. You can add a marker by moving to the position where you want to set the marker at, then right-clicking the timeline and selecting Add Marker to add a new marker.

You then go to the Markers window where you can set marker text (value) and manage existing markers.

Notice that every marker is identified on the timeline by a symbol like diamond (♦). You can change marker position by moving this symbol, and you can remove it by right-clicking it and choosing Remove (you can also use the Markers window.)

Now encode the video and prepare it to be used in Silverlight.

 

Code

After you have created your MediaElement and prepared it with the video, you can then subscribe to the MarkerReached event and handle the marker when the video reaches it. An example of handling markers is to display the marker text on the screen if, for example, it is a caption/subtitle for the video:

void theMedia_MarkerReached(object sender, TimelineMarkerRoutedEventArgs e)
{
    theTextBlock.Text = e.Marker.Text;
}

And you can also load the markers to a list box (for example) and let the user moves to the position of the markers he chooses (worth mentioning that markers are only available after MediaOpened event is raised):

void theList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (this.theList.SelectedIndex > -1)
        theMedia.Position = theMedia.Markers[theList.SelectedIndex].Time;
}

Makes sense?

What’s New in Silverlight 5? – Performance and Other Improvements

What’s New in Silverlight 5?

شاهد كورس كامل سيلفرلايت 5 مجانا هنا.

This content is based on Beta release, expect many changes in the final release.

Read What’s New in Silverlight 5 series here.

 

 

Introduction

In this article, we’ll have a brief discussion of the performance, networking, and other improvements in Silverlight 5.

 

Performance

Performance has been improved in Silverlight 5 in a number of ways:

  • Significant improvement in performance through the entire product
  • Startup performance has been improved using a multi-core background JIT compiler
  • XAML parsing time has been improved especially for UserControl and ResourceDictionary
  • Added 64-bit runtime version. Not in beta
  • Hardware accelerated rendering in Internet Explorer 9 in windowless mode

 

Networking

And networking has its share of improvements:

  • Network latency has been improved significantly
  • 90% performance improvements in ClientHttpWebRequest scenarios

 

Services

Added support for WS-Trust in WCF. (WS-Trust is a specification aimed to improve security and validation.)

 

Profiling and Testing

  • Automated UI testing
  • Improved profiling support:
    • Memory
    • CPU
    • Thread contention

 

Other

  • Support for Windows Azure
  • In-Browser HTML support
  • Many more fixes and improvements throughout the product

 

Now, check out other What’s New in Silverlight 5? articles.

What’s New in Silverlight 5? – Elevated-Trust Changes

What’s New in Silverlight 5?

شاهد كورس كامل سيلفرلايت 5 مجانا هنا.

This content is based on Beta release, expect many changes in the final release.

Read What’s New in Silverlight 5 series here.

Download Sample Code

 

Introduction

In this article, we’ll have a brief discussion of the new features in elevated-trust applications in Silverlight 5.

 

Overview

Elevated-trust applications have undergone few changes in Silverlight 5:

  • In-Browser Support
  • Multiple Windows Support
  • File System Access
  • Platform Invocation

Let’s see those changes in details.

 

In-Browser Support

Unlike previous versions, in Silverlight 5 you don’t have to run your application as OOB to get elevated-trust features, you can now run elevated-trust applications in the browser and get all the features you need.

There’re two security considerations to be taken in order to run your elevated-trust application in browser:

  1. You must enable running your application in browser via either the Client Access Policy file or the Windows Registry. Using the policy file allows your application to run in a private network. Using the Windows Registry approach allows your application to run on a single machine. In either approaches, you cannot deploy in-browser elevated-trust applications to everyone on the web.
  2. In addition, your XAP must be signed with a trusted certificate e.g. X.509 certificate) available in the Trusted Publisher Certificate store.

Without any of those requirements, you won’t be able to run your elevated-trust application in browser. But wait, there’s a catch!

In your development machine you can run elevated-trust applications in browser only on whitelisted domains. For example, by default, localhost is whitelisted, so you can easily run an elevated-trust application in the browser in localhost. If you change to another domain, ensure that you add this name to the whitelist, or you won’t be able to run the application. More comes about this later.

The last thing to keep in mind is that in the Beta version, in-browser elevated-trust applications are supported in Windows only. Other platforms are going to be available in the final version.

Client Access Policy Approach

If you want your application to be available in a private network, you need to do some changes to the client access policy file in order to allow it to run in browser as elevated-trust. The changes are as the following; just set the ElevatedPermissions property of SecuritySettings to Required:

<Deployment.OutOfBrowserSettings>
  ...
  <OutOfBrowserSettings.SecuritySettings>
    <SecuritySettings ElevatedPermissions="Required" />
  <OutOfBrowserSettings.SecuritySettings>
  ...
</Deployment.OutOfBrowserSettings>

Windows Registry Approach

The other approach you have is to use the Windows Registry to enable in-browser elevated-trust applications to run on a particular machine. To do this, go to HKEY_LOCAL_MACHINESOFTWAREMicrosoftSilverlight. In this registry key, you have several values related to elevated-trust applications:

  • AllowElevatedTrustAppsInBrowser:
    Set to TRUE (0x00000001) to enable running elevated-trust applications in browser.
  • AllowInstallOfElevatedTrustApps:
    Set to TRUE to enable installation of elevated-trust applications to the PC (i.e. OOB.)
  • AllowLaunchOfElevatedTrustApps:
    Set to TRUE to enable launching elevated-trust applications (in-browser or OOB.)

Example

To run an elevated-trust application in browser you need first to enable OOB and elevated-trust features from project properties. After that, create a Web application and link it to the Silverlight application to host it. If you didn’t do this, your Silverlight application will run as OOB. Your application is able now to run as elevated-trust in browser. To ensure this, you can check for the Application.HasElevatedPermissions property:

  theTextBlock.Text = "Is Elevated-Trust: " +
    Application.Current.HasElevatedPermissions.ToString();

Now try something more interesting. Try reading a file in the file system that only elevated-trust applications have access to:

theTextBlock.Text =
        File.ReadAllText(@"C:WindowsSystem32Driversetchosts");

As you see, the application works as expected, and has all the elevated-trust features as OOB applications.

Now let’s try changing the domain from localhost to another domain. Go to Web application project properties, switch to the Web tab, then go to Server settings and change the server from Visual Studio Development Server to Local IIS Web Server and use your machine name as the domain and save the settings. (You might be asked to confirm virtual directory creation, it’s because you have changed to another domain and used another virtual directory.)

Now try to run the application. The application won’t be able to run and you’ll get a security exception, that’s because the new domain is not whitelisted like localhost.

 

Multiple Windows Support

Multiple Windows Support or Ad-hoc Windows is a feature that enables elevated-trust OOB applications in Silverlight 5 to have windows other than the main window. The feature is available through the Window class which is now instantiable. You use the Window class to create another window and set its contents through the Content property and show it using the Visibility property.

The following code creates a Window that’s 400 pixels wide and 300 pixels long, and has just a button in the interface:

    Window wnd = new Window();
    wnd.Width = 400;
    wnd.Height = 300;
    wnd.Content = new Button { Content = "Say Hello" };
    wnd.Visibility = System.Windows.Visibility.Visible;

Notice that you can end the application by closing the main window; it doesn’t matter if you close any other window.

You’re not limited to just a button or a single object in the window contents. You can create another page (i.e. user control) and set it to be the content of the window:

    Window wnd = new Window();
    wnd.Width = 400;
    wnd.Height = 300;
    wnd.Content = new MyOtherPage();
    wnd.Visibility = System.Windows.Visibility.Visible;

And you can also create windows with custom chrome by removing the toolbar and borders and create them yourself via code:

    Window wnd = new Window();
    wnd.Width = 400;
    wnd.Height = 300;
    wnd.Content = new MyOtherPageWithCustomChrome();
    wnd.WindowStyle = WindowStyle.None;
    wnd.Visibility = System.Windows.Visibility.Visible;

Notice that if you don’t have elevated-trust features, you won’t be able to spawn other windows. And if you tried to, you’ll get a security exception.

 

File System Access

Unlike elevated-trust applications in Silverlight 4 which have access to only My Documents folder, Silverlight 5 elevated-trust applications (in-browser and out-of-browser) have full access to the file system. But keep in mind that although you have full access to the file system, you still restricted by the rights of the user running your application. If the user doesn’t have access to a specific file or folder, then you don’t have access to that file or folder.

To test this feature, try reading contents from a file in the system folder (for instance):

theTextBlock.Text =
        File.ReadAllText(@"C:WindowsSystem32Driversetchosts");

As you see, you have access to that file.

By default, if you’re running Windows Vista or Windows 7, your user wouldn’t have administrator privileges as long as he doesn’t launch Visual Studio (or your application) in elevated mode. So if you don’t have admin privileges and tries to gain the write access to the same file your application would fail, that’s because your application is restricted by user rights and the user don’t have write access to that file:

    File.AppendAllText
      (@"C:WindowsSystem32Driversetchosts",
      "nfoo");

Same results when trying to write to the system drive without elevated permissions for the user:

    File.WriteAllText(@"C:test.txt", "Foo");

 

Platform Invocation

Silverlight 4 has introduced support for automation and COM components. Silverlight 5 has added support for Platform Invocation (or P/Invoke.) Platform Invocation allows you to call native code (e.g. DLLs and Windows API) from your Silverlight application. This is a great improvement of course. It uses the same mechanism as other WinForms applications. Unfortunately, this feature is currently not available in the Beta.

 

Summary

The following changes have been happened to elevated-trust applications in Silverlight 5:

  • In-Browser Support:
    You can now run elevated-trust applications in Silverlight 5 in browser. Must be enabled via client access policy file or Windows Registry, and XAP must be signed with a trusted certificate.
  • Multiple Windows Support:
    Allows elevated-trust OOB applications to span windows other than the main window.
  • File System Access:
    Allows you to have full access to the file system with the rights of the current user.
  • Platform Invocation:
    Allows you to call native code like DLLs and Windows API from your Silverlight application. Not available in Beta.

Now, check out other What’s New in Silverlight 5? articles.

What’s New in Silverlight 5? – Media Changes

What’s New in Silverlight 5?

شاهد كورس كامل سيلفرلايت 5 مجانا هنا.

This content is based on Beta release, expect many changes in the final release.

Read What’s New in Silverlight 5 series here.

Download Sample Code

 

Introduction

In this article, we’ll have a brief discussion of the new features in media stack in Silverlight 5.

 

Overview

Silverlight 5 has undergone few changes in the media stack:

  • Better Battery Management:
    Less battery when playing media. Plus, no screensaver in the full-screen mode.
  • Remote Control Support:
    Now you can control the media being played in Silverlight 5 using a remote control. This feature is currently not available in the Beta.
  • Hardware Decoding; More GPU Support:
    For better performance, Silverlight 5 depends more on the GPU for media decoding especially for the high definition H.264 formats. In Beta, no DRM or Mac support.
  • 1080p Support:
    Combined with hardware decoding, user can now watch high definition 1080p media on a Netbook or Tablet.
  • IIS Media Services 4:
    Support for media streaming using IIS Media Services 4 has been added.

And two more features require more details:

  • Speed Control; Trick Play:
    Allows you to control playing speed, and to play the media forward or backward.
  • Low-Latency Sounds:
    Silverlight 5 has added native support for raw wave files.

 

Speed Control; Trick Play

This feature allows you to control playing speed, to speed-up or slow-down the media. The ability to change playing speed is called, Trick Play. This feature is available in the MediaElement control through a new property called PlaybackRate. This property takes a decimal value ranges from -8, -4, -2, … to …, 0.5, 1.2, 1.4, 2, 4, 8, and the default is 1. The downside of the Beta version is that it doesn’t have a pitch correction, means that when you change the rate to a value other than the default (1) you wouldn’t have any sound. And because of a bug found in the Beta version, you must set this property in code, if you set it in XAML will reset at application start to the default value.

The following are three examples of changing the playing speed of a media element:

    // Normal Speed
    media.PlaybackRate = 1.0;
    // Fast
    media.PlaybackRate = 4.0;
    // Slow
    media.PlaybackRate = -2.0;

 

Low-Latency Sounds

In the past, you had to create a wave file parser that reads file headers to determine if this is a wave file, and after recognizing a wave file, the parser reads file contents and encode them in order for your application to be able to play it. That changed completely in Silverlight 5. Today, you have native support for raw sounds data (i.e. low-latency sounds.) You can now play wave files and wave data directly in your application without the need to encode them.

This feature is available through a new API based on the XNA Framework. It uses the same classes and functions available in XNA. The main class you have is SoundEffect (Microsoft.Xna.Framework.Audio) that you can use it to play wave files and raw data. You have all the control over the media played; you can set volume, pitch, and stereo space. And you can create a more sophisticated class of SoundEffect called SoundEffectInstance that represents an instance of that sound file. This instance can be played repeatedly, and allows you to have all the control like SoundEffect and more, as we’ll see in the examples.

Finally, keep in mind that Silverlight 5 has the same limitations as XNA; it allows only for 8/16bit PCM, mono/stereo, 22.5/44.1/48khz wave files.

The following code loads the SoundEffect class with a wave file available in application resources:

    var resource = Application.GetResourceStream(
      new Uri("MediaLowLatency;component/tada.wav",
        UriKind.RelativeOrAbsolute));

    SoundEffect effect = SoundEffect.FromStream(resource.Stream);

After you have the SoundEffect at your hands, you can call the Play() method to play it. This method have two overrides, one can accept no parameters in which it plays the media normally with the default volume and no changes in the pitch or the stereo space. The other override accepts three values:

  1. volume:
    Ranges from 0.0f to 1.0f.
  2. pitch:
    Ranges from -1.0f to 1.0f. Default is 0f.
  3. pan:
    Controls the stereo space. Ranges from -1.0f to 1.0f. Default is 0f.

The following are some examples of playing sound files using SoundEffect:

    // Normal
    effect.Play();
    // Normal (default values)
    effect.Play(1.0f, 0f, 0f);
    // Faster
    effect.Play(1.0f, 1.0f, 0f);
    // Slower
    effect.Play(1.0f, -1.0f, 0f);

And finally you can use the SoundEffectInstance to create multiple instances of the same audio, or to play it repeatedly (Volume, Pitch, and Pan values are available through properties):

    SoundEffectInstance instance = effect.CreateInstance();

    instance.IsLooped = true;
    instance.Play();

 

Summary

While Silverlight 5 manages battery better, it also has improved media performance by depending more on the GPU for media decoding. And it allows now for high definition 1080p and H.264 videos. In addition, Silverlight 5 has introduced remote control support. And finally, the greatest features are its support for raw wave files and the ability to control playing speed.

Now, check out other What’s New in Silverlight 5? articles.