前回の続きです。
表示サイズにあわせて描画内容を変更しようとするとPaintイベント関連で修正が必要になります。PictureBox1_Paint(object sender, PaintEventArgs e)メソッドはそのままでいいのですが、そのなかで呼び出されるメソッドを変更する必要があります。
まずは引数としてわたされたビットマップを表示するメソッドShowBitmapメソッドを変更します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
public partial class UserControlImage : UserControl { public void ShowBitmap(Graphics g, Bitmap bitmap) { OptimizationScrloolBar(bitmap); Bitmap bitmap1 = GetBitmapDrawPictureBox(bitmap); if(BitmapRectangle != null) { g.DrawImage(bitmap1, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height)); if(blankBitmap != null) { Rectangle rect = blankBitmap.Rectangle; rect.X -= ScrollBarPosX; rect.Y -= ScrollBarPosY; rect.X = (int)(rect.X * DisplayMagnification); rect.Y = (int)(rect.Y * DisplayMagnification); rect.Width = (int)(rect.Width * DisplayMagnification); rect.Height = (int)(rect.Height * DisplayMagnification); g.DrawImage(blankBitmap.Bitmap, rect); } if(blankColorPoints != null) { int x1 = blankColorPoints.Min(x => x.X); int y1 = blankColorPoints.Min(x => x.Y); int x2 = blankColorPoints.Max(x => x.X); int y2 = blankColorPoints.Max(x => x.Y); Bitmap blank = new Bitmap(x2 - x1 + 1, y2 - y1 + 1); foreach(var cp in blankColorPoints) blank.SetPixel(cp.X - x1, cp.Y - y1, cp.Color); Rectangle rect = new Rectangle(x1, y1, x2 - x1 + 1, y2 - y1 + 1); blankBitmap = new BitmapRectangle(blank, rect); blankColorPoints.Clear(); blankColorPoints = null; } { Rectangle rect = BitmapRectangle.Rectangle; rect.X -= ScrollBarPosX; rect.Y -= ScrollBarPosY; rect.X = (int)(rect.X * DisplayMagnification); rect.Y = (int)(rect.Y * DisplayMagnification); rect.Width = (int)(rect.Width * DisplayMagnification); rect.Height = (int)(rect.Height * DisplayMagnification); g.DrawImage(BitmapRectangle.Bitmap, rect); } } else { g.DrawImage(bitmap1, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height)); } } } |
PictureBox.Invalidateメソッドを呼ぶためのInvalidatePictureBoxメソッドを変更します。PictureBox.Invalidateメソッドにわたす引数が変更になります。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public partial class UserControlImage : UserControl { public void InvalidatePictureBox() { if(Bitmap == null) return; int bitmapWidth = (int)(Bitmap.Width * DisplayMagnification); int bitmapHeight = (int)(Bitmap.Height * DisplayMagnification); if(pictureBox1.Width >= bitmapWidth || pictureBox1.Height >= bitmapHeight) pictureBox1.Invalidate(new Rectangle(0, 0, bitmapWidth, bitmapHeight)); else pictureBox1.Invalidate(); } } |
表示サイズが変更になると範囲選択されている部分を矩形で囲んで表示するためのDrawBoderRectangleメソッドも変更が必要です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
public partial class UserControlImage : UserControl { void DrawBoderRectangle(Graphics g) { Rectangle rect; if(BitmapRectangle == null) rect = GetRectangle(startPoint, endPoint); else rect = BitmapRectangle.Rectangle; rect.X -= ScrollBarPosX; rect.Y -= ScrollBarPosY; rect.X = (int)(rect.X * DisplayMagnification); rect.Y = (int)(rect.Y * DisplayMagnification); rect.Width = (int)(rect.Width * DisplayMagnification); rect.Height = (int)(rect.Height * DisplayMagnification); g.DrawRectangle(new Pen(GetBoderBrush()), rect); List<Rectangle> griperRects = new List<Rectangle>(); griperRects.Add(new Rectangle(new Point(rect.X, rect.Y), new Size(GriperSize, GriperSize))); griperRects.Add(new Rectangle(new Point(rect.X + rect.Width / 2 - GriperSize / 2, rect.Y), new Size(GriperSize, GriperSize))); griperRects.Add(new Rectangle(new Point(rect.Right - GriperSize, rect.Y), new Size(GriperSize, GriperSize))); griperRects.Add(new Rectangle(new Point(rect.X, rect.Bottom - GriperSize), new Size(GriperSize, GriperSize))); griperRects.Add(new Rectangle(new Point(rect.X + rect.Width / 2 - GriperSize / 2, rect.Bottom - GriperSize), new Size(GriperSize, GriperSize))); griperRects.Add(new Rectangle(new Point(rect.Right - GriperSize, rect.Bottom - GriperSize), new Size(GriperSize, GriperSize))); griperRects.Add(new Rectangle(new Point(rect.X, rect.Y + rect.Height / 2 - GriperSize / 2), new Size(GriperSize, GriperSize))); griperRects.Add(new Rectangle(new Point(rect.Right - GriperSize, rect.Y + rect.Height / 2 - GriperSize / 2), new Size(GriperSize, GriperSize))); g.FillRectangles(GetBoderBrush(), griperRects.ToArray()); } } |
DrawLine(Graphics g)メソッドは[図形を描画 ⇒ 直線]が選択されているときマウスが移動しているときに描画される確定前の直線の描画をおこなうメソッドです。これも変更が必要です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public partial class UserControlImage : UserControl { void DrawLine(Graphics g) { int x1 = startPoint.X - ScrollBarPosX; int y1 = startPoint.Y - ScrollBarPosY; int x2 = endPoint.X - ScrollBarPosX; int y2 = endPoint.Y - ScrollBarPosY; x1 = (int)(x1 * DisplayMagnification); x2 = (int)(x2 * DisplayMagnification); y1 = (int)(y1 * DisplayMagnification); y2 = (int)(y2 * DisplayMagnification); g.DrawLine(new Pen(DrawColor), x1, y1, x2, y2); } } |
同様に[図形を描画 ⇒ 自由曲線]が選択されているときの処理をするメソッド、DrawFreeLine(Graphics g, List
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
public partial class UserControlImage : UserControl { void DrawFreeLine(Graphics g, List<Point> points, Pen pen) { if(points.Count < 2) return; Point fromPoint = points[0]; Point[] points1 = points.ToArray(); int xScrollBarPos = ScrollBarPosX; int yScrollBarPos = ScrollBarPosY; foreach(Point toPoint in points1) { int x1 = fromPoint.X - xScrollBarPos; int y1 = fromPoint.Y - yScrollBarPos; int x2 = toPoint.X - xScrollBarPos; int y2 = toPoint.Y - yScrollBarPos; x1 = (int)(x1 * DisplayMagnification); x2 = (int)(x2 * DisplayMagnification); y1 = (int)(y1 * DisplayMagnification); y2 = (int)(y2 * DisplayMagnification); g.DrawLine(pen, x1, y1, x2, y2); fromPoint = toPoint; } } } |
DrawRectangle(Graphics g)メソッドは確定前の長方形や楕円を描画するためにマウスが移動しているときに呼び出されるメソッドです。これも変更します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
public partial class UserControlImage : UserControl { void DrawRectangle(Graphics g) { Rectangle rect; if(BitmapRectangle == null) rect = GetRectangle(startPoint, endPoint); else rect = BitmapRectangle.Rectangle; rect.X -= ScrollBarPosX; rect.Y -= ScrollBarPosY; rect.X = (int)(rect.X * DisplayMagnification); rect.Y = (int)(rect.Y * DisplayMagnification); rect.Width = (int)(rect.Width * DisplayMagnification); rect.Height = (int)(rect.Height * DisplayMagnification); if(EditMode == EditMode.DrawRectangle) g.DrawRectangle(new Pen(DrawColor), rect); if(EditMode == EditMode.FillRectangle) g.FillRectangle(new SolidBrush(DrawColor), rect); if(EditMode == EditMode.DrawEllipse) g.DrawEllipse(new Pen(DrawColor), rect); if(EditMode == EditMode.FillEllipse) g.FillEllipse(new SolidBrush(DrawColor), rect); DrawBoderRectangle(g); } } |
フィールド変更用のグリッパーを表示するメソッドです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
public partial class UserControlImage : UserControl { void DrawFieldGrip(Bitmap bitmap, Graphics g) { if(pictureBox1.Image != null) pictureBox1.Image = null; int width = (int)(bitmap.Width * DisplayMagnification); int height = (int)(bitmap.Height * DisplayMagnification); int scrollBarPosX = (int)(ScrollBarPosX * DisplayMagnification); int scrollBarPosY = (int)(ScrollBarPosY * DisplayMagnification); int x = width - scrollBarPosX - GriperSize; int y = height - scrollBarPosY - GriperSize; g.DrawRectangle(new Pen(Color.LightGray), new Rectangle(x, y, GriperSize, GriperSize)); x = width / 2 - scrollBarPosX - GriperSize / 2; y = height - scrollBarPosY - GriperSize; g.DrawRectangle(new Pen(Color.LightGray), new Rectangle(x, y, GriperSize, GriperSize)); x = width - scrollBarPosX - GriperSize; y = height / 2 - scrollBarPosY - GriperSize / 2; g.DrawRectangle(new Pen(Color.LightGray), new Rectangle(x, y, GriperSize, GriperSize)); g.DrawRectangle(new Pen(Color.LightGray), new Rectangle(-1, -1, width - scrollBarPosX + 1, height - scrollBarPosY + 1)); } } |