这是一个老问题,如果其他人也有类似的问题。见下文。首先让我们检查一下 Ops 代码。
(1) 查看代码:建议的第一个更改是保持 Pen 的格式简单,直到我们更好地了解 Pen 在绘制图形时的实际工作原理。看看我们从图像创建图形的 Op 行,这是一个很好的例子,说明如何通过使用位图的图形上下文直接绘制(“这意味着写入”)到提供的位图。接下来,Op 提供了一个很好的 Graphics DrawLine 方法示例,该方法可以将定义的线绘制到提供的位图上。
(2) 由于缺少细节,我们必须对 Op 提供的位图及其绘制位图的方法做出以下假设。假设这个pictureBox1中已经存在一个图像;如果未设置图像,我们从图像中获取的图形将来自空图像,或者每个像素将是黑色的,就像脚注一样:
(a) Pen 的颜色对于现有位图是唯一的,并且颜色的 alpha 分量足够高,可以在绘制时实际看到生成的颜色(如有疑问,请使用唯一的纯色或者至少将 alpha 通道 设置为 255)?
(b) Op 想要绘制的这条线从 Left 3、Top 3 到 Left 133 开始,即 3 像素在位图左侧的右侧,这条线的高度为 133,因此出于演示目的,Pen 的线大小已更改为宽度 = 3。
(c) 最后考虑,pictureBox1.Size 是否足以让我们看到这条画线?线的几何形状形成一个类似于 RectangleF(3, 3, 3, 133) 结构的矩形,因此如果图片框1 Bounds rectangle 与派生线的矩形相交,则该 intersection 是可以绘制线条并视为可见的位置。
在我们可以从图形绘制到pictureBox1 图像之前,我们必须首先将pictureBox1 图像数据转换回可用的图像类型,例如位图。原因是图片框仅以数组格式存储像素数据,如果不转换为图像类型,GDI/GDI+ 不能直接使用。 bitamp、jpeg、png 等。
如果您通过 自定义用户控件 和正确处理 PaintEventArgs OnPaint 实现和/或使用相关的图形屏幕缓冲区上下文场景。
对于那些只想知道缺少什么的人:
private void button1_Click(Object sender, EventArgs e)
{
Pen selPen = new Pen(Color.Red, 2); // The Op uses random color which is not good idea for testing so we'll choose a solid color not on the existing bitmap and we'll confine our Pen's line size to 2 until we know what we're doing.
// Unfortionately the picture box "image" once loaded is not directly usable afterwords.
// We need tp recreate the pictureBox1 image to a usable form, being the "newBmp", and for efficiency the bitmap type is chosen
Bitmap newBmp = new Bitmap(pictureBox1.Width, pictureBox1.Height, PixelFormat.Format32bppArgb); // Tip: Using System.Drawing.Imaging for pixel format which uses same pixel format as screen for speed
// We create the graphics from our new and empty bitmap container
Graphics g = Graphics.FromImage(newBmp);
// Next we draw the pictureBox1 data array to our equivelent sized bitmap container
g.DrawImage(pictureBox1.Image, 0, 0);
g.DrawLine(selPen, 3, 3, 3, 133); // Format: (pen, x1, y1, x2, y2)
pictureBox1.Image = newBmp;
// Don't forget to dispose of no longer needed resources
g.Dispose();
selPen.Dispose();
newBmp.Dispose(); // or save newBmp to file before dispose ie. newBmp.Save("yourfilepath", ImageFormat.Jpeg) or in whatever image type you disire;
}
到目前为止,Op 的代码只在位图表面画一条线,如果我们要“看到”这种变化,我们必须将 位图保存到文件 以便稍后在图像查看器中查看,或者我们必须将更新后的位图绘制到我们的显示监视器屏幕。
有几种方法可以绘制到显示器的屏幕上。可以使用的最常见的图形上下文是 Control.CreateGraghics、来自 (PaintEventArgs) 的图形到屏幕方法和/或通过使用有时称为并用作手动双缓冲图形上下文,其中全部通过图形中的DrawImage方法实现。
在本例中,基于 Op 自己的代码,最简单的解决方案是使用 pictureBox1 控件显示这个新更新的位图。我们将简单地使用新更新的位图更新控件的图像,当然,一旦第一次转换为使用图形图像,如上面的代码描述中所示。
编码愉快!