Hi,
I am simulating a
paint application using graphics object. The problem happening is
,whenever i draw a new shape the previous one is getting cleared off. I
don't want that to happen. I want the shape to be there howmuch ever
time i redraw the shapes. Please tell me the solution for this....
Loading
Jan MontanoPosted Jun 13, 2008, 4:19 AM
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
namespace CreateShapes
{
public partial class Form1 : Form
{
private System.Drawing.Bitmap myBitmap;
public static int index;
int FirstXCoordinate, FirstYCoordinate, SecondXCoordinate, SecondYCoordinate;
bool mousePressed;
Graphics[] g = new Graphics[100];
GraphicsState gs;
List
public Form1()
{
InitializeComponent();
this.MouseDown += new MouseEventHandler(Form1_MouseDown);
//this.Paint += new PaintEventHandler(Form1_Paint);
this.MouseWheel += new MouseEventHandler(Form1_MouseWheel);
this.MouseUp += new MouseEventHandler(Form1_MouseUp);
}
void Form1_MouseWheel(object sender, MouseEventArgs e)
{
SecondXCoordinate = e.X;
SecondYCoordinate = e.Y;
//MessageBox.Show(e.X.ToString());
if (mousePressed == true)
{
this.Paint += new PaintEventHandler(Form1_Paint);
Refresh();
}
else
{
this.Paint -= new PaintEventHandler(Form1_Paint);
}
}
void Form1_Paint(object sender, PaintEventArgs e)
{
g[index] = e.Graphics;
myBitmap = new Bitmap(50, 50);
Rectangle rect = new Rectangle(50, 50, 20, 20);
g[index].DrawImage(myBitmap, 0, 0, 20, 20);
g[index].DrawRectangle(new Pen(Color.Black), FirstXCoordinate, FirstYCoordinate, (SecondXCoordinate - FirstXCoordinate), (SecondYCoordinate - FirstYCoordinate));
foreach (Rectangle rectangle in rectangles)
{
e.Graphics.DrawRectangle(Pens.Red, rectangle);
}
}
void Form1_MouseDown(object sender, MouseEventArgs e)
{
//for (int i = 0; i < index; i++)
//{
// MessageBox.Show(index.ToString());
// g[i].DrawRectangle(new Pen(Color.Black), FirstXCoordinate, FirstYCoordinate, (SecondXCoordinate - FirstXCoordinate), (SecondYCoordinate - FirstYCoordinate));
//}
FirstXCoordinate = e.X;
FirstYCoordinate = e.Y;
mousePressed = true;
MouseMove += new MouseEventHandler(Form1_MouseWheel);
}
void Form1_MouseUp(object sender, MouseEventArgs e)
{
//MessageBox.Show("up");
SecondXCoordinate = e.X;
SecondYCoordinate = e.Y;
ResetMouseEventArgs();
mousePressed = false;
//ImageConverter img = new ImageConverter();
//g[index] = this.CreateGraphics();
//this.Paint += new PaintEventHandler(Form1_Paint);
//index++;
rectangles.Add(new Rectangle(FirstXCoordinate, FirstYCoordinate, SecondXCoordinate - FirstXCoordinate, SecondYCoordinate - FirstYCoordinate));
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Vipin MammenPosted Jun 11, 2008, 3:36 AM
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
namespace CreateShapes
{
public partial class Form1 : Form
{
private System.Drawing.Bitmap myBitmap;
public static int index;
int FirstXCoordinate, FirstYCoordinate, SecondXCoordinate, SecondYCoordinate;
bool mousePressed;
Graphics[] g =new Graphics[100];
GraphicsState gs;
public Form1()
{
InitializeComponent();
this.MouseDown += new MouseEventHandler(Form1_MouseDown);
this.Paint += new PaintEventHandler(Form1_Paint);
this.MouseWheel+=new MouseEventHandler(Form1_MouseWheel);
this.MouseUp+=new MouseEventHandler(Form1_MouseUp);
}
void Form1_MouseWheel(object sender, MouseEventArgs e)
{
SecondXCoordinate = e.X;
SecondYCoordinate = e.Y;
//MessageBox.Show(e.X.ToString());
if(mousePressed==true)
{
this.Paint += new PaintEventHandler(Form1_Paint);
Refresh();
}
}
void Form1_Paint(object sender, PaintEventArgs e)
{
g[index] = e.Graphics;
myBitmap = new Bitmap(50, 50);
Rectangle rect = new Rectangle(50, 50, 20, 20);
g[index].DrawImage(myBitmap, 0, 0, 20, 20);
g[index].DrawRectangle(new Pen(Color.Black), FirstXCoordinate, FirstYCoordinate, (SecondXCoordinate - FirstXCoordinate), (SecondYCoordinate - FirstYCoordinate));
}
void Form1_MouseDown(object sender, MouseEventArgs e)
{
for (int i = 0; i < index; i++)
{
MessageBox.Show(index.ToString());
g[i].DrawRectangle(new Pen(Color.Black), FirstXCoordinate, FirstYCoordinate, (SecondXCoordinate - FirstXCoordinate), (SecondYCoordinate - FirstYCoordinate));
}
FirstXCoordinate = e.X;
FirstYCoordinate = e.Y;
mousePressed = true;
MouseMove += new MouseEventHandler(Form1_MouseWheel);
}
void Form1_MouseUp(object sender, MouseEventArgs e)
{
//MessageBox.Show("up");
SecondXCoordinate = e.X;
SecondYCoordinate = e.Y;
ResetMouseEventArgs();
mousePressed = false;
ImageConverter img = new ImageConverter();
g[index] = this.CreateGraphics();
this.Paint += new PaintEventHandler(Form1_Paint);
index++;
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
mavericPosted Jun 11, 2008, 3:27 AM
Jan MontanoPosted Jun 11, 2008, 3:19 AM