2012-04-29 7 views
2

VB.NETフォームの個々のピクセルの色を変更するにはどうすればよいですか?VB.netフォームのピクセルカラーを変更しますか?

ありがとうございました。

+0

ここではっきりしてください。なぜ個々のピクセルを変更したいのですか? – Coffee

+0

[スタックオーバーフローは個人研究の助手ではありません](http://meta.stackexchange.com/a/128553/140505) – Oded

+0

マンデルブロセットを表示するプログラムを作成しています。個人の色を変更する必要があります各ピクセルはグラフ上の点を表し、したがってそれ自身の色を持つので、フォーム上のピクセル。 – SiliconCelery

答えて

2

をWinformsのためのハード要件は、Windowsがそれを要求したときにフォームを再描画することができるはずです。ウィンドウを最小化して復元すると、どのようなことが起こりますか?または、古いバージョンのWindowsで別のウィンドウを移動したとき。

このように、ウィンドウのピクセルを設定するだけでは不十分です。ウィンドウを再描画するときにピクセルをすべて失うことになります。代わりに、ビットマップを使用します。追加の負担は、ユーザーインターフェイスの応答性を維持して、ワーカースレッドで計算を行う必要があるということです。 BackgroundWorkerはその権利を得るのに便利です。

これを行う1つの方法は、ワーカーに入力するビットマップと表示するビットマップの2つのビットマップを使用することです。ピクセルの1行ごとに、作業中のビットマップのコピーが作成され、ReportProgress()に渡されます。 ProgressChangedイベントは、古いビットマップを破棄して、渡された新しいビットマップを保存し、Invalidateを呼び出して再描画を強制します。

0

あなたはこれらのリソースの恩恵を受ける可能性がありますSetting background color of a form

DeveloperFusion forum、およびextracting pixel color

+1

ご協力いただきありがとうございますが、本当に必要なのは、ピクセルの色を変更することではなく、色を変更することです。私が知っている唯一の解決策は、画面上のすべてのピクセルの画像ボックスを作成し、それらの色を変更することですが、それは本当に非効率的であると私を襲います。 – SiliconCelery

0

ここにいくつかのデモコードがあります。ハンスが言及した理由のため、塗り直すのは遅いです。速度を上げる簡単な方法は、遅れてビットマップを再計算することだけです。

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 
関連する問題