ویژوال 30 شارپ

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

ویژوال 30 شارپ

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

Snake

double i = 0;
        SolidBrush s = new SolidBrush(Color.Black);
        private void timer1_Tick(object sender, EventArgs e)
        {
            Bitmap b = new Bitmap(pictureBox1.Image, pictureBox1.Size);
            Graphics g = Graphics.FromImage(b);
            i+=3;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            g.DrawLine(new Pen(Brushes.Black),
                float.Parse((Math.Cos(Math.PI * i / 180) * (b.Width / 20 + (i / 11)) + b.Width / 2).ToString()),
                float.Parse((Math.Sin(Math.PI * i / 180) * (b.Width / 20 + (i / 11)) + b.Width / 2).ToString()),
                float.Parse((Math.Cos(Math.PI * (i-1) / 180) * (b.Width / 20+((i-1)/11)) + b.Width / 2).ToString()),
                float.Parse((Math.Sin(Math.PI * (i-1) / 180) * (b.Width / 20+((i-1)/11)) + b.Width / 2).ToString())
                );
            pictureBox1.Image = b;
            this.Text = i.ToString();
            if (i>=2660)
            {
                timer1.Enabled = false;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Bitmap b = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            pictureBox1.Image = b;
        }

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));
}