1366721のpictureBox1に15040のサイズでランダムな座標で線を99回描いたものをタイル状に並べて描画します。
trackBar1のScrollイベントによって描画します。
(もしも描画中に追加で描画しようとした時はキャンセル)
それの描画に要した時間をlabel1に表示します。
100msほどかかります。
見た目にはカクカクしています。
これってもうこれ以上速くならないんでしょうか?
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; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } int seed = 10000; bool drawBusy = false; private void drawDemand() { if (drawBusy) return; drawBusy = true; System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); draw(pictureBox1); //draw(pictureBox2); sw.Stop(); double time = sw.ElapsedTicks / (double)System.Diagnostics.Stopwatch.Frequency * 1000.0; label1.Text = time.ToString(); label1.Refresh(); drawBusy = false; } private void draw(PictureBox tgtPicBox) { int w = tgtPicBox.Width; int h = tgtPicBox.Height; int miniW = 150; int miniH = 40; Bitmap big = new Bitmap(w, h); Graphics g = Graphics.FromImage(big); for (int y = 0; y < h; y += miniH) { for (int x = 0; x < w; x += miniW) { seed++; g.DrawImage(minibitmapGet(miniW, miniH, seed), x, y, miniW, miniH); } } tgtPicBox.Image = big; tgtPicBox.Refresh(); g.Dispose(); } private Bitmap minibitmapGet(int width, int height,int seed) { Bitmap rslt = new Bitmap(width,height); System.Random r = new System.Random(seed); int[] xRan = new int[100]; for (int i = 0; i < xRan.Length; i++) { xRan[i] = r.Next(width); } int[] yRan = new int[100]; for (int i = 0; i < yRan.Length; i++) { yRan[i] = r.Next(height); } Graphics g = Graphics.FromImage(rslt); //g.DrawLines(Pens.Black, ps); for (int i = 0; i < 99; i++) { g.DrawLine(Pens.Black, xRan[i], yRan[i], xRan[i+1], yRan[i+1]); } g.Dispose(); return rslt; } private void trackBar1_Scroll(object sender, EventArgs e) { drawDemand(); } } }
回答3件
あなたの回答
tips
プレビュー