using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Text; using System.Windows.Forms; public class MainClass { public static void Main() { // Create two new bitmap images Bitmap bmp1 = new Bitmap(100, 100, PixelFormat.Format32bppArgb); Bitmap bmp2 = new Bitmap(100, 100, PixelFormat.Format24bppRgb); // Test for alpha bool b1 = ((bmp1.PixelFormat & PixelFormat.Alpha) != 0); bool b2 = ((bmp2.PixelFormat & PixelFormat.Alpha) != 0); // Output results to console window Console.WriteLine("bmp1 has alpha?: " + b1); Console.WriteLine("bmp2 has alpha?: " + b2); // Clean up bmp1.Dispose(); bmp2.Dispose(); } }
2D Graphics
Draw an array of images
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; public class MainForm : Form { private Bitmap[] myImages = new Bitmap[3]; public MainForm() { myImages[0] = new Bitmap("imageA.bmp"); myImages[1] = new Bitmap("imageB.bmp"); myImages[2] = new Bitmap("imageC.bmp"); } protected void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; int yOffset = 10; foreach (Bitmap b in myImages) { g.DrawImage(b, 10, yOffset, 90, 90); yOffset += 100; } } public static void Main() { Application.Run(new MainForm()); } }
Double buffer with Bitmap
using System; using System.Drawing; using System.Windows.Forms; public class Form1 : Form { Pen p; SolidBrush b, bT = new SolidBrush(Color.Black); string path = "5.bmp"; Image im; Font f; public Form1() { Color cP = Color.Gray; Color cB = Color.LightGray; p = new Pen(cP, 6); b = new SolidBrush(cB); im = Image.FromFile(path); f = new Font(new FontFamily("Times New Roman"), 10); } static void Main() { Application.Run(new Form1()); } protected override void OnPaint(PaintEventArgs pea) { Sketch(); //SketchDBuf(); } private void Sketch() { Graphics g = Graphics.FromHwnd(this.Handle); g.FillRectangle(b, 4, 4, 260, 220); g.DrawRectangle(p, 4, 4, 260, 220); g.DrawImage(im, 33, 35, 200, 145); g.DrawString("AAAAAA", f, bT, 180, 190); g.Dispose(); } private void SketchDBuf() { int hh = 3, w = 260, h = 220; Graphics g; Bitmap bm = new Bitmap(w + 2 * hh, h + 2 * hh); g = Graphics.FromImage(bm); g.FillRectangle(b, hh, hh, w, h); g.DrawRectangle(new Pen(Color.Gray, 2 * hh), hh, hh, w, h); g.DrawImage(im, hh + 30, hh + 32, 200, 145); g.DrawString("Text", f, bT, 180, 190); g = Graphics.FromHwnd(this.Handle); g.DrawImage(bm, 1, 1); g.Dispose(); } }
write the pixel information to the console window
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
protected override void OnPaint(PaintEventArgs e) {
Graphics gForm = e.Graphics;
gForm.FillRectangle(Brushes.White, this.ClientRectangle);
Bitmap bmp = new Bitmap(6, 6);
Graphics gBmp = Graphics.FromImage(bmp);
gBmp.FillRectangle(Brushes.White, 0, 0, bmp.Width, bmp.Height);
gBmp.DrawLine(Pens.Red, 0, 0, 5, 5);
gForm.DrawImage(bmp, 20, 20, bmp.Width, bmp.Height);
for (int y = 0; y < bmp.Height; ++y) { for (int x = 0; x < bmp.Width; ++x) { Color c = bmp.GetPixel(x, y); Console.Write("{0,2:x}{1,2:x}{2,2:x}{3,2:x} ",c.A, c.R, c.G, c.G); } Console.WriteLine(); } bmp.Dispose(); } public static void Main() { Application.Run(new Form1()); } } [/csharp]
Create a green color with an alpha component then draw a green rectangle to the bitmap in memory
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
protected override void OnPaint(PaintEventArgs e) {
Graphics gForm = e.Graphics;
gForm.FillRectangle(Brushes.White, this.ClientRectangle);
for (int i = 1; i <= 7; ++i) {
Rectangle r = new Rectangle(i * 40 - 15, 0, 15,
this.ClientRectangle.Height);
gForm.FillRectangle(Brushes.Orange, r);
}
Bitmap bmp = new Bitmap(260, 260, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Graphics gBmp = Graphics.FromImage(bmp);
Color green = Color.FromArgb(0x40, 0, 0xff, 0);
Brush greenBrush = new SolidBrush(green);
gBmp.FillRectangle(greenBrush, 10, 10, 140, 140);
gForm.DrawImage(bmp, 20, 20, bmp.Width, bmp.Height);
bmp.Dispose();
gBmp.Dispose();
greenBrush.Dispose();
}
public static void Main() {
Application.Run(new Form1());
}
}
[/csharp]
Create a red color with an alpha component then draw a red circle to the bitmap in memory
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
protected override void OnPaint(PaintEventArgs e) {
Graphics gForm = e.Graphics;
gForm.FillRectangle(Brushes.White, this.ClientRectangle);
for (int i = 1; i <= 7; ++i)
{
Rectangle r = new Rectangle(i * 40 - 15, 0, 15,this.ClientRectangle.Height);
gForm.FillRectangle(Brushes.Orange, r);
}
Bitmap bmp = new Bitmap(260, 260,System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Graphics gBmp = Graphics.FromImage(bmp);
Color red = Color.FromArgb(0x60, 0xff, 0, 0);
Brush redBrush = new SolidBrush(red);
gBmp.FillEllipse(redBrush, 70, 70, 160, 160);
gForm.DrawImage(bmp, 20, 20, bmp.Width, bmp.Height);
bmp.Dispose();
gBmp.Dispose();
redBrush.Dispose();
}
public static void Main() {
Application.Run(new Form1());
}
}
[/csharp]
Create a Bitmap image in memory and set its CompositingMode
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
protected override void OnPaint(PaintEventArgs e) {
Graphics gForm = e.Graphics;
gForm.FillRectangle(Brushes.White, this.ClientRectangle);
for (int i = 1; i <= 7; ++i)
{
Rectangle r = new Rectangle(i * 40 - 15, 0, 15,
this.ClientRectangle.Height);
gForm.FillRectangle(Brushes.Orange, r);
}
Bitmap bmp = new Bitmap(260, 260,System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Graphics gBmp = Graphics.FromImage(bmp);
gBmp.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
gForm.DrawImage(bmp, 20, 20, bmp.Width, bmp.Height);
bmp.Dispose();
gBmp.Dispose();
}
public static void Main() {
Application.Run(new Form1());
}
}
[/csharp]