Thursday, November 20, 2008

C#: Capture ScreenShot

Courtesy: NOBUGZ (http://forums.microsoft.com/MSDN/User/Profile.aspx?UserID=170041&SiteID=1)


using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace KSeeSharp
{
class ScreenShot
{
[DllImport("gdi32.dll")]
static extern bool BitBlt(IntPtr hdcDest, int xDest, int yDest, int wDest, int hDest, IntPtr hdcSource, int xSrc, int ySrc, CopyPixelOperation rop);
[DllImport("user32.dll")]
static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDc);
[DllImport("gdi32.dll")]
static extern IntPtr DeleteDC(IntPtr hDc);
[DllImport("gdi32.dll")]
static extern IntPtr DeleteObject(IntPtr hDc);
[DllImport("gdi32.dll")]
static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);
[DllImport("gdi32.dll")]
static extern IntPtr CreateCompatibleDC(IntPtr hdc);
[DllImport("gdi32.dll")]
static extern IntPtr SelectObject(IntPtr hdc, IntPtr bmp);
[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr ptr);

public void CaptureScreenShot(string filePath)
{
Size sz = Screen.PrimaryScreen.Bounds.Size;
IntPtr hDesk = GetDesktopWindow();
IntPtr hSrce = GetWindowDC(hDesk);
IntPtr hDest = CreateCompatibleDC(hSrce);
IntPtr hBmp = CreateCompatibleBitmap(hSrce, sz.Width, sz.Height);
IntPtr hOldBmp = SelectObject(hDest, hBmp);
bool b = BitBlt(hDest, 0, 0, sz.Width, sz.Height, hSrce, 0, 0, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt);
Bitmap bmp = Bitmap.FromHbitmap(hBmp);
SelectObject(hDest, hOldBmp);
DeleteObject(hBmp);
DeleteDC(hDest);
ReleaseDC(hDesk, hSrce);
bmp.Save(filePath);
bmp.Dispose();
}

}
}

C#: Retrieve Machine Information

using System.Management;
using System.Diagnostics;

class MachineInfo
{
ManagementObjectSearcher query;
ManagementObjectCollection result;
string responseString;
int responseInt;

public string GetMachineName()
{
return Environment.MachineName.ToUpper();
}

public string GetOSVersion()
{
return Environment.OSVersion.VersionString;
}

public string GetProcessorCount()
{
return Environment.ProcessorCount.ToString();
}

public string GetIPAddress()
{
IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName());
IPAddress[] ipAddress = ipEntry.AddressList;
return ipAddress[0].ToString();
}

public string GetTotalPhysicalMemory()
{
query = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalMemoryConfiguration");
result = query.Get();
foreach (ManagementObject managementObject in result)
{
responseInt = Convert.ToInt32(managementObject["TotalPhysicalMemory"].ToString());
}
responseInt = (responseInt / 1024);
responseString = responseInt.ToString() + " MB";
return responseString;
}

public string GetAvailablePhysicalMemory()
{
PerformanceCounter counter = new PerformanceCounter("Memory", "Available Bytes");
responseInt = ((int)Convert.ToInt64(counter.NextValue())) / (1024 * 1024);
responseString = responseInt.ToString() + " MB";
return responseString;
}

public string GetTotalVirtualMemory()
{
query = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalMemoryConfiguration");
result = query.Get();
foreach (ManagementObject managementObject in result)
{
responseInt = Convert.ToInt32(managementObject["TotalVirtualMemory"].ToString());
}
responseInt = (responseInt / 1024);
responseString = responseInt.ToString() + " MB";
return responseString;
}

public string GetAvailableVirtualMemory()
{
query = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalMemoryConfiguration");
result = query.Get();
foreach (ManagementObject managementObject in result)
{
responseInt = Convert.ToInt32(managementObject["AvailableVirtualMemory"].ToString());
}
responseInt = (responseInt / 1024);
responseString = responseInt.ToString() + " MB";
return responseString;
}

public string GetCpuFrequency()
{
query = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
result = query.Get();
foreach (ManagementObject managementObject in result)
{
responseInt = Convert.ToInt32(managementObject["CurrentClockSpeed"].ToString());
}
responseString = responseInt.ToString() + " MHz";
return responseString;
}
}

Thursday, November 13, 2008

C#: Check if an application is already running

using System.Diagnostics;

bool IsApplicationAlreadyRunning()
{
string proc=Process.GetCurrentProcess().ProcessName;
Process[] processes=Process.GetProcessesByName(proc);
if (processes.Length > 1)
return true;
else
return false;
}