Skip to content

Instantly share code, notes, and snippets.

@iodiot
Created December 9, 2015 08:43
Show Gist options
  • Save iodiot/8cd34490551edf229eeb to your computer and use it in GitHub Desktop.
Save iodiot/8cd34490551edf229eeb to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Threading;
namespace PF_Soft_test
{
class Drawing
{
public static Bitmap bitmap;
public static Point Start_Point;
public static bool isdraw = false;
public static Pen pen;
public static Brush brush;
public static Graphics graphics;
public static bool get_img(string path)
{
try
{
bitmap = new Bitmap(path);
return true;
}
catch
{
return false;
}
// bitmap.Dispose();
}
public static void Set_Start_Point(MouseEventArgs e)
{
isdraw = true;
Start_Point.X = e.X;
Start_Point.Y = e.Y;
}
public static void Initialize_G_table(PictureBox PictureBox)
{
Bitmap bit = new Bitmap(PictureBox.Width, PictureBox.Height);
PictureBox.Image = bit;
}
public static void Line(PictureBox PictureBox, MouseEventArgs e, Color color)
{
pen = new Pen(color, 3); // вынести за пределы функции или не выделять постоянно память!!!!!!!!!
if (isdraw == true)
{
graphics = PictureBox.CreateGraphics();
graphics.DrawLine(pen, Start_Point, new Point(e.X, e.Y));
}
else
if (isdraw == false)
{
using (graphics = Graphics.FromImage(PictureBox.Image))// вынести за пределы функции
{
graphics.DrawLine(pen, Start_Point, new Point(e.X, e.Y));
}
}
pen.Dispose();
graphics.Dispose();
}
// Action<T,Action<T,Action<T,T>>>
public static void Pencil(PictureBox PictureBox, MouseEventArgs e, Color color)
{
pen = new Pen(color, 1);// вынести за пределы функции или не выделять постоянно память!!!!!!!!!
using (graphics = Graphics.FromImage(PictureBox.Image))// вынести за пределы функции
{
graphics.DrawLine(pen, Start_Point.X, Start_Point.Y, e.X, e.Y);
Start_Point.X = e.X;
Start_Point.Y = e.Y;
}
pen.Dispose();
graphics.Dispose();
}
public delegate void Draw_Delegate<T>(T param, int x, int y, int width, int height);
private static void draw_figure<T>(MouseEventArgs e, Draw_Delegate<T> draw_func, T brushOrPen)
{
if (Start_Point.X > e.X && Start_Point.Y > e.Y)
{
draw_func(brushOrPen, e.X, e.Y, Start_Point.X - e.X, Start_Point.Y - e.Y);
}
else
{
if (Start_Point.X < e.X && Start_Point.Y < e.Y)
{
draw_func(brushOrPen, Start_Point.X, Start_Point.Y, e.X - Start_Point.X, e.Y - Start_Point.Y);
}
else if (Start_Point.X > e.X && Start_Point.Y < e.Y)
{
draw_func(brushOrPen, e.X, Start_Point.Y, Start_Point.X - e.X, e.Y - Start_Point.Y);
}
else if (Start_Point.X < e.X && Start_Point.Y > e.Y)
{
draw_func(brushOrPen, Start_Point.X, e.Y, e.X - Start_Point.X, Start_Point.Y - e.Y);
}
}
}
public static void Rectangle_Show(PictureBox PictureBox, MouseEventArgs e, Color color)
{
pen = new Pen(color, 1); // вынести за пределы функции или не выделять постоянно память!!!!!!!!!
graphics = PictureBox.CreateGraphics();// вынести за пределы функции
PictureBox.Refresh();
draw_figure<Pen>(e, graphics.DrawRectangle, pen);
pen.Dispose();
graphics.Dispose();
}
public static void Ellipse_Show(PictureBox PictureBox, MouseEventArgs e, Color color)
{
pen = new Pen(color, 1); // вынести за пределы функции или не выделять постоянно память!!!!!!!!!
graphics = PictureBox.CreateGraphics();// вынести за пределы функции
PictureBox.Refresh();
draw_figure<Pen>(e, graphics.DrawEllipse, pen);
pen.Dispose();
graphics.Dispose();
}
public static void Rectangle_Draw(PictureBox PictureBox, MouseEventArgs e, Color color, CheckBox Fill_rectangle)
{
pen = new Pen(Color.Black, 3);// вынести за пределы функции или не выделять постоянно память!!!!!!!!!
brush = new SolidBrush(Color.Blue);// вынести за пределы функции
using (graphics = Graphics.FromImage(PictureBox.Image))
{
if (Fill_rectangle.Checked)
{
draw_figure<Brush>(e,graphics.FillRectangle, brush);
}
draw_figure<Pen>(e, graphics.DrawRectangle, pen);
}
pen.Dispose();
graphics.Dispose();
}
public static void Ellipse_Draw(PictureBox PictureBox, MouseEventArgs e, Color color, CheckBox Fill_ellipse)
{
pen = new Pen(Color.Black, 3);// вынести за пределы функции или не выделять постоянно память!!!!!!!!!
brush = new SolidBrush(Color.Blue);// вынести за пределы функции
using (graphics = Graphics.FromImage(PictureBox.Image))
{
if (Fill_ellipse.Checked)
{
draw_figure<Brush>(e, graphics.FillEllipse, brush);
}
draw_figure<Pen>(e, graphics.DrawEllipse, pen);
}
pen.Dispose();
graphics.Dispose();
}
public static void Invert_image(PictureBox PictureBox) ///сделать в отдельном потоке!!!!!
{
Bitmap temp = (Bitmap)bitmap.Clone();
Color oldColor ;
Color newColor;
for (int x = 0; x < temp.Width; x++)
{
for (int y = 0; y < temp.Height; y++)
{
oldColor = temp.GetPixel(x, y);
newColor = Color.FromArgb(oldColor.A, 255 - oldColor.R, 255 - oldColor.G, 255 - oldColor.B);
bitmap.SetPixel(x, y, newColor);
}
PictureBox.Refresh();
}
temp.Dispose();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment