A
A. Irb
Guest
Hi all,
Im a bit of a novice in C#, especially the whole drawing aspect of it.
Im doing a small assignment for a class where you have to create a paint application that draws circles, rectangles, lines with colors. I've done most of the work but I cant seem to get it to work properly. Right now it draws rectangles as it should (with mouse) but as soon as I want to draw another one it deletes the original and does a new one (it shouldnt do this). It does this because of the system Control.Refresh(); method Im using - but I have to use it in order to draw proper rectangles, otherwise it draws rectangles like this:
IdotSTACKdotIMGURdotCOM/Myfsx.png
Is there a way to use the Refresh() method so it draws the rectangles correctly, but also doesnt refresh the form and delete the existing stuff drawn?
The full code I have now:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace MD4
{
public partial class Form1 : Form
{
Graphics g;
int x = -1;
int y = -1;
bool moving = false;
Pen pen;
bool line = false, rectangle = false, ellipse = false, eraser = false, penDraw = true;
Rectangle rect;
Point LocationXY; //square begin location
Point LocationX1Y1; //square end location
public Form1()
{
InitializeComponent();
g = panel1.CreateGraphics();
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
pen = new Pen(Color.Black, 5);
pen.StartCap = pen.EndCap = System.Drawing.Drawing2D.LineCap.Round;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
PictureBox p = (PictureBox)sender;
pen.Color = p.BackColor;
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
moving = true;
x = e.X; //Starting x and Y
y = e.Y;
panel1.Cursor = Cursors.Cross;
LocationXY = e.Location; //saves start location
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
if(moving == true)
{
LocationX1Y1 = e.Location;
}
moving = false;
x = -1;
y = -1;
panel1.Cursor = Cursors.Default;
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if(moving && x!= -1 && y != -1)
{
LocationX1Y1 = e.Location; //saves current location
if (eraser)
{
pen.Color = Color.White;
pen.Width = 50;
g.DrawLine(pen, new Point(x, y), e.Location);
x = e.X;
y = e.Y;
}
if (rectangle)
{
if (rect != null)
{
Refresh();
pen.Width = 5;
g.DrawRectangle(pen, GetRect());
}
}
if (ellipse)
{
Refresh();
pen.Width = 5;
g.DrawEllipse(pen, GetRect());
}
if (penDraw)
{
pen.Width = 5;
g.DrawLine(pen, new Point(x, y), e.Location);
x = e.X;
y = e.Y;
}
if (line)
{
Refresh();
pen.Width = 5;
g.DrawLine(pen, x, y, e.X, e.Y);
}
}
}
private Rectangle GetRect()
{
rect = new Rectangle();
rect.X = Math.Min(LocationXY.X, LocationX1Y1.X);
rect.Y = Math.Min(LocationXY.Y, LocationX1Y1.Y);
rect.Width = Math.Abs(LocationXY.X - LocationX1Y1.X);
rect.Height = Math.Abs(LocationXY.Y - LocationX1Y1.Y);
return rect;
}
private void drawPen_Click(object sender, EventArgs e)
{
line = false;
rectangle = false;
ellipse = false;
eraser = false;
penDraw = true;
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer(@"C:\Users\Miuskaste\Desktop\MD4\track2.wav");
player.Play();
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
}
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer(@"C:\Users\Miuskaste\Desktop\MD4\track1.wav");
player.Play();
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
int width = panel1.Size.Width;
int height = panel1.Size.Height;
using (Bitmap bmp = new Bitmap(width, height))
{
panel1.DrawToBitmap(bmp, new Rectangle(0, 0, width, height));
bmp.Save(@"C:\Users\Miuskaste\Desktop\testBMP.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
private void newToolStripMenuItem_Click(object sender, EventArgs e) //poga prieks New -- refresh lai izdzestu visu no ekrana..
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer(@"C:\Users\Miuskaste\Desktop\MD4\track1.wav");
player.Play();
Refresh();
}
private void printToolStripMenuItem_Click(object sender, EventArgs e)
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer(@"C:\Users\Miuskaste\Desktop\MD4\track3.wav");
player.Play();
if (printDialog1.ShowDialog() == DialogResult.OK)
{
}
}
private void colorDialogButton_Click(object sender, EventArgs e)
{
colorDialog1.ShowDialog();
pen.Color = colorDialog1.Color;
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void lineButton_Click(object sender, EventArgs e)
{
line = true;
rectangle = false;
ellipse = false;
eraser = false;
penDraw = false;
}
private void rectangleButton_Click(object sender, EventArgs e)
{
rectangle = true;
line = false;
ellipse = false;
eraser = false;
penDraw = false;
}
private void ellipseButton_Click(object sender, EventArgs e)
{
ellipse = true;
line = false;
rectangle = false;
eraser = false;
penDraw = false;
}
private void eraserButton_Click(object sender, EventArgs e)
{
eraser = true;
rectangle = false;
line = false;
ellipse = false;
penDraw = false;
}
}
}
And quite honestly, Im having troule saving/printing/opening files to the panel as well - if anyone could point me in the right direction on this? (It has to save/print/open the contents of panel1)
Any suggestions would be greatly appreciated.
Thank you.
Continue reading...
Im a bit of a novice in C#, especially the whole drawing aspect of it.
Im doing a small assignment for a class where you have to create a paint application that draws circles, rectangles, lines with colors. I've done most of the work but I cant seem to get it to work properly. Right now it draws rectangles as it should (with mouse) but as soon as I want to draw another one it deletes the original and does a new one (it shouldnt do this). It does this because of the system Control.Refresh(); method Im using - but I have to use it in order to draw proper rectangles, otherwise it draws rectangles like this:
IdotSTACKdotIMGURdotCOM/Myfsx.png
Is there a way to use the Refresh() method so it draws the rectangles correctly, but also doesnt refresh the form and delete the existing stuff drawn?
The full code I have now:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace MD4
{
public partial class Form1 : Form
{
Graphics g;
int x = -1;
int y = -1;
bool moving = false;
Pen pen;
bool line = false, rectangle = false, ellipse = false, eraser = false, penDraw = true;
Rectangle rect;
Point LocationXY; //square begin location
Point LocationX1Y1; //square end location
public Form1()
{
InitializeComponent();
g = panel1.CreateGraphics();
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
pen = new Pen(Color.Black, 5);
pen.StartCap = pen.EndCap = System.Drawing.Drawing2D.LineCap.Round;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
PictureBox p = (PictureBox)sender;
pen.Color = p.BackColor;
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
moving = true;
x = e.X; //Starting x and Y
y = e.Y;
panel1.Cursor = Cursors.Cross;
LocationXY = e.Location; //saves start location
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
if(moving == true)
{
LocationX1Y1 = e.Location;
}
moving = false;
x = -1;
y = -1;
panel1.Cursor = Cursors.Default;
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if(moving && x!= -1 && y != -1)
{
LocationX1Y1 = e.Location; //saves current location
if (eraser)
{
pen.Color = Color.White;
pen.Width = 50;
g.DrawLine(pen, new Point(x, y), e.Location);
x = e.X;
y = e.Y;
}
if (rectangle)
{
if (rect != null)
{
Refresh();
pen.Width = 5;
g.DrawRectangle(pen, GetRect());
}
}
if (ellipse)
{
Refresh();
pen.Width = 5;
g.DrawEllipse(pen, GetRect());
}
if (penDraw)
{
pen.Width = 5;
g.DrawLine(pen, new Point(x, y), e.Location);
x = e.X;
y = e.Y;
}
if (line)
{
Refresh();
pen.Width = 5;
g.DrawLine(pen, x, y, e.X, e.Y);
}
}
}
private Rectangle GetRect()
{
rect = new Rectangle();
rect.X = Math.Min(LocationXY.X, LocationX1Y1.X);
rect.Y = Math.Min(LocationXY.Y, LocationX1Y1.Y);
rect.Width = Math.Abs(LocationXY.X - LocationX1Y1.X);
rect.Height = Math.Abs(LocationXY.Y - LocationX1Y1.Y);
return rect;
}
private void drawPen_Click(object sender, EventArgs e)
{
line = false;
rectangle = false;
ellipse = false;
eraser = false;
penDraw = true;
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer(@"C:\Users\Miuskaste\Desktop\MD4\track2.wav");
player.Play();
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
}
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer(@"C:\Users\Miuskaste\Desktop\MD4\track1.wav");
player.Play();
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
int width = panel1.Size.Width;
int height = panel1.Size.Height;
using (Bitmap bmp = new Bitmap(width, height))
{
panel1.DrawToBitmap(bmp, new Rectangle(0, 0, width, height));
bmp.Save(@"C:\Users\Miuskaste\Desktop\testBMP.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
private void newToolStripMenuItem_Click(object sender, EventArgs e) //poga prieks New -- refresh lai izdzestu visu no ekrana..
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer(@"C:\Users\Miuskaste\Desktop\MD4\track1.wav");
player.Play();
Refresh();
}
private void printToolStripMenuItem_Click(object sender, EventArgs e)
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer(@"C:\Users\Miuskaste\Desktop\MD4\track3.wav");
player.Play();
if (printDialog1.ShowDialog() == DialogResult.OK)
{
}
}
private void colorDialogButton_Click(object sender, EventArgs e)
{
colorDialog1.ShowDialog();
pen.Color = colorDialog1.Color;
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void lineButton_Click(object sender, EventArgs e)
{
line = true;
rectangle = false;
ellipse = false;
eraser = false;
penDraw = false;
}
private void rectangleButton_Click(object sender, EventArgs e)
{
rectangle = true;
line = false;
ellipse = false;
eraser = false;
penDraw = false;
}
private void ellipseButton_Click(object sender, EventArgs e)
{
ellipse = true;
line = false;
rectangle = false;
eraser = false;
penDraw = false;
}
private void eraserButton_Click(object sender, EventArgs e)
{
eraser = true;
rectangle = false;
line = false;
ellipse = false;
penDraw = false;
}
}
}
And quite honestly, Im having troule saving/printing/opening files to the panel as well - if anyone could point me in the right direction on this? (It has to save/print/open the contents of panel1)
Any suggestions would be greatly appreciated.
Thank you.
Continue reading...