2013-07-22 37 views
0

我正在製作自定義顏色對話框。它看起來像這樣Color dialog 請注意,在紅色矩形中,我在底部彩虹的mouse_move事件上繪製了這個色調幻燈片。 我的問題是繪圖速度。這有點慢。有沒有另一種方法來繪製這個面板? 這裏是我的代碼:繪製HSL空間的色調幻燈片的最快方法

// picColorRuler is the rainbow below 
    // picColorArea is the panel with red border in the attached image 


    // Change the target location. 
    private void picColorRuler_MouseMove(object sender, MouseEventArgs e) 
    { 
     if (e.Button == MouseButtons.Left) 
     { 
      rulerColorTarget.Left = e.X; 
      if (rulerColorTarget.Left < 0) rulerColorTarget.Left = 0; 
      if (rulerColorTarget.Left > picColorRuler.ClientSize.Width - 1) 
      { 
       rulerColorTarget.Left = picColorRuler.ClientSize.Width - 1; 
      } 
      picColorRuler.Refresh(); 
     } 
    } 

    // Painting the rainbow. 
    private void picColorRuler_Paint(object sender, PaintEventArgs e) 
    { 
     // Draw the target. 
     rulerColorTarget.Color = rulerImg.GetPixel(rulerColorTarget.Left, rulerColorTarget.Top); 
     rulerColorTarget.Draw(e.Graphics); 
     // Change the picColorArea's colors. 
     PaintPicColorArea(); 
    } 

    private void PaintPicColorArea() 
    { 
     var width = picColorArea.ClientSize.Width; 
     var height = picColorArea.ClientSize.Height; 
     var satSeed = 1f/width; 
     var lightSeed = 1f/height; 
     var sat = 0f; 
     for (var x = 0; x < width; ++x) 
     { 
      if (x == width - 1) sat = 1; 
      var light = 0f; 
      for (var y = 0; y < height; ++y) 
      { 
       if (y == height - 1) light = 1; 
       var sc = Util.Hsl2Rgb(rulerColorTarget.Color.GetHue(), sat, light); 
       areaImg.SetPixel(x, y, sc); 
       light += lightSeed; 
      } 
      sat += satSeed; 
     } 
     picColorArea.BackgroundImage = null; 
     picColorArea.BackgroundImage = areaImg; 
    } 

回答

0

儘量避免位圖圖像上得到&設置像素,這是出了名的慢。

我個人使用FastBitmap,但還有其他方法可以做到這一點: Bitmap.SetPixel(x,y, Color) too slow

+0

謝謝Carra,我使用FastBitmap,現在性能更好。 – SmartGoat