如何在图像上绘制多个矩形? [英] How to draw multiple rectangles on an image?
问题描述
Hello All,
我想根据图像大小在图像上绘制多个矩形,例如,如果我有图像宽度= 733,高度= 501,我想绘制6 * 8矩形,即6行和8列矩形。我该怎么画。
目前我能够绘制多个矩形但没有得到正确的逻辑,这将是更好的方法。
我的尝试:
public Form1()
{
InitializeComponent();
int j = pictureBox1.Location.X;
int k = pictureBox1.Location.Y;
int imagewidth = pictureBox1.Image.Size.Width;
int imageheight = pictureBox1.Image.Size.Height;
System.Drawing.Image img = System.Drawing.Image.FromFile(@E:\ Caledonia.jpg);
MessageBox.Show(Width: + img.Width +,高度:+ img.Height);
var size = pictureBox1.ClientSize;
//获取多个矩形
for(k = 10; k <= 1500; k ++)
{
// for(j = 13; j< = img.Width; j ++)
// {
// j = j - 1;
k = k - 1;
rect = new UserRect(new Rectangle(j,k,200,150));
listRec.Add(rect.rect);
rect.SetPictureBox(this.pictureBox1 );
// i = i + 20;
// j = j + 12;
j = j + 100;
k = k + 100;
//}
}
}
Hello All ,
I want to draw multiple rectangles on an image based on image size , for example if i have image Width=733 , height=501 , and i want to draw 6*8 rectangles i.e 6 rows and 8 column rectangles . how can i draw .
Currently i'm able to draw multiple rectangles but not getting the correct logic , what will be the better way to do that .
What I have tried:
public Form1()
{
InitializeComponent();
int j = pictureBox1.Location.X;
int k = pictureBox1.Location.Y;
int imagewidth = pictureBox1.Image.Size.Width;
int imageheight = pictureBox1.Image.Size.Height;
System.Drawing.Image img = System.Drawing.Image.FromFile(@"E:\Caledonia.jpg");
MessageBox.Show("Width: " + img.Width + ", Height: " + img.Height);
var size = pictureBox1.ClientSize;
// getting multiple rectangles
for (k = 10; k <= 1500; k++)
{
//for (j = 13; j <= img.Width; j++)
//{
// j = j - 1;
k = k - 1;
rect = new UserRect(new Rectangle(j, k, 200, 150));
listRec.Add(rect.rect);
rect.SetPictureBox(this.pictureBox1);
//i = i + 20;
//j = j + 12;
j = j + 100;
k = k + 100;
//}
}
}
推荐答案
我的建议是创建一个具有您想要的行为的自定义PictureBox:
my Suggestion is to create a customized PictureBox with the Behaviour you want to have :
using System;
using System.Windows.Forms;
using System.Drawing;
public class GridPictureBox : PictureBox
{
public Size GridSize
{
get { return my_GridSize; }
set
{
my_GridSize = value;
this.Invalidate();
}
}
private Size my_GridSize = new Size(8, 8);
public Color GridColor
{
get { return my_GridColor; }
set
{
my_GridColor = value;
this.Invalidate();
}
}
private Color my_GridColor = Color.Black;
protected override void OnSizeModeChanged(EventArgs e)
{
this.Invalidate();
base.OnSizeModeChanged(e);
}
protected override void OnSizeChanged(EventArgs e)
{
this.Invalidate();
base.OnSizeChanged(e);
}
protected override void OnPaint(PaintEventArgs pe)
{
Graphics gr = pe.Graphics;
base.OnPaint(pe);
if (Image != null)
{
//get Image-Location and -Size in Picturebox
Point p = default(Point);
Size s = default(Size);
switch (SizeMode)
{
case PictureBoxSizeMode.Normal:
case PictureBoxSizeMode.StretchImage:
case PictureBoxSizeMode.CenterImage:
s = this.Size;
p = new Point(0, 0);
break;
case PictureBoxSizeMode.AutoSize:
s = Image.Size;
p = new Point(0, 0);
break;
case PictureBoxSizeMode.Zoom:
float factor = Math.Max(Convert.ToSingle(Image.Height) / Convert.ToSingle(this.Height), Convert.ToSingle(Image.Width) / Convert.ToSingle(this.Width));
s = new Size(Convert.ToInt32(Convert.ToSingle(Image.Width) / factor), Convert.ToInt32(Convert.ToSingle(Image.Height) / factor));
p = new Point((this.Width - s.Width) / 2, (this.Height - s.Height) / 2);
break;
}
//draw Grid-Lines (vertical)
for (int x = 0; x <= s.Width; x += my_GridSize.Width)
{
gr.DrawLine(new Pen(my_GridColor, 1), x + p.X, p.Y, x + p.X, p.Y + s.Height);
}
//draw Grid-Lines (horizontal)
for (int y = 0; y <= s.Height; y += my_GridSize.Height)
{
gr.DrawLine(new Pen(my_GridColor, 1), p.X, y + p.Y, p.X + s.Width, y + p.Y);
}
}
}
}
这篇关于如何在图像上绘制多个矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!