ویژوال 30 شارپ

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

ویژوال 30 شارپ

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

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

ListView View Change

private void button2_Click(object sender, EventArgs e)
{
    int n = (int) listView1.View;
    if (n == 4)
        n = -1;
    listView1.View = (View)Enum.ToObject(typeof(View), ++n);
}

Gray Scale 2

using System.Drawing.Imaging;

public static Bitmap MakeGrayscale(Bitmap original)
{
    //create a blank bitmap the same size as original
    Bitmap newBitmap =
    new Bitmap(original.Width, original.Height);

    //get a graphics object from the new image
    Graphics g = Graphics.FromImage(newBitmap);

    //create the grayscale ColorMatrix
    ColorMatrix colorMatrix = new ColorMatrix(
    new float[][]{
        new float[] {.3f, .3f, .3f, 0, 0},
        new float[] {.59f, .59f, .59f, 0, 0},
        new float[] {.11f, .11f, .11f, 0, 0},
        new float[] {0, 0, 0, 1, 0},
        new float[] {0, 0, 0, 0, 1}});

    //create some image attributes
    ImageAttributes attributes = new ImageAttributes();

    //set the color matrix attribute
    attributes.SetColorMatrix(colorMatrix);

    //draw the original image on the new image
    //using the grayscale color matrix
    g.DrawImage(original,
    new Rectangle(0, 0, original.Width, original.Height),
    0, 0, original.Width, original.Height,
    GraphicsUnit.Pixel, attributes);

    //dispose the Graphics object
    g.Dispose();
    return newBitmap;
}


private void button1_Click(object sender, EventArgs e)
{
    Bitmap b = (Bitmap) pictureBox1.Image;
    pictureBox2.Image = MakeGrayscale(b);
}

Gray Scale

using System.Drawing.Imaging;


public Image GrayScaleImage(Graphics graph, Image img, int left, int top)
        {
                     
            ColorMatrix colorMix = new ColorMatrix();           
            colorMix.Matrix00 = 1 / 3f;
            colorMix.Matrix01 = 1 / 3f;
            colorMix.Matrix02 = 1 / 3f;
            colorMix.Matrix10 = 1 / 3f;
            colorMix.Matrix11 = 1 / 3f;
            colorMix.Matrix12 = 1 / 3f;
            colorMix.Matrix20 = 1 / 3f;
            colorMix.Matrix21 = 1 / 3f;
            colorMix.Matrix22 = 1 / 3f;

            ImageAttributes imgAttrib = new ImageAttributes();
            imgAttrib.SetColorMatrix(colorMix);


            graph.DrawImage(img, new Rectangle(left, top, img.Width,
                 img.Height), 0, 0, img.Width, img.Height,
                 GraphicsUnit.Pixel, imgAttrib);
            Bitmap bmp = new Bitmap(img);
            return bmp;


        }

Graphic Up

private void Form1_Paint(object sender, PaintEventArgs e)
{
    // حالت معمولی
    e.Graphics.DrawEllipse(new Pen(Color.Red, 10), 30, 30, 50, 50);

    //  SmoothingMode.AntiAlias  روانسازی به کمک
    e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
    e.Graphics.DrawEllipse(new Pen(Color.Blue, 10), 30, 100, 50, 50);
}