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);
}
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;
}
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);
}
bool b = false;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
b = true;
a.Clear();
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
b = false;
}
ArrayList a = new ArrayList();
Point[] p = new Point[50];
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (b == true)
{
a.Add(new Point(e.X, e.Y));
if (a.Count >= 50)
{
for (int i = 0; i < a.Count - 50; i++)
{
a.RemoveAt(i);
}
a.CopyTo(p);
}
else
{
a.CopyTo(p);
}
Bitmap bp = new Bitmap(this.Width, this.Height);
Graphics g = Graphics.FromImage(bp);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
Pen pn = new Pen(Brushes.Black);
pn.Width = 2;
pn.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
g.DrawLines(pn, p);
this.BackgroundImage = bp;
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
// Create pen.
Pen blackPen = new Pen(Color.Black, 3);
// Create points that define polygon.
Point point1 = new Point(30, 50);
Point point2 = new Point(100, 25);
Point point3 = new Point(200, 5);
Point point4 = new Point(250, 50);
Point point5 = new Point(270, 100);
Point point6 = new Point(250, 250);
Point[] curvePoints = {point1, point2, point3, point4, point5, point6};
// Draw polygon to screen.
g.DrawPolygon(blackPen, curvePoints);
// Fill polygon
g.FillPolygon(Brushes.Red, curvePoints);
}