Showing posts with label Source Code. Show all posts
Showing posts with label Source Code. Show all posts

Sunday, October 5, 2008

Search through Windows Active Directory

Search through Windows Active Directory

How to search through Windows Active Directory is a topic
that will be presented in this post by writing some simple functions.

using System.DirectoryServices;
using System.Collections.Generic;
using System;
namespace photointeraction
{
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
///// Description: Simple functions to search through 
///// Microsoft Windows Active Directory
///// Author: G. R. Roosta
///// License: Free To Use (No Restriction)
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////


public static class Integerphoto
{
/// <summary> 
/// Authenticate user against a domain.
 /// </summary> 
/// <param name="loginName"></param>
 /// <param name="domainPath"></param>
 /// <returns></returns>
public static bool AuthenticateUser(string loginName, string domainPath){string userName = loginName.Substring(loginName.LastIndexOf('\\') + 1);DirectoryEntry searchRoot = new DirectoryEntry(domainPath);DirectorySearcher search = new DirectorySearcher(searchRoot);search.Filter = String.Format("(&(objectClass=user)(objectCategory=person)(SAMAccountName={0}))", userName);search.PropertiesToLoad.Add("cn");SearchResult result = search.FindOne();return result != null;}

/// <summary>/// Check if a user login exists is in the current/// Windows Active Directory./// </summary>/// <param name="loginName"></param>/// <returns></returns>public static bool CheckLogin(string loginName){string userName = loginName.Substring(loginName.LastIndexOf('\\') + 1);DirectorySearcher search = new DirectorySearcher();search.Filter = String.Format("(SAMAccountName={0})", userName);search.PropertiesToLoad.Add("cn");SearchResult result = search.FindOne();return result != null;}


/// <summary>
/// Retrieve list of all users in a domain./// </summary>/// <param name="domainPath">domainPath is set like "LDAP://DomainName"</param>/// <returns></returns>public static List<string> GetDomainUsers(string domainPath){List<string> rtn = new List<string>();DirectoryEntry searchRoot = new DirectoryEntry(domainPath);DirectorySearcher search = new DirectorySearcher(searchRoot);search.Filter = "(&(objectClass=user)(objectCategory=person))";search.PropertiesToLoad.Add("samaccountname");SearchResult result;SearchResultCollection resultCol = search.FindAll();if (resultCol != null){for (int counter = 0; counter < resultCol.Count; counter++){result = resultCol[counter];if (result.Properties.Contains("samaccountname")){rtn.Add((String)result.Properties["samaccountname"][0]);}}}return rtn;}

/// <summary>
/// Return list of users of an Active Directory group./// </summary>/// <param name="groupName"></param>/// <returns></returns>public static List<string> GetGroupUsers(string groupName){List<string> rtn = new List<string>();SearchResult result;DirectorySearcher search = new DirectorySearcher();search.Filter = String.Format("(cn={0})", groupName);search.PropertiesToLoad.Add("member");result = search.FindOne();if (result != null){for (int counter = 0; counter < result.Properties["member"].Count; counter++)
{
string user = (string)result.Properties["member"][counter];rtn.Add(user);
}
}return rtn;
}
}
}

Saturday, September 27, 2008

Brighten Photo

Brighten Photo 

In this post, a simple function is presented to brighten/darken photos. In this function I use GDI+ and write code in unsafe mode (for efficiency).

using System.Drawing;
using System.Drawing.Imaging;
using GRBIF._CONST;
using System;

namespace IntelliPhoto
{

// Description: A simple GDI+ function for brightening
// Author: G. R. Roosta
// License: Free To Use (No Restriction)

public class BasicFilter
{

public static void Brighten(Bitmap bitmap, int brightness, string newFullFileName)
{
Brighten(bitmap, brightness);
bitmap.Save(newFullFileName);
return;
}

public static void Brighten(Bitmap bitmap, int brightness) // -255 <= brightness <= 255
{
if (brightness < -255 brightness > 255)
throw new Exception("Invalid 'brightness' value!");
BitmapData bmpData = bitmap.LockBits(
new Rectangle(0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.ReadWrite,
PixelFormat.Format24bppRgb);
int stride = bmpData.Stride;
System.IntPtr Scan0 = bmpData.Scan0;
unsafe
{
byte* ptr = (byte*)(void*)Scan0;
int offset = stride - bitmap.Width * 3;
int netWidth = bitmap.Width * 3;
for (int y = 0; y <>
{
for (int x = 0; x <>
{
int c = ptr[0] + brightness;
if (c > 255)
c = 255;
else if (c <>
c = 0;

ptr[0] = (byte)c;
++ptr;
}
ptr += offset;
}
}

bitmap.UnlockBits(bmpData);
return;
}
}
}

Example:


Original Image (taken by Digital Camera: Sony DSC-S650)





Brightened with brightness=+40 using IntelliPhoto.BasicFilter.Brigten




Brightened with brightness=+80 using IntelliPhoto.BasicFilter.Brigten





Brightened with brightness=-40 using IntelliPhoto.BasicFilter.Brigten






Brightened with brightness=-80 using IntelliPhoto.BasicFilter.Brigten