ویژوال 30 شارپ

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

ویژوال 30 شارپ

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

Transparent Layer On Image

using System.Drawing.Drawing2D;

privatestaticImage fill_layer(PictureBox Pic_Box,Image img,Color Layer_color,int Percent)
{
//از img به عنوان بک آپ استفاده می شود
//percent درصد شفافیت است
Pic_Box.Image = img;
Bitmap bmp_img = newBitmap(Pic_Box.Image);
Graphics ghp = Graphics.FromImage(bmp_img);
LinearGradientBrush LineaBrush;
LineaBrush = newLinearGradientBrush(newRectangle(0, 0, bmp_img.Width, bmp_img.Height), Color.FromArgb(Percent, Layer_color), Color.FromArgb(Percent, Layer_color), LinearGradientMode.BackwardDiagonal);
ghp.FillRectangle(LineaBrush, newRectangle(0, 0, bmp_img.Width, bmp_img.Height));
return (Image)bmp_img;
}

pictureBox1.Image = fill_layer(pictureBox1,pictureBox2.Image, Color,Value);

Transparent layer

private void button2_Click(object sender, EventArgs e)
        {
            Random r = new Random();
            Bitmap b = new Bitmap(Width, Height);
            Graphics g = Graphics.FromImage(b);
            SolidBrush br = new SolidBrush(Color.FromArgb(150, r.Next(255),
                r.Next(255), r.Next(255)));
            g.FillRectangle(br,0,0,Width,Height);
            pictureBox1.Image = b;
        }

Transparent Circle Painting

bool b = false;
        int x1, y1 = 0;
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (b == true)
            {
                SolidBrush sb = new SolidBrush(Color.White);
                Bitmap bi = new Bitmap(Width, Height);
                Graphics g = Graphics.FromImage(bi);
                int xx = e.X - x1;
                int yy = e.Y - y1;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                g.DrawEllipse(new Pen(sb), new Rectangle(x1, y1, xx, yy));
                sb.Color = Color.FromArgb(150, 255, 0, 0);
                g.FillEllipse(sb, new Rectangle(x1, y1, xx, yy));
                pictureBox1.Image = bi;

            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            b = false;
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            b = true;
            x1 = e.X;
            y1 = e.Y;
        }

Texture Brush

bool cl = false;
        int x1, y1 = 0;
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            cl = true;
            x1 = e.X;
            y1 = e.Y;
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            cl = false;
        }
        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (cl == true)
            {
                Bitmap b = new Bitmap(5, 5);
                Rectangle r1 = new Rectangle(0, 0, 5, 5);
                Graphics g = Graphics.FromImage(b);
                g.SmoothingMode = SmoothingMode.AntiAlias;
                LinearGradientBrush lgb = new LinearGradientBrush(r1,
                    Color.Yellow, Color.Green , LinearGradientMode.ForwardDiagonal);
                g.FillRectangle(lgb, r1);
               
                Rectangle r = new Rectangle(x1, y1, (e.X - x1) + 1, (e.Y - y1) + 1);
                Bitmap b1 = new Bitmap(Width, Height);
                Graphics g1 = Graphics.FromImage(b1);
                g1.SmoothingMode = SmoothingMode.AntiAlias;
                TextureBrush tb = new TextureBrush(b);
                g1.FillRectangle(tb, r);
                g1.DrawRectangle(new Pen(Brushes.Black), r);
                this.BackgroundImage = b1;

            }
        }

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