For reasons of aesthetics, I want to create a line composed of parallelograms like this:
But it turns out that the OnPaint override event only allows you to draw rectangles:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Rectangle[] rectangles = new Rectangle[]
{
new Rectangle(0, 0, 100, 30),
new Rectangle(100, 0, 100, 30),
new Rectangle(200, 0, 100, 30),
new Rectangle(300, 0, 100, 30),
new Rectangle(400, 0, 100, 30),
};
e.Graphics.DrawRectangles(new Pen(Brushes.Black), rectangles);
}
My problem is that I need to convert the rectangles into parallelograms, and give each one a different color.
The FillPolygon can do the job:
protected override void OnPaint(PaintEventArgs e) {
base.OnPaint(e);
e.Graphics.Clear(Color.White);
int x = 10;
int y = 10;
int width = 148;
int height = 64;
int lean = 36;
Color[] colors = new[] { Color.FromArgb(169, 198, 254),
Color.FromArgb(226, 112, 112),
Color.FromArgb(255, 226, 112),
Color.FromArgb(112, 226, 112),
Color.FromArgb(165, 142, 170)};
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
for (int i = 0; i < colors.Length; ++i) {
using (SolidBrush br = new SolidBrush(colors[i])) {
e.Graphics.FillPolygon(br, new Point[] { new Point(x, y),
new Point(x + lean, y + height),
new Point(x + lean + width, y + height),
new Point(x + width, y)});
x += width;
}
}
}
Related
I've been drawn multiple rectangles on the picturebox image.I want to resize all that rectangles using mouse events. Can anyone help me out regarding this?
public List<rectangle> listRec = new List<rectangle>();
Graphics g;
//private Graphics g;
Point startPos;
Point currentPos;
bool drawing;
Rectangle r1;
Rectangle rect = new Rectangle();
private Rectangle getRectangle()
{
r1 = new Rectangle(
Math.Min(startPos.X, currentPos.X),
Math.Min(startPos.Y, currentPos.Y),
Math.Abs(startPos.X - currentPos.X),
Math.Abs(startPos.Y - currentPos.Y));
return r1;
}
private void button1_Click(object sender, EventArgs e)
{
String data;
Font font = new Font("Arial", 14);
arg1 = Convert.ToInt32(textBox1.Text);
arg2 = Convert.ToInt32(textBox2.Text);
Rectangle rect = new Rectangle();
rect.Size = new Size(40, 65);
for (int x = 0; x < arg1; x++)
{
// rect.X = x * rect.Width;
rect.X = x * (rect.Width + 30) + 73;
for (int y = 0; y < arg2; y++)
{
rect.Y = y * (rect.Height + 35) + 38;
listRec.Add(rect);
data = rect.ToString();
TextWriter txt = new StreamWriter("E:\\B1Pockets.txt", true);
txt.WriteLine(data);
txt.Close();
// MessageBox.Show(rect.ToString());
}
}
foreach (Rectangle rec in listRec)
{
g = pictureBox1.CreateGraphics();
Pen p = new Pen(Color.Red, 3);
g.DrawRectangle(p, rec);
g.DrawString("p1", font, new SolidBrush(Color.Yellow), (rect.Width + 30), 35);
g.DrawString("p2", font, new SolidBrush(Color.Yellow), (rect.Width + 40) + 60, 35);
g.DrawString("p3", font, new SolidBrush(Color.Yellow), (rect.Width + 40) + 130, 35);
g.DrawString("p4", font, new SolidBrush(Color.Yellow), (rect.Width + 30), (rect.Height + 30) + 40);
g.DrawString("p5", font, new SolidBrush(Color.Yellow), (rect.Width + 40) + 60, (rect.Height + 30) + 40);
g.DrawString("p6", font, new SolidBrush(Color.Yellow), (rect.Width + 40) + 130, (rect.Height + 30) + 40);
}
}
I've tried this code to my application.I drawn rectangles in 2*3 manner.and also draw a big rectangle above it.In short my picturebox containing many rectangles and i want to add resizing options for all the rectangles in c#
Ok, I have got a printDocument1_PrintPage method which is supposed to print out the contents of a ListView. During execution, the method only prints out the first page. Could someone help me to printout the remaining page(s)?
I have identified the end of the first page via the variable ypos and set the property e.HasMorePages = true.
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e){
Graphics graphics = e.Graphics;
int ypos = 78;
Font f1 = new Font("Arial", 14, FontStyle.Bold, GraphicsUnit.Pixel);
Brush brush = new SolidBrush(Color.LightSlateGray);
graphics.FillRectangle(brush, new Rectangle(10, 10, 770, 50));
Pen blackpen = new Pen(Color.Black);
Pen graypen = new Pen(Color.LightGray);
Pen redpen = new Pen(Color.Red);
graphics.DrawRectangle(blackpen, new Rectangle(10, 10, 770, 50));
Brush B = new SolidBrush(listView1.ForeColor);
graphics.DrawLine(blackpen, 10, 10, 10, 1132);
graphics.DrawString("FORENAME", f1, Brushes.Black, new Point(20, 25));
graphics.DrawLine(blackpen, 130, 10, 130, 1132);
graphics.DrawString("SURNAME", f1, Brushes.Black, new Point(140, 25));
graphics.DrawLine(blackpen, 290, 10, 290, 1132);
graphics.DrawString("EXT.", f1, Brushes.Black, new Point(300, 25));
graphics.DrawLine(blackpen, 380, 10, 380, 1132);
graphics.DrawString("JOB TITLE", f1, Brushes.Black, new Point(410, 25));
graphics.DrawLine(blackpen, 780, 10, 780, 1132);
int[] X = { 15, 140, 300, 390, 720 };
int Y = 60;
f1 = listView1.Font;
for (int I = 0; I < listView1.Items.Count; I++){
for (int J = 0; J < listView1.Items[I].SubItems.Count - 1; J++){
graphics.DrawString(listView1.Items[I].SubItems[J].Text, f1, B, X[J], Y);
}
Y += f1.Height;
graphics.DrawLine(graypen, 10, ypos, 780, ypos);
ypos = ypos + 17;
if (ypos > 1132){
e.HasMorePages = true;
return;
}
}
graphics.Dispose();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
Pen mypen = default(Pen);
mypen = new Pen(System.Drawing.Color.Red, 3);
mypen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
//For Dash Line in Rectangle
Pen mypen1 = default(Pen);
mypen1 = new Pen(System.Drawing.Color.Blue, 1);
mypen1.DashStyle![enter image description here][2] = System.Drawing.Drawing2D.DashStyle.Dash;
//Pen mypen2 =default(Pen);
//mypen2 = new Pen(System.Drawing.Color.Yellow, 3);
//mypen2.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
//Pen mypen3 = default(Pen);
//mypen3 = new Pen(System.Drawing.Color.Violet, 1);
//mypen3.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
L1 = Rect1LT.X + 5;
T1 = Rect1LT.Y + 5;
W1 = Rect1RB.X - Rect1LT.X;
H1 = Rect1RB.Y - Rect1LT.Y;
e.Graphics.DrawRectangle(mypen, new System.Drawing.Rectangle(L1, T1, W1, H1));
e.Graphics.DrawRectangle(mypen1, new System.Drawing.Rectangle(L1, T1, W1, H1));
e.Graphics.DrawRectangle(Pens.Blue, new System.Drawing.Rectangle(Rect1LT.X, Rect1LT.Y, 10, 10));
e.Graphics.DrawRectangle(Pens.Blue, new System.Drawing.Rectangle(Rect1RT.X, Rect1RT.Y, 10, 10));
e.Graphics.DrawRectangle(Pens.Blue, new System.Drawing.Rectangle(Rect1LB.X, Rect1LB.Y, 10, 10));
e.Graphics.DrawRectangle(Pens.Blue, new System.Drawing.Rectangle(Rect1RB.X, Rect1RB.Y, 10, 10));
e.Graphics.FillRectangle(Brushes.Blue, Rect1LT);
e.Graphics.FillRectangle(Brushes.Blue, Rect1RT);
e.Graphics.FillRectangle(Brushes.Blue, Rect1LB);
e.Graphics.FillRectangle(Brushes.Blue, Rect1RB);
e.Graphics.DrawRectangle(Pens.Blue, new System.Drawing.Rectangle(Rect1T.X, Rect1T.Y, 10, 10));
e.Graphics.DrawRectangle(Pens.Blue, new System.Drawing.Rectangle(Rect1R.X, Rect1R.Y, 10, 10));
e.Graphics.DrawRectangle(Pens.Blue, new System.Drawing.Rectangle(Rect1B.X, Rect1B.Y, 10, 10));
e.Graphics.DrawRectangle(Pens.Blue, new System.Drawing.Rectangle(Rect1L.X, Rect1L.Y, 10, 10));
e.Graphics.FillRectangle(Brushes.Blue, Rect1T);
e.Graphics.FillRectangle(Brushes.Blue, Rect1R);
e.Graphics.FillRectangle(Brushes.Blue, Rect1B);
e.Graphics.FillRectangle(Brushes.Blue, Rect1L);
DataGridView dg1 = new DataGridView();
//while (!exit)
//{
// var time = GetTime();
// Update(time);
// Render(time);
//}
}
I want to Divide Rectangle in Rows and Columns and Size of Rows and Column can be changeable at runtime? and No of Rows and Columns also can be changeable? I don't want to split whole rectangle I just want to divide them in rectangle.
Here is a simplfied example:
int cols = 7; int rows = 11;
private void panel1_Paint(object sender, PaintEventArgs e)
{
Rectangle Rect = // your Rectangle!
Rectangle pRect = Rect; // panel2.ClientRectangle;
float width = 1f * pRect.Width / cols;
float height = 1f * pRect.Height / rows;
using (Pen pen = new Pen(System.Drawing.Color.Blue, 1))
{
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
for (int c = 0; c < cols; c++)
for (int r = 0; r < rows; r++)
{
RectangleF rect = new RectangleF(pRect.X + c * width, pRect.Y + r * height,
width, height);
e.Graphics.FillRectangle(Brushes.Coral, rect);
// e.Graphics.DrawRectangle(pen, rect.X, rect.Y, rect.Width, rect.Height);
}
for (int c = 0; c < cols; c++) e.Graphics.DrawLine(pen,
pRect.X + c * width, pRect.Y, pRect.X + c * width, pRect.Y + pRect.Height);
for (int r = 0; r < rows; r++) e.Graphics.DrawLine(pen,
pRect.X, pRect.Y + r * height, pRect.X + pRect.Width, pRect.Y + r * height);
e.Graphics.DrawRectangle(Pens.Red,
pRect.X, pRect.Y, pRect.Width - 1, pRect.Height - 1);
}
}
Note a few changes:
The border color and the fill color must not be the same.
The fill must come first or it will overwrite the border
I work with floats to fill the panel completely; if the cols and rows don't divide into the panel/rectangle size evenly, the recangles will not all have the same sizes ..
At first I have ignored your DashStyle. If you want to have a DashStyle you must completely change your plan! The reason is that if you draw Rectangles in a grid you..
..either have them overlapping and then the dashes will get in each others way. There is a DashOffset parameter but I don't think it can be twisted to make it work over any grid.
..or you need to draw the rectangles inside the grid cells but then they will be twice a thick and the patterns still will disturb each other.
Instead you simply draw only a few lines as shown!
Here is my example with Dashes:
I'd like to know how to paint User Control with background Transparency even if the user control is raised or moved at runtime.
my code is
private void UserControl1_Paint(object sender, PaintEventArgs e)
{
var g = e.Graphics;
g.Clear(Color.White);
g.SmoothingMode = SmoothingMode.HighQuality;//可以反锯齿
var rectBound = new Rectangle(0, 0, Width-1, Height-1);
var b = new SolidBrush(Color.FromArgb(0, 122, 204));
var rect = new Rectangle(2, 2, Width - 4, Height - 4);
if(!_isSelected)//FillRectangle
g.FillEllipse(b, rectBound);
else
g.FillEllipse(b, rect);
var pen = new Pen(Color.Yellow);
pen.DashStyle = DashStyle.DashDot;
g.DrawLine(pen,10,10,100,10);
pen.DashStyle = DashStyle.Dash;
g.DrawLine(pen, 10, 15, 100, 15);
pen.DashStyle = DashStyle.DashDotDot;
g.DrawLine(pen, 10, 20, 100, 20);
pen.DashStyle = DashStyle.Dot;
g.DrawLine(pen, 10, 25, 100, 25);
pen.DashStyle = DashStyle.Solid;
g.DrawLine(pen, 10, 30, 100, 30);
if (_isSelected)
{
pen = new Pen(Color.Black) { DashStyle = DashStyle.Dot, Width = 1 };
g.DrawRectangle(pen, rectBound);
}
}
and demo picture here
there will be a lot of User Controls Created at runtime,some of them maybe overlapped,then it should be clear the user control's background.how to do that?
I want to print data contained in part of a area in a windows form in C#. This is my DrawAll() method which called when click on preview button.I want to print only the data in that part without background image. But It not give the correct format,
this is the correct format
This is what i got
this is the DrawAll method
` private void DrawAll(Graphics g)
{
// RectangleF srcRect = new RectangleF(0, 0, this.pictureBox1.Width, this.pictureBox1.Height);
Rectangle srcRect=new Rectangle(0,0,this.pictureBox1.Width,this.pictureBox1.Height);
int nWidth = printDocument1.PrinterSettings.DefaultPageSettings.PaperSize.Width;
int nHeight = printDocument1.PrinterSettings.DefaultPageSettings.PaperSize.Height;
Rectangle destRect = new Rectangle(0, 0, nWidth, nHeight /3);
// Rectangle drawRect = (Rectangle)destRect.;
// g.DrawImage(this.pictureBox1.BackgroundImage, destRect, srcRect, GraphicsUnit.Pixel);
Pen aPen = new Pen(Brushes.Black, 1);
g.DrawRectangle(aPen,destRect);
float scalex = destRect.Width / srcRect.Width;
float scaley = destRect.Height / srcRect.Height;
// Pen aPen = new Pen(Brushes.Black, 1);
for (int i = 0; i < this.Controls.Count; i++)
{
if (Controls[i].GetType() == this.paytext.GetType())
{
TextBox theText = (TextBox)Controls[i];
g.DrawString(theText.Text, theText.Font, Brushes.Black, theText.Bounds.Left * scalex, theText.Bounds.Top * scaley, new StringFormat());
}
if (Controls[i].GetType() == this.label4.GetType())
{
Label theTextlbl = (Label)Controls[i];
g.DrawString(theTextlbl.Text, theTextlbl.Font, Brushes.Black, theTextlbl.Bounds.Left * scalex, theTextlbl.Bounds.Top * scaley, new StringFormat());
}
if (Controls[i].GetType() == this.dateTimePicker1.GetType())
{
DateTimePicker theDate = (DateTimePicker)Controls[i];
g.DrawString(theDate.Text, theDate.Font, Brushes.Black, theDate.Bounds.Left * scalex, theDate.Bounds.Top * scaley, new StringFormat());
}
}
}`