Let us look at a simple C# Windows Forms HelloWorldPaint example.
Example 1: HelloWorldPaint
This example will comprise the following files:
HelloWorldPaint.cs
Step 1: Create Project
- The first step is to create a C# Project.
- Go to
FILE-->New-->Project
to create a new project.
Step 2: Write Code
Write Code as follows:
*(a). HelloWorldPaint.cs
Create a file named HelloWorldPaint.cs
Here is the full code
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Examples {
class Form1 : Form {
public static void Main() {
Application.Run(new Form1());
}
public Form1() {
this.panel.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right;
this.panel.BackColor = SystemColors.Window;
this.panel.Bounds = new Rectangle(20, 20, 290, 60);
this.panel.Font = new Font(this.panel.Font.FontFamily.Name, 22.0f);
this.panel.Paint += new PaintEventHandler(this.OnPanelPaint);
this.panel.SizeChanged += new EventHandler(this.OnPanelSizeChanged);
this.textBox.Anchor = AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right;
this.textBox.Location = new Point(20, 90);
this.textBox.Text = "Hello, World !";
this.textBox.Width = 290;
this.timer.Interval = 60;
this.timer.Tick += new EventHandler(this.OnTimer);
this.timer.Enabled = true;
this.ClientSize = new Size(330, 130);
this.Controls.AddRange(new Control[] {this.panel, this.textBox});
this.Text = "Wiggly";
}
protected void OnPanelPaint(object sender, PaintEventArgs e) {
Point pos = new Point((e.ClipRectangle.Size.Width - (int)e.Graphics.MeasureString(this.textBox.Text, this.panel.Font).Width) / 2, (e.ClipRectangle.Size.Height - (int)e.Graphics.MeasureString(this.textBox.Text, this.panel.Font).Height) / 2);
for (int i = 0; i < this.textBox.Text.Length; i++) {
int index = (this.step + i) % sin.Length;
e.Graphics.DrawString(this.textBox.Text[i].ToString(), this.panel.Font, new SolidBrush(HsbToRgb((float)(15 - index) * 16 / 255 * 360, 1.0f, 0.75f)), new Point(pos.X, pos.Y + sin[index] * (e.ClipRectangle.Height - this.panel.Font.Height) / 400));
pos.X += (int)e.Graphics.MeasureString(string.Format("{0}", this.textBox.Text[i]), this.panel.Font).Width - 9;
}
}
protected void OnPanelSizeChanged(object sener, EventArgs e) {
this.panel.Refresh();
}
protected void OnTimer(object sender, EventArgs e) {
this.step++;
this.panel.Refresh();
}
private static readonly int[] sin = { 0, 38, 71, 92, 100, 92, 71, 38, 0, -38, -71, -92, -100, -92, -71, -38 };
private Panel panel = new Panel();
private TextBox textBox = new TextBox();
private Timer timer = new Timer();
private int step = 0;
private static Color HsbToRgb(float hue, float saturation, float brightness) {
if (saturation == 0)
return Color.FromArgb(255, (byte)(brightness * 255.0), (byte)(brightness * 255.0), (byte)(brightness * 255.0));
hue = hue == 360 ? 0 : hue / 60;
float f = hue - (float)Math.Truncate(hue);
float p = brightness * (1.0f - saturation);
float q = brightness * (1.0f - (saturation * f));
float t = brightness * (1.0f - (saturation * (1.0f - f)));
switch ((int)Math.Truncate(hue)) {
case 0: return Color.FromArgb(255, (byte)(brightness * 255.0), (byte)(t * 255.0), (byte)(p * 255.0));
case 1: return Color.FromArgb(255, (byte)(q * 255.0), (byte)(brightness * 255.0), (byte)(p * 255.0));
case 2: return Color.FromArgb(255, (byte)(p * 255.0), (byte)(brightness * 255.0), (byte)(t * 255.0));
case 3: return Color.FromArgb(255, (byte)(p * 255.0), (byte)(q * 255.0), (byte)(brightness * 255.0));
case 4: return Color.FromArgb(255, (byte)(t * 255.0), (byte)(p * 255.0), (byte)(brightness * 255.0));
default: return Color.FromArgb(255, (byte)(brightness * 255.0), (byte)(p * 255.0), (byte)(q * 255.0));
}
}
}
}
Run
Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj
project, build and run.
Reference
Download the code using the below links:
Number | Link |
---|---|
1. | Download Example |
2. | Follow code author |
Example 2: PaintEvent
This example will comprise the following files:
PaintEvent.cs
Step 1: Create Project
- The first step is to create a C# Project.
- Go to
FILE-->New-->Project
to create a new project.
Step 2: Write Code
Write Code as follows:
*(a). PaintEvent.cs
Create a file named PaintEvent.cs
Here is the full code
using System.Drawing;
using System.Windows.Forms;
namespace Examples {
class Form1 : Form {
// The main entry point for the application.
public static void Main() {
Application.Run(new Form1());
}
public Form1() {
this.Controls.Add(this.panel1);
this.ClientSize = new Size(300, 300);
this.panel1.Location = new Point(10, 10);
this.panel1.BorderStyle = BorderStyle.Fixed3D;
this.panel1.Size = new Size(280, 280);
this.panel1.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
this.panel1.Paint += delegate(object sender, PaintEventArgs e) {
e.Graphics.FillRectangle(brush1, e.ClipRectangle);
pen1.DashPattern = new float[] {1.0f, 1.0f, 2.0f, 1.0f, 4.0f, 1.0f, 2.0f, 1.0f};
pen1.DashCap = System.Drawing.Drawing2D.DashCap.Flat;
pen1.EndCap = System.Drawing.Drawing2D.LineCap.Round;
e.Graphics.DrawRectangle(pen1, 40, 40, 200, 200);
};
}
private Panel panel1 = new Panel();
private static TextureBrush brush1 = new TextureBrush(Image.FromFile("../../texture.jpg"));
private static Pen pen1 = new Pen(new System.Drawing.Drawing2D.LinearGradientBrush(new Point(0, 0), new Point(100, 0), Color.Red, Color.LightPink), 10.0f);
}
}
Run
Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj
project, build and run.
Reference
Download the code using the below links:
Number | Link |
---|---|
1. | Download Example |
2. | Follow code author |
Example 3: Painting
This example will comprise the following files:
Painting.cs
Step 1: Create Project
- The first step is to create a C# Project.
- Go to
FILE-->New-->Project
to create a new project.
Step 2: Write Code
Write Code as follows:
*(a). Painting.cs
Create a file named Painting.cs
Here is the full code
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Examples {
class Form1 : Form {
public static void Main() {
Application.EnableVisualStyles();
Application.Run(new Form1());
}
public Form1() {
Text = "Painting example";
ClientSize = new Size(640, 480);
panelColorsContainer.BorderStyle = BorderStyle.FixedSingle;
panelColorsContainer.Location = new Point(10, 10);
panelColorsContainer.ClientSize = new Size(512, 32);
panelColorsContainer.Parent = this;
buttonClear.Parent = this;
buttonClear.Location = new Point(542, 15);
buttonClear.Text = "Clear";
buttonClear.Click += delegate {
picture = new Bitmap(pictureWidth, pictureHeight);
panelPainting.Invalidate(false);
};
trackBarZoom.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
trackBarZoom.AutoSize = false;
trackBarZoom.Location = new Point(10, 50);
trackBarZoom.TickStyle = TickStyle.None;
trackBarZoom.SetRange(1, 50);
trackBarZoom.Value = zoom;
trackBarZoom.Parent = this;
trackBarZoom.Size = new Size(ClientSize.Width - 20, 25);
trackBarZoom.ValueChanged += delegate {
zoom = trackBarZoom.Value;
panelPainting.Invalidate(false);
};
panelPainting.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right;
panelPainting.BackColor = Color.WhiteSmoke;
panelPainting.BorderStyle = BorderStyle.FixedSingle;
panelPainting.Location = new Point(10, 82);
panelPainting.Parent = this;
panelPainting.Size = new Size(620, 388);
foreach (Color color in new Color[] { Color.DarkMagenta, Color.DarkCyan, Color.Brown, Color.DarkBlue, Color.DarkGreen, Color.DarkRed, Color.Gray, Color.DarkGray, Color.Magenta, Color.Cyan, Color.Yellow, Color.Blue, Color.Green, Color.Red, Color.White, Color.Black }) {
Panel panelColor = new Panel { Parent = panelColorsContainer, Size = new Size(32, 32), BackColor = color, Dock = DockStyle.Left };
panelColor.Click += this.ChooseCurrentColor;
panelColors.Add(panelColor);
}
panelPainting.MouseDown += delegate (object sender, MouseEventArgs e) {
picture.SetPixel(e.X / zoom, e.Y / zoom, e.Button == MouseButtons.Left ? currentColor : Color.Empty);
panelPainting.Invalidate(new Rectangle(e.X / zoom * zoom, e.Y / zoom * zoom, zoom, zoom), false);
};
panelPainting.MouseMove += delegate (object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left && e.X >= 0 && e.X < panelPainting.ClientSize.Width && e.Y >= 0 && e.Y < panelPainting.ClientSize.Height) {
picture.SetPixel(e.X / zoom, e.Y / zoom, currentColor);
panelPainting.Invalidate(new Rectangle(e.X / zoom * zoom, e.Y / zoom * zoom, zoom, zoom), false);
}
};
panelPainting.Paint += delegate (object sender, PaintEventArgs e) {
for (int x = 0; x < panelPainting.ClientSize.Width; x += zoom) {
for (int y = 0; y < panelPainting.ClientSize.Height; y += zoom) {
if (!picture.GetPixel(x, y).IsEmpty) {
SolidBrush solidBrush = new SolidBrush(picture.GetPixel(x / zoom, y / zoom));
e.Graphics.FillRectangle(solidBrush, x, y, zoom, zoom);
solidBrush.Dispose();
}
}
}
if (zoom > 3) {
Pen pen = new Pen(Color.LightBlue, 1);
for (int index = 0; index < panelPainting.ClientSize.Width; index += zoom)
e.Graphics.DrawLine(pen, index, 0, index, panelPainting.ClientSize.Height);
for (int index = 0; index < panelPainting.ClientSize.Height; index += zoom)
e.Graphics.DrawLine(pen, 0, index, panelPainting.ClientSize.Width, index);
pen.Dispose();
}
};
}
private void ChooseCurrentColor(object sender, EventArgs e) {
foreach (var panel in panelColors)
panel.BorderStyle = panel.Handle != (sender as Control).Handle? BorderStyle.None : BorderStyle.FixedSingle;
currentColor = (sender as Control).BackColor;
}
private static int pictureWidth = 5000;
private static int pictureHeight = 5000;
private static int zoom = 20;
private Color currentColor = Color.Black;
private Bitmap picture = new Bitmap(pictureWidth, pictureHeight);
private Panel panelColorsContainer = new Panel();
private System.Collections.Generic.List<Panel> panelColors = new System.Collections.Generic.List<Panel>();
private Button buttonClear = new Button();
private TrackBar trackBarZoom = new TrackBar();
private Panel panelPainting = new Panel();
}
}
Run
Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj
project, build and run.
Reference
Download the code using the below links:
Number | Link |
---|---|
1. | Download Example |
2. | Follow code author |