我試圖找到最好的路徑去創建一個像素「網格」,將允許基本的繪畫功能,如通過單擊和移動鼠標着色像素,選擇用於複製,粘貼或移動的區域,或者使用其他圖形功能來爲像素呈現文本或形狀。我已經看了一些例子,比如this example,它覆蓋了面板控制,並且與我試圖實現的效果類似,但是繪畫很慢,看起來它不能很好地用於繪製老鼠。是否有控制權,或者我可以重寫的控制權,這將允許我尋找的功能?C#爲繪畫像素和渲染文本創建網格
以下是對上面的例子看起來像一個示例: Sample pixel grid
而且我適應從上面的例子代碼:
public class Pixel
{
public Rectangle Bounds { get; set; }
public bool IsOn { get; set; }
public bool IsSelected { get; set; }
}
public class PixelGridControl : Panel
{
public int Columns = 99;
public int Rows = 63;
private readonly Pixel[,] pixels;
public PixelGridControl()
{
this.DoubleBuffered = true;
this.ResizeRedraw = true;
// initialize pixel grid:
pixels = new Pixel[Columns, Rows];
for (int y = 0; y < Rows; ++y)
{
for (int x = 0; x < Columns; ++x)
{
pixels[x, y] = new Pixel();
}
}
}
// adjust each column and row to fit entire client area:
protected override void OnResize(EventArgs e)
{
int top = 0;
for (int y = 0; y < Rows; ++y)
{
int left = 0;
int height = (this.ClientSize.Height - top)/(Rows - y);
for (int x = 0; x < Columns; ++x)
{
int width = (this.ClientSize.Width - left)/(Columns - x);
pixels[x, y].Bounds = new Rectangle(left, top, width, height);
left += width;
}
top += height;
}
base.OnResize(e);
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
for (int y = 0; y < Rows; ++y)
{
for (int x = 0; x < Columns; ++x)
{
if (pixels[x, y].IsOn)
{
e.Graphics.FillRectangle(Brushes.Gold, pixels[x, y].Bounds);
}
else
{
ControlPaint.DrawButton(e.Graphics, pixels[x, y].Bounds,
ButtonState.Normal);
}
}
}
base.OnPaint(e);
}
// determine which button the user pressed:
protected override void OnMouseDown(MouseEventArgs e)
{
for (int y = 0; y < Rows; ++y)
{
for (int x = 0; x < Columns; ++x)
{
if (pixels[x, y].Bounds.Contains(e.Location))
{
pixels[x, y].IsOn = true;
this.Invalidate();
MessageBox.Show(
string.Format("You pressed on button ({0}, {1})",
x.ToString(), y.ToString())
);
}
}
}
base.OnMouseDown(e);
}
}
爲什麼說GDI +速度慢,任何測量?你知道[Double Buffered Graphics](https://msdn.microsoft.com/en-us/library/b367a457(v = vs.110).aspx)嗎? –
我並不是想說GDI +很慢,只是我在鏈接的示例中提到的實現。我編輯了這篇文章,並添加了我正在測試的代碼,該代碼也使用了您指的DoubleBuffered圖形。在點擊鼠標按鈕和矩形區域的實際外觀變化顏色之間,大約需要2秒。我試圖找出是否有不同的控制方式或不同的方式來實現這一點,這將提供流暢的體驗而不會有任何延遲。 – Rob
你可以提交演示測試項目到** github **供其他人測試嗎? –