1
Answer

Need Your Help in Improving the Speed of Graphics while Drawing Lines on Picturebox with Image

SAMSON JOHN

SAMSON JOHN

16y
6.5k
1

Hi Experts,

Could you please help us in improving the spped of some graphics. I am attaching the sample code for your reference. We are drawing lines and then selecting them on a picturebox container using C#. The code is working fine but its a bit slow

Thanks for your time and effort in advance. The Code is below pasted.

Regards,

Sam

using System;

using System.Collections.Generic;

using System.Collections;

using System.Text;

using System.Drawing;

using System.Drawing.Drawing2D;

using System.Windows.Forms;

using System.Threading;

namespace SkoltaskenApplication.Classes

{

class LineFinal:PageEditing

{

#region Variable

public Boolean m_Drawing;

public Boolean FlgAddLine = false;

// ' Buffer for erasing rubberband lines.

private Bitmap m_BufferBitmap;

private Bitmap BitMapOnSelect;

public Graphics m_BufferGraphics;

//' The mouse position.

public Point m_Pos1;

public Point m_Pos2;

private Boolean FirstLineFlag = true;

public PictureBox pictureBox1 = null;

public MouseEventHandler mu = null;

public MouseEventHandler mm = null;

public MouseEventHandler md = null;

public PaintEventHandler pp = null;

private Pen myPen=null;

private Image img;

private Image backimg;

private lineData[] ArrayLine;

private Point MouseClickPosition;

public Boolean FlagOnMouseMove = false;

public string SKLAction="Selection";

#endregion

#region Constructor_Initialise

public LineFinal(){}

public LineFinal(string MouseMode) {

SKLAction = MouseMode;

}

public void InitializeComponent()

{

if (img == null)

{

img = (Image)ImageSelected.Clone();

backimg = (Image)ImageSelected.Clone();

}

this.mu = new MouseEventHandler(this.pictureBox1_MouseUp);

this.mm = new MouseEventHandler(this.pictureBox1_MouseMove);

this.md = new MouseEventHandler(this.pictureBox1_MouseDown);

this.pp = new PaintEventHandler(this.pictureBox1_Paint);

this.pictureBox1.MouseUp += this.mu;

this.pictureBox1.MouseMove += this.mm;

this.pictureBox1.MouseDown += this.md;

this.pictureBox1.Paint += this.pp;

this.pictureBox1.Image = img;

this.pictureBox1.BackgroundImage = backimg;

BitMapOnSelect = new Bitmap(this.pictureBox1.Size.Width, this.pictureBox1.Size.Height, this.pictureBox1.CreateGraphics());

m_BufferBitmap = new Bitmap(this.pictureBox1.Size.Width, this.pictureBox1.Size.Height, this.pictureBox1.CreateGraphics());

m_BufferGraphics = Graphics.FromImage(m_BufferBitmap);

m_Pos1 = PtPicturebox;

}

public void UnInitializeComponent()

{

this.pictureBox1.MouseUp -= this.mu;

this.pictureBox1.MouseMove += this.mm;

this.pictureBox1.MouseDown -= this.md;

mu = null;

md = null;

}

#endregion

#region Events

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)

{

if (SKLAction == "Drawing")

{

SKLAction = "Selection";

FirstLineFlag = false;

//' Draw the new line permanently on the buffer.

//' Note that the points used by DrawReversibleLine

//' are in screen coordinates so we need to use

//' PointToClient to convert that to form coordinates.

myPen = new Pen(Color.Blue, 1);

 

m_BufferGraphics = Graphics.FromImage(m_BufferBitmap);

m_BufferGraphics.DrawLine(myPen, this.pictureBox1.PointToClient(m_Pos1), this.pictureBox1.PointToClient(m_Pos2));

//saving data in array

lineData objLineData = new lineData();

objLineData.start = m_Pos1;

objLineData.end = m_Pos2;

objLineData.LinePen = myPen;

objLineData.ActualSelected = false;

objLineData.Selected = false;

LineArray.Add(objLineData);

 

}

}

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)

{

// ' Do nothing if this isn't the left mouse button.

if (e.Button != System.Windows.Forms.MouseButtons.Left) return ;

//// ' Save the current mouse position.

m_Pos1 = PtPicturebox;

m_Pos2 = Control.MousePosition;

ArrayLine = (lineData[])LineArray.ToArray(typeof(lineData));

LineArray.Clear();

for (int i = 0; i <= ArrayLine.Length -1; i++)

{

if (ArrayLine[i].Selected)

{

ArrayLine[i].ActualSelected = true;

}

LineArray.Add(ArrayLine[i]);

}

MouseClickPosition = Control.MousePosition;

}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)

{

// ' Do nothing if we're not drawing.

switch (SKLAction)

{

case "Drawing":

//' Erase the previous line.

if (FirstLineFlag == false)

{

ControlPaint.DrawReversibleLine(m_Pos1, m_Pos2, this.pictureBox1.BackColor);

}

FirstLineFlag = false;

//' Save the new point.

m_Pos2 = Control.MousePosition;

//' Draw the new line.

ControlPaint.DrawReversibleLine(m_Pos1, m_Pos2, this.pictureBox1.BackColor);

break ;

case "Selection":

//logic to draw seleted line in Selection  Calulation like m,c,y etc y=mx+c logic is ther

Selection objSelection = new Selection(Control.MousePosition);

objSelection.GetSelection();

 

try

{

img = (Image)ImageSelected.Clone();

this.pictureBox1.Image = img;

m_BufferBitmap = new Bitmap(this.pictureBox1.Size.Width, this.pictureBox1.Size.Height, this.pictureBox1.CreateGraphics());

m_BufferGraphics = Graphics.FromImage(m_BufferBitmap);

foreach (lineData x in LineArray)

{

if (x.Selected)

{

myPen = new Pen(Color.Yellow, 50);

myPen.StartCap = LineCap.Round;

myPen.EndCap = LineCap.Round;

m_BufferGraphics.DrawLine(myPen, this.pictureBox1.PointToClient(x.start), this.pictureBox1.PointToClient(x.end));

break;

}

}

//logic to redraw line

foreach (lineData x in LineArray)

{

m_BufferGraphics.DrawLine(x.LinePen, this.pictureBox1.PointToClient(x.start), this.pictureBox1.PointToClient(x.end));

}

}

catch (Exception ex)

{

}

break;

case "Moving":

 

 

 

break;

}

}

// Redraw the form.

private void pictureBox1_Paint(Object sender,PaintEventArgs e)

{

DrawForm(e.Graphics);

}

#endregion

#region Method

private void DrawForm(Graphics gr)

{

if (m_BufferBitmap != null)

{

gr.DrawImage(m_BufferBitmap, 0, 0);

}

}

private lineData GetSelctedLine()

{

lineData objlineData = new lineData();

foreach (lineData x in LineArray)

{

if (x.ActualSelected)

{

objlineData.start = x.start;

objlineData.end = x.end;

objlineData.LinePen = x.LinePen;

objlineData.Selected = x.Selected;

objlineData.ActualSelected = x.ActualSelected;

}

}

return objlineData;

}

public void DrawAfterDelete()

{

Image Img=null;

if (Img == null)

{

Img = (Image)ImageSelected.Clone();

}

this.pictureBox1.Image = Img;

m_BufferGraphics = Graphics.FromImage(m_BufferBitmap);

foreach (lineData x in LineArray)

// for (int i = 0; i <= ArrayLine.Length - 1; i++)

{

m_BufferGraphics.DrawLine(x.LinePen, this.pictureBox1.PointToClient(x.start), this.pictureBox1.PointToClient(x.end));

}

}

public void DrawWhileMoving()

{

Image Img = null;

if (Img == null)

{

Img = (Image)ImageSelected.Clone();

}

this.pictureBox1.Image = Img;

m_BufferGraphics = Graphics.FromImage(m_BufferBitmap);

foreach (lineData x in LineArray)

// for (int i = 0; i <= ArrayLine.Length - 1; i++)

{

 

m_BufferGraphics.DrawLine(x.LinePen, this.pictureBox1.PointToClient(x.start), this.pictureBox1.PointToClient(x.end));

}

}

 

#endregion

}

}

Answers (1)