ویژوال 30 شارپ

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

ویژوال 30 شارپ

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

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

Painting Chart

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            int a, b, c, d = 0;
            a = int.Parse(numericUpDown1.Value.ToString());
            b = a * 3600;
            c = b / 1000;
            d = 50;
            SolidBrush sb = new SolidBrush(Color.Black);
            Bitmap bit = new Bitmap(Width, Height);
            Graphics g = Graphics.FromImage(bit);
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            g.DrawPie(new Pen(sb, 1), d, 20, 190, 170, 0, 360);
            sb.Color = Color.Silver;
            g.FillPie(sb, d, 20, 190, 170, 0, 360);
            sb.Color = Color.Black;
            g.DrawPie(new Pen(sb, 1), d, 20, 190, 170, -c, c);
            sb.Color = Color.Yellow;
            g.FillPie(sb, d, 20, 190, 170, -c, c);
            this.BackgroundImage = bit;

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            numericUpDown1_ValueChanged(null, null);
        }

Make Bitmap On Startup 2

using System.Drawing;

void New_file(PictureBox pb)
{
bmp_image = newBitmap(pb.Width, pb.Height);
g = Graphics.FromImage(bmp_image);
g.FillRectangle(Brushes.White, newRectangle(0, 0, bmp_image.Width, bmp_image.Height));
pb.Image = bmp_image;
}

Make Bitmap On Startup

private Image CreateBitmap()
{
    System.Drawing.Bitmap flag = new System.Drawing.Bitmap(10, 10);
    for (int x = 0; x < flag.Height; ++x)
        for (int y = 0; y < flag.Width; ++y)
            flag.SetPixel(x, y, Color.White);
    for (int x = 0; x < flag.Height; ++x)
        flag.SetPixel(x, x, Color.Red);
    return flag;
}

private void Form1_Load(object sender, EventArgs e)
{
    pictureBox1.Image = CreateBitmap();
}