我有一个绘图程序,显示正在编辑的图像的放大版本,我将插值模式设置为“最近邻居”。此外,我有一个像素网格,在那里绘制,网格完全在每个地方,图像上有一个像素。因此它会向您显示准确的像素数量。 我添加了一个画笔在图片框上绘制,但我希望画笔精确地绘制在正方形上,就像在其他具有像素网格视图的绘图程序中一样。因此,只要鼠标指针位于该像素方块内,它就只会填充它所在的像素方块的内部。以下是代码: (我的图片框名为canvaseditor。)
生成像素网格的代码:
Private Sub canvaseditor_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles canvaseditor.Paint
If Not bmpNew Is Nothing Then
e.Graphics.DrawImage(bmpNew, 0, 0)
End If
Dim g As Graphics = e.Graphics
Dim pn As New Pen(Color.DimGray) '~~~ color of the lines
Dim x As Integer
Dim y As Integer
Dim intSpacing As Integer = 8 '~~~ spacing between adjacent lines
'~~~ Draw the horizontal lines
x = canvaseditor.Width
For y = 0 To canvaseditor.Height Step intSpacing
g.DrawLine(pn, New Point(0, y), New Point(x, y))
Next
'~~~ Draw the vertical lines
y = canvaseditor.Height
For x = 0 To canvaseditor.Width Step intSpacing
g.DrawLine(pn, New Point(x, 0), New Point(x, y))
Next
End Sub
这是插值代码: (TL14只是众多图片盒中的一个,其中包含将在canvaseditor中编辑的原始图像,而scalefactor只是一个设置为1的变量整数)
If TL14.Image Is Nothing Then
Else
canvaseditor.Image = Nothing
canvaseditor.Image = TL14.Image
canvaseditor.Width = TL14.Width * 8 + 1
canvaseditor.Height = TL14.Height * 8 + 1
'load and draw the image(s) once
BackgroundImage1 = New Bitmap(TL14.Image)
bmpNew = New Bitmap(canvaseditor.Width * scaleFactor, canvaseditor.Height * scaleFactor)
Using g As Graphics = Graphics.FromImage(bmpNew)
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half
g.DrawImage(BackgroundImage1, 0, 0, bmpNew.Width, bmpNew.Height)
End Using
canvaseditor.Focus()
GroupBox13.Focus()
End If
这允许我点击原始TL14图片框并将图像加载到canvaseditor图片框中,将其缩放到更大的尺寸,绘制像素网格,并在图像拉伸时带走抗锯齿。所以你最终得到了这个:
This is what it does when I draw on it
但是当我画上它时,它并没有完全排列在正方形内部。它只是绘制我的鼠标所在的位置,所以如何限制它呢?