ویژوال 30 شارپ

سورس کدهای جالب سی شارپ فقط با یک نگاه

ویژوال 30 شارپ

سورس کدهای جالب سی شارپ فقط با یک نگاه

Reg Editor

// Create SubKey
Registry.LocalMachine.CreateSubKey(@"Software\Sinpin", RegistryKeyPermissionCheck.ReadWriteSubTree);

//Create Key and Set Value
RegistryKey reg = Registry.LocalMachine.OpenSubKey(@"Software\Sinpin", true);      
reg.SetValue("DWord", "1", RegistryValueKind.DWord);
reg.SetValue("ExpandString", "1", RegistryValueKind.ExpandString);
reg.SetValue("QWord", "1", RegistryValueKind.QWord);
reg.SetValue("String", "1", RegistryValueKind.String);
reg.SetValue("Unknown", "1", RegistryValueKind.Unknown);

// Delete Key
reg.DeleteValue("DWOrd");

// Delete SubKey
Registry.LocalMachine.DeleteSubKey(@"Software\Sinpin");

// Read Key Value
string val = reg.GetValue("QWord").ToString();

// Retrieve All Keys
foreach (string s in reg.GetValueNames())
    MessageBox.Show(s);

Read Write Pixel - Class

using System.Drawing.Imaging;

Bitmap bmp = new Bitmap(picturebox1.Image);

//read
Color c = bmp.GetPixel(1, 1);

//write
bmp.SetPixel(2, 2, c);


private void GetPixel_Example(PaintEventArgs e)
{

    // Create a Bitmap object from an image file.
    Bitmap myBitmap = new Bitmap("Grapes.jpg");

    // Get the color of a pixel within myBitmap.
    Color pixelColor = myBitmap.GetPixel(50, 50);

    // Fill a rectangle with pixelColor.
    SolidBrush pixelBrush = new SolidBrush(pixelColor);
    e.Graphics.FillRectangle(pixelBrush, 0, 0, 100, 100);
}

Read Write Pixel

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

[DllImport( "user32.dll" )]
static extern IntPtr GetDC( IntPtr hWnd );
[DllImport( "user32.dll" )]
static extern int ReleaseDC( IntPtr hWnd, IntPtr hDC );
[DllImport( "gdi32.dll" )]
static extern int GetPixel( IntPtr hDC, int x, int y );
[DllImport( "gdi32.dll" )]
static extern int SetPixel( IntPtr hDC, int x, int y, int color );

static public Color GetPixel( Control control, int x, int y )
{
    Color color = Color.Empty;
    if (control != null)
    {
        IntPtr hDC = GetDC( control.Handle );
        int colorRef = GetPixel( hDC, x, y );
        color = Color.FromArgb(
            (int)(colorRef & 0x000000FF),
            (int)(colorRef & 0x0000FF00) >> 8,
            (int)(colorRef & 0x00FF0000) >> 16 );
        ReleaseDC( control.Handle, hDC );
    }
    return color;
}
static public void SetPixel( Control control, int x, int y, Color color )
{
    if (control != null)
    {
        IntPtr hDC = GetDC( control.Handle );
        int argb = color.ToArgb();
        int colorRef =
            (int)((argb & 0x00FF0000) >> 16) |
            (int)(argb & 0x0000FF00) |
            (int)((argb & 0x000000FF) << 16);
        SetPixel( hDC, x, y, colorRef );
        ReleaseDC( control.Handle, hDC );
    }
}

Random Text 4

private string GenerateRandomString(int size)
{
    Random r = new Random();
    string legalChars = "1234567890";
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < size; i++)
        sb.Append(legalChars.Substring(r.Next(0, legalChars.Length - 1), 1));
    return sb.ToString();
}

private void button1_Click(object sender, EventArgs e)
{
     MessageBox.Show(GenerateRandomString(6));
}

Random Text 3

private long GenerateId3()
{
    byte[] buffer = Guid.NewGuid().ToByteArray();
    return BitConverter.ToInt64(buffer, 0);
}