24ビットBitmapでのShape描画例
using System; using System.Drawing; using System.Drawing.Imaging; using System.Windows.Forms; using ImageUtils; namespace ShapeTestApp { public partial class MainForm : Form { int drawType = 0; public MainForm() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { int w = ClientSize.Width; int h = ClientSize.Height; int xmin = -100; int xmax = w + 100; int ymin = -100; int ymax = h + 100; Bitmap bmp = new Bitmap(w, h, PixelFormat.Format24bppRgb); Shape24 shp = new Shape24(bmp); Random rnd = new Random(); for (int i = 0; i < 100; i++) { shp.Color = ColorTranslator.FromWin32(rnd.Next(1 << 25)); Point pt = new Point(rnd.Next(xmin, xmax), rnd.Next(ymin, ymax)); Size sz = new Size(rnd.Next(w / 3), rnd.Next(h / 3)); Rectangle rect = new Rectangle(pt, sz); switch (drawType) { case 0: if (rnd.Next(2) == 0) sz.Width = -sz.Width; if (rnd.Next(2) == 0) sz.Height = -sz.Height; shp.DrawLine(pt, pt + sz); break; case 1: shp.DrawRectangle(rect); break; case 2: shp.FillRectangle(rect); break; case 3: shp.DrawEllipse(rect); break; default: shp.FillEllipse(rect); break; } } shp.Dispose(); Graphics g = CreateGraphics(); g.DrawImage(bmp, 0, 0); g.Dispose(); bmp.Dispose(); if (++drawType >= 5) drawType = 0; } } }
なお、Shape.csはImageUtils.csと併用するように作ったのでImageUtils namespaceに所属するようにした。