for (int i = 0; i < propInfo.Length; i++)
{
if (propInfo[i].Name == "Width")
{
width = (int)propInfo[i].GetValue(bm, null);
break;
}
}
// Call the GetPixel method
MethodInfo[] methInfo = bm.GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance);
for (int i = 0; i < methInfo.Length; i++)
{
if (methInfo[i].Name == "GetPixel")
{
ParameterInfo[] paramInfo = methInfo[i].GetParameters();
if (paramInfo.Length == 2)
{
object[] xy = new object[2];
if (paramInfo[0].Name == "x")
{
xy[0] = x;
xy[1] = y;
}
else
{
xy[1] = x;
xy[0] = y;
}
pixColor = (Color)methInfo[i].Invoke(bm, xy);
break;
}
'VB
Imports System.Reflection
Imports System.Drawing
Dim bm As New Bitmap(200, 100)
Dim width As Integer = 0
' Explicitly set one pixel for testing
Dim x As Integer = 199
Dim y As Integer = 20
Dim pixColor As Color = Color.Black
bm.SetPixel(x, y, Color.Magenta)
' Get the "Width" property
Dim propInfo As PropertyInfo() = _
bm.GetType().GetProperties((BindingFlags.GetProperty Or _
BindingFlags.Public Or BindingFlags.Instance))
Dim i As Integer
For i = 0 To propInfo.Length - 1
If propInfo(i).Name = "Width" Then
width = Fix(propInfo(i).GetValue(bm, Nothing))
Exit For
End If
Next i
' Call the SetPixel method
Dim methInfo As MethodInfo() = bm.GetType().GetMethods((BindingFlags.Public _
Or BindingFlags.Instance))
For i = 0 To methInfo.Length - 1
If methInfo(i).Name = "GetPixel" Then
Dim paramInfo As ParameterInfo() = methInfo(i).GetParameters()
If paramInfo.Length = 2 Then
Dim xy(1) As Object
If paramInfo(0).Name = "x" Then
xy(0) = x
xy(1) = y
Else
xy(1) = x
xy(0) = y
End If
pixColor = CType(methInfo(i).Invoke(bm, xy), Color)
Exit For
End If
End If
Next i
7.30. How do I determine the device name programatically?
The device name can be accessed through the System.Net namespace, as demonstrated by the following code. //C#
String devName = System.Net.Dns.GetHostName();
'VB
Dim devName As String = System.Net.Dns.GetHostName()