Добро пожаловать на мой блог

The world is yours

PAINT

автор: admin | Сентябрь 14, 2010 | Раздел: Жизнь

Задумавались вы когда нибудь как сделан PAINT?
Вот немного из его реализации:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.IO;
namespace DrawMe
{
public partial class Form1 : Form
{</code>

protected Point[] apt = new Point[4];
Point startPoint = new Point();	 // stores the starting point of the object to be drawn
Point currentPoint = new Point();   // stores the current position of the mouse cursor
bool drag = false;//рисуем или нет
Point pt;
Point rectangle;
List rect = new List();
Square s = new Square();
int x = 0;
bool ButtonLeftIsUp = false;//Mouse Left button
List drawingshape = new List();	   //list to hold the shapes that have to be redrawn in onpaint
List
startLocation = new List
();  //list of starting points of shapes for onpaint
List
endLocation = new List
();	//list of end points of shapes for onpaint
List
Color_list = new List
();	 //list of colors holding the colors of the shapes for onPaint
List brushwidth = new List();	 //list of brushwidth of shapes for onPaint
List
points = new List
();// when we drawing some line
List bezier_line = new List();// when we drawing some line
Color Oldcolor = new Color();
Color color = new Color();
Graphics g;
Graphics g2;
Bitmap b;
Pen pen;
int width;
enum ToolSelect
{
eLine, eFreeHand, eRectangle, eElipse,
eSlinky, eSelectionRectangle, eABC, eErarser, eBrush, eTextureBrush, none, eBezier
};

ToolSelect tool;		// instance of toolselect enum
public Form1()
{
color = Color.Black;
Oldcolor = color;
width = 1;
pen = new Pen(Brushes.Black, 1);
tool = ToolSelect.none;
InitializeComponent();
this.ResizeRedraw = true;
this.DoubleBuffered = true;
OnResize(EventArgs.Empty);
}
protected override void OnResize(EventArgs ea)
{
base.OnResize(ea);

int cx = ClientSize.Width;
int cy = ClientSize.Height;

//apt[0] = new Point(cx / 4, cy / 2);
//apt[1] = new Point(cx / 2, cy / 4);
//apt[2] = new Point(cx / 2, 3 * cy / 4);
//apt[3] = new Point(3 * cx / 4, cy / 2);
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (tool == ToolSelect.eRectangle)
{

s = new Square(e.X, e.Y, color, width);
}
if (tool == ToolSelect.eBezier)
{

if (e.Button == MouseButtons.Left)
{
pt = apt[1];
}

else if (e.Button == MouseButtons.Right)
{
pt = apt[2];
}

else
return;

}

ButtonLeftIsUp = false;
if (tool == ToolSelect.eLine)
{
startPoint = e.Location;
}
drag = true;

}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (tool == ToolSelect.eRectangle)
{
s.EndX = e.X;
s.EndY = e.Y;
}
if (tool == ToolSelect.eBezier)
{
if (e.Button == MouseButtons.Left)
{
apt[1] = new Point(e.X, e.Y);
x = 1;
}
else if (e.Button == MouseButtons.Right)
{
apt[2] = new Point(e.X, e.Y);
x = 2;
}
}
label1.Text = "X:" + e.X.ToString();
label2.Text = "Y:" + e.Y.ToString();

if (drag == true)
{
if (tool == ToolSelect.eLine)
{
currentPoint = e.Location;
}
pictureBox1.Invalidate();
}
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{

Graphics g = Graphics.FromImage(b);
Graphics g2 = e.Graphics;
//Draw a line
//if (tool == ToolSelect.eLine)
//{
width = Convert.ToInt16(comboBox1.Text);

foreach (Line l in points)
{
Pen p = new Pen(l._color, l._width);
g2.DrawLine(p, l.p1, l.p2);

}
g2.DrawLine(pen, startPoint, currentPoint);
//}
//if (tool == ToolSelect.eBezier)
// {
width = Convert.ToInt16(comboBox1.Text);
foreach (BezierClass bc in bezier_line)
{

Pen pb = new Pen(bc._color, bc._width);
g2.DrawBeziers(pb, bc.p4);
}
g2.DrawBeziers(pen, apt);
//}
foreach (Square sq in rect)
{

Pen pb = new Pen(sq._color, sq._width);
g2.DrawRectangle(pb, sq.Rect);
}
g2.DrawRectangle(Pens.Red, s.Rect);

}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{

drag = false;
switch (tool)
{
case ToolSelect.eRectangle:
{

//rect.Add(new Square(startPoint.X,currentPoint.Y,color,width));
comboBox1.Visible = false;
ButtonLeftIsUp = false;
button11.Visible = false;
break;
}
case ToolSelect.eBezier:
{

comboBox1.Visible = true;
ButtonLeftIsUp = false;
button11.Visible = true;
break;
}
case ToolSelect.eLine:
{
points.Add(new Line(startPoint, currentPoint, color, width));
comboBox1.Visible = true;
button11.Visible = false;
break;
}
default:
{
comboBox1.Visible = false;
pictureBox1.Cursor = Cursors.Default;
button11.Visible = false;
break;
}
}
}

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

private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Cursor = Cursors.Cross;
tool = ToolSelect.eLine;

}

private void цветToolStripMenuItem_Click(object sender, EventArgs e)
{
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
width = Convert.ToInt16(comboBox1.Text);
color = colorDialog1.Color;
pen = new Pen(color, width);
}
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
width = Convert.ToInt16(comboBox1.Text);
pen = new Pen(color, width);
}

private void button2_Click(object sender, EventArgs e)
{
pictureBox1.Cursor = Cursors.Cross;
tool = ToolSelect.eBezier;

}

private void pictureBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (tool == ToolSelect.eBezier)
{
if (e.Button == MouseButtons.Left)
{
apt[0] = new Point(e.X, e.Y);
x = 3;
}

else if (e.Button == MouseButtons.Right)
{
apt[3] = new Point(e.X, e.Y);
x = 4;
}

}

}

private void button11_Click(object sender, EventArgs e)
{

//if (ButtonLeftIsUp == true)
//{
//    apt[0] = apt[1] = apt[2] = apt[3] = new Point(0, 0);
//}
//ButtonLeftIsUp = true;

//x = 0;
bezier_line.Add(new BezierClass(apt, color, width));

Point[] b = new Point[4];
apt = b;
}

private void button3_Click(object sender, EventArgs e)
{
tool = ToolSelect.eRectangle;
}

private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "Image files (*.BMP, *.JPG, *.GIF, *.TIF, *.PNG, *.ICO, *.EMF, *.WMF)|*.bmp;*.jpg;*.gif; *.tif; *.png; *.ico; *.emf; *.wmf";
if (dialog.ShowDialog() == DialogResult.OK)
{
Image image = Image.FromFile(dialog.FileName);
int width = image.Width;
int height = image.Height;
pictureBox1.Width = width;
pictureBox1.Height = height;

b = new Bitmap(Image.FromFile(dialog.FileName), width, height);

pictureBox1.Image = b;
}

}

private void printToolStripMenuItem_Click(object sender, EventArgs e)
{
printDialog1.ShowDialog();

}

private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Title = "Сохранить картинку как ...";
saveFileDialog1.OverwritePrompt = true;
saveFileDialog1.CheckPathExists = true;
saveFileDialog1.Filter =
"Bitmap File(*.bmp)|*.bmp|" +
"GIF File(*.gif)|*.gif|" +
"JPEG File(*.jpg)|*.jpg|" +
"TIF File(*.tif)|*.tif|" +
"PNG File(*.png)|*.png";
saveFileDialog1.ShowHelp = true;
// If selected, save
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{

pictureBox1.DrawToBitmap(b, pictureBox1.ClientRectangle);

// Get the user-selected file name
string fileName = saveFileDialog1.FileName;
// Get the extension
string strFilExtn =
fileName.Remove(0, fileName.Length - 3);
// Save file

switch (strFilExtn)
{
case "bmp":
b.Save(fileName, System.Drawing.Imaging.ImageFormat.Bmp);
break;
case "jpg":
b.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case "gif":
b.Save(fileName, System.Drawing.Imaging.ImageFormat.Gif);
break;
case "tif":
b.Save(fileName, System.Drawing.Imaging.ImageFormat.Tiff);
break;
case "png":
b.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
break;
default:
break;
}
}

}

private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{

Application.Exit();
}

private void button4_Click(object sender, EventArgs e)
{

}

private void button9_Click(object sender, EventArgs e)
{

}

}
}

Скачать полный проэкт можно здесь!

DrawMe

Мой блог находят по следующим фразам

Оставить отзыв