在VB.net窗体中更改像素颜色?

在VB.net窗体中更改像素颜色?,vb.net,colors,pixel,Vb.net,Colors,Pixel,如何在VB.NET窗体中更改单个像素的颜色 谢谢。您可能会从以下资源中受益: ,并且Winforms的一个硬要求是,您应该能够在Windows要求时重新绘制表单。这将在最小化和恢复窗口时发生。或者在旧版本的Windows上,当您在您的窗口上移动另一个窗口时 因此,仅仅在窗口上设置像素是不够的,当窗口重新绘制时,您将丢失所有像素。而是使用位图。另一个负担是,您必须保持用户界面的响应性,因此需要在工作线程上进行计算。后台工作人员很容易做到这一点 一种方法是使用两个位图,一个用于填充辅助对象,另一个用

如何在VB.NET窗体中更改单个像素的颜色


谢谢。

您可能会从以下资源中受益:


,并且

Winforms的一个硬要求是,您应该能够在Windows要求时重新绘制表单。这将在最小化和恢复窗口时发生。或者在旧版本的Windows上,当您在您的窗口上移动另一个窗口时

因此,仅仅在窗口上设置像素是不够的,当窗口重新绘制时,您将丢失所有像素。而是使用位图。另一个负担是,您必须保持用户界面的响应性,因此需要在工作线程上进行计算。后台工作人员很容易做到这一点


一种方法是使用两个位图,一个用于填充辅助对象,另一个用于显示。例如,每一行像素复制一份工作位图,并将其传递给ReportProgress()。ProgressChanged事件然后处理旧位图并存储新传递的位图,并调用Invalidate强制重新绘制。

以下是一些演示代码。由于汉斯提到的原因,重新粉刷很慢。一个简单的加速方法是只在延迟后重新计算位图

Public Class Form1

  Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    'create new bitmap

    If Me.ClientRectangle.Width <= 0 Then Exit Sub
    If Me.ClientRectangle.Height <= 0 Then Exit Sub

    Using bmpNew As New Bitmap(Me.ClientRectangle.Width, Me.ClientRectangle.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
      'draw some coloured pixels
      Using g As Graphics = Graphics.FromImage(bmpNew)
        For x As Integer = 0 To bmpNew.Width - 1
          For y As Integer = 0 To bmpNew.Height - 1
            Dim intR As Integer = CInt(255 * (x / (bmpNew.Width - 1)))
            Dim intG As Integer = CInt(255 * (y / (bmpNew.Height - 1)))
            Dim intB As Integer = CInt(255 * ((x + y) / (bmpNew.Width + bmpNew.Height - 2)))
            Using penNew As New Pen(Color.FromArgb(255, intR, intG, intB))
              'NOTE: when the form resizes, only the new section is painted, according to e.ClipRectangle.
              g.DrawRectangle(penNew, New Rectangle(New Point(x, y), New Size(1, 1)))
            End Using
          Next y
        Next x
      End Using
      e.Graphics.DrawImage(bmpNew, New Point(0, 0))
    End Using

  End Sub

  Private Sub Form1_ResizeEnd(sender As Object, e As System.EventArgs) Handles Me.ResizeEnd
    Me.Invalidate() 'NOTE: when form resizes, only the new section is painted, according to e.ClipRectangle in Form1_Paint(). We invalidate the whole form here to form an  entire form repaint, since we are calculating the colour of the pixel from the size of the form. Try commenting out this line to see the difference.
  End Sub

End Class
公共类表单1
私有子表单1_Paint(发送方作为对象,e作为System.Windows.Forms.PaintEventArgs)处理Me.Paint
'创建新位图

如果是Me.ClientRectangle.Width,你能在这里说得更清楚些吗?你为什么要更改单个像素?我正在制作一个显示Mandelbrot集合的程序,我需要更改窗体上单个像素的颜色,因为每个像素代表图形上的一个点,因此有自己的颜色。哇,别那么咄咄逼人了。我已经尽可能多地在谷歌上搜索了,大多数结果都有助于检测单个像素的颜色,而我发现设置这些颜色的少数结果只会导致语法错误。我知道这不是一个特别高级的编程问题,但我只是希望有人至少能给我指出正确的方向。谢谢你的帮助,但我真正需要的是改变像素的颜色,而不是得到它们的颜色。我所知道的唯一解决方案是为屏幕上的每个像素创建一个图片框,并更改它们的颜色,但这让我觉得效率很低。