Wednesday, July 27, 2011

Windows Logon API for .NET

When you are developing windows or web applications and there is need for validating user credentials (either local or domain), the 'LogonUser' API helps alot and it makes developers life much easier.






This API function accepts five parameters and one of them is 'out' parameter.


Public Declare Auto Function LogonUser Lib "advapi32.dll" (ByVal lpszUsername As [String], _
ByVal lpszDomain As [String], ByVal lpszPassword As [String], _
ByVal dwLogonType As Integer, ByVal dwLogonProvider As Integer, _
ByRef phToken As IntPtr) As Boolean



This function returns 'true' if the authentication was success and 'false' if failed. The good part about it is that, it returns a valid exception with relevant error message when it fails to authenticate provided credentials.



After successful authentication, the phToken parameter(ByRef phToken As IntPtr)would give handle to the token and it can be used to create any process from that context. If you just want to use this API for authentication, you can ignore it.


Here is the usage.



Public Declare Auto Function LogonUser Lib "advapi32.dll" (ByVal lpszUsername As [String], _
ByVal lpszDomain As [String], ByVal lpszPassword As [String], _
ByVal dwLogonType As Integer, ByVal dwLogonProvider As Integer, _
ByRef phToken As IntPtr) As Boolean

Private Function LoginUser(ByVal UserName As String, ByVal Password As String, ByVal Domain As String) As Boolean
Try
Dim tokenHandle As New IntPtr(0)
' Call LogonUser to obtain a handle to an access token.
Dim returnValue As Boolean = Authorization.LogonUser(UserName, Password, Domain, 2, 0, tokenHandle)

'check if logon successful
If returnValue = False Then
Dim ret As Integer = Marshal.GetLastWin32Error()
Throw New System.ComponentModel.Win32Exception(ret)
Exit Function
End If

Return True

Catch ex As Exception
'Here is where it returns relevant error message when the API fails to authenticate user.
'ex.Message
End Try
Return False
End Function

Tuesday, July 26, 2011

Kinect - Remove background of person

Microsoft Kinect SDK for Windows is now available as a free download via Microsoft Research, which will allow developers to create new uses for the motion-based system beyond gaming.

This is my first article on Kinect SDK. I have come up with a simple application created on C# using Kinect SDK.

You can check the prerequisites here for developing Kinect based application.

This application automatically identifies foreground object structure (human/any moving object) and removes its background and displays only object on the canvas.



I have attached the source code for your reference.

Link to project --> http://goo.gl/LB3LL

Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Research.Kinect.Audio;
using Microsoft.Research.Kinect.Nui;
namespace KinectSample
{
/// 
/// Interaction logic for MainWindow.xaml
/// 
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private Runtime kinect;
private void OnWindowsLoaded(object sender, RoutedEventArgs e)
{
kinect = new Runtime();
kinect.Initialize(RuntimeOptions.UseColor | RuntimeOptions.UseSkeletalTracking | RuntimeOptions.UseDepthAndPlayerIndex);
kinect.DepthStream.Open(ImageStreamType.Depth, 2, ImageResolution.Resolution320x240, ImageType.DepthAndPlayerIndex);
kinect.VideoStream.Open(ImageStreamType.Video, 2, ImageResolution.Resolution640x480, ImageType.Color);
this.Person.Source = kinect.CreateLivePlayerRenderer();
}

private void OnWindowUnloaded(object sender, RoutedEventArgs e)
{
kinect.Uninitialize();
Environment.Exit(0);
}


}


public static class RuntimeExtensions
{
public static WriteableBitmap CreateLivePlayerRenderer(this Runtime runtime)
{
if (runtime.DepthStream.Width == 0)
throw new InvalidOperationException("Either open the depth stream before calling this method or use the overload which takes in the resolution that the depth stream will later be opened with.");
return runtime.CreateLivePlayerRenderer(runtime.DepthStream.Width, runtime.DepthStream.Height);
}
public static WriteableBitmap CreateLivePlayerRenderer(this Runtime runtime, int depthWidth, int depthHeight)
{
PlanarImage depthImage = new PlanarImage();
WriteableBitmap target = new WriteableBitmap(depthWidth, depthHeight, 96, 96, PixelFormats.Bgra32, null);
var depthRect = new System.Windows.Int32Rect(0, 0, depthWidth, depthHeight);

runtime.DepthFrameReady += (s, e) =>
{
depthImage = e.ImageFrame.Image;
//Debug.Assert(depthImage.Height == depthHeight && depthImage.Width == depthWidth);
};

runtime.VideoFrameReady += (s, e) =>
{
// don't do anything if we don't yet have a depth image
if (depthImage.Bits == null) return;

byte[] color = e.ImageFrame.Image.Bits;

byte[] output = new byte[depthWidth * depthHeight * 4];

// loop over each pixel in the depth image
int outputIndex = 0;
for (int depthY = 0, depthIndex = 0; depthY < depthHeight; depthY++)                 {                     for (int depthX = 0; depthX < depthWidth; depthX++, depthIndex += 2)                     {                         // combine the 2 bytes of depth data representing this pixel                         short depthValue = (short)(depthImage.Bits[depthIndex] | (depthImage.Bits[depthIndex + 1] << 8));                          // extract the id of a tracked player from the first bit of depth data for this pixel                         int player = depthImage.Bits[depthIndex] & 7;                          // find a pixel in the color image which matches this coordinate from the depth image                         int colorX, colorY;                         runtime.NuiCamera.GetColorPixelCoordinatesFromDepthPixel(                             e.ImageFrame.Resolution,                             e.ImageFrame.ViewArea,                             depthX, depthY, // depth coordinate                             depthValue,  // depth value                             out colorX, out colorY);  // color coordinate                          // ensure that the calculated color location is within the bounds of the image                         colorX = Math.Max(0, Math.Min(colorX, e.ImageFrame.Image.Width - 1));                         colorY = Math.Max(0, Math.Min(colorY, e.ImageFrame.Image.Height - 1));                          output[outputIndex++] = color[(4 * (colorX + (colorY * e.ImageFrame.Image.Width))) + 0];                         output[outputIndex++] = color[(4 * (colorX + (colorY * e.ImageFrame.Image.Width))) + 1];                         output[outputIndex++] = color[(4 * (colorX + (colorY * e.ImageFrame.Image.Width))) + 2];                         output[outputIndex++] = player > 0 ? (byte)255 : (byte)0;
}
}
target.WritePixels(depthRect, output, depthWidth * PixelFormats.Bgra32.BitsPerPixel / 8, 0);
};
return target;
}
}

}




Please feel free to contact me incase of any queries.