2012-08-30 94 views
0

我寫了一個小型顏色選擇器代碼我自己,它運作良好。然而這個問題,當我移動滑塊來獲得所選擇的顏色(RGB)的畫布(白黑和選定的顏色漸變花時間的延遲是因爲代碼: enter image description here顏色選擇器梯度圖

的代碼如下:

Try 
    'Dim converter As New SlidertoColor 
    'Dim cornercolor As Color = converter.Convert(MySlider.Value, Nothing, Nothing, Nothing) 

    Dim cornercolor As Color = Value 
    Dim y As Integer = 0 
    Dim x As Integer = 0 


    Dim wb As New WriteableBitmap(256, 256, 96, 96, PixelFormats.Pbgra32, Nothing) 
    Dim stride As Integer = CInt((wb.PixelWidth * wb.Format.BitsPerPixel)/8) 

    Dim RD As Decimal = cornercolor.R/255 
    Dim GD As Decimal = cornercolor.G/255 
    Dim BD As Decimal = cornercolor.B/255 

    Dim r As Decimal = cornercolor.R 
    Dim g As Decimal = cornercolor.G 
    Dim b As Decimal = cornercolor.B 

    Dim lr As New List(Of Decimal) 
    Dim lb As New List(Of Decimal) 
    Dim lg As New List(Of Decimal) 

    Try 
     For i = 0 To 255 
      lr.Add(r) 
      r = r - RD 
      lg.Add(g) 
      g = g - GD 
      lb.Add(b) 
      b = b - BD 
     Next 

    Catch ex As Exception 

    End Try 

    r = cornercolor.R 
    g = cornercolor.G 
    b = cornercolor.B 

    'For y = 255 To 0 Step -1 
    For y = 0 To 255 
     RD = ((255 - y) - lr(y))/255 
     GD = ((255 - y) - lg(y))/255 
     BD = ((255 - y) - lb(y))/255 

     If RD < 0 Then 
      RD = RD * -1 
     End If 
     If GD < 0 Then 
      GD = GD * -1 
     End If 
     If BD < 0 Then 
      BD = BD * -1 
     End If 

     'Need to work on this section 
     r = 255 - y 
     g = 255 - y 
     b = 255 - y 


     For x = 0 To 255 
      Try 
       Dim colorData As Byte() = { 
       CByte(b), 
       CByte(g), 
       CByte(r), 
       CByte(255)} 

       Dim rect As New Int32Rect(x, y, 1, 1) 
       wb.WritePixels(rect, colorData, stride, 0) 

       r = r - RD 
       g = g - GD 
       b = b - BD 

       If r < 0 Then 
        r = 0 
       End If 
       If g < 0 Then 
        g = 0 
       End If 
       If b < 0 Then 
        b = 0 
       End If 
      Catch ex As Exception 

      End Try 

     Next 
    Next 

    'Dim colorData As Byte() = {CByte(blue), CByte(green), 
    '       CByte(red), CByte(255)} 
    'Dim rect As New Int32Rect(x, y, 1, 1) 

    'Try 
    ' wb.WritePixels(rect, colorData, stride, 0) 
    'Catch ex As Exception 
    'End Try 

    Dim k As New ImageBrush 
    k.ImageSource = wb 
    Return k 

你能告訴我如何加快這個過程,或者是有一個有效的方式來實現這一目標。我知道有免費使用的顏色選擇器,但我想嘗試一下我自己。 我我使用VB.Net,WPF。 謝謝。

+0

經過幾輪測試和瀏覽像素編碼後,我發現了WritableBitmap.Lock和Unlock Methods,開始處理速度非常快,但仍然不夠,速度快了5倍。 – surpavan

回答

1

找到答案,看來該梯度圖也通過使用XAML如下產生完全相同的方式:

</Canvas>--> 
    <Canvas Width="256" Height="256" Margin="32,30,215,26"> 
     <Canvas.Background> 
      <LinearGradientBrush StartPoint="0 0.5" EndPoint="1 0.5"> 
       <GradientStop Color="White" Offset="0" /> 
       <GradientStop Offset="1" Color="{Binding ElementName=MySlider, Path=Value, Converter={StaticResource SlidertoColor1}}" /> 
      </LinearGradientBrush> 
     </Canvas.Background> 
     <Canvas Width="256" Height="256"> 
      <Canvas.Background> 
       <LinearGradientBrush StartPoint="0.5 0" EndPoint="0.5 1"> 
        <GradientStop Color="Transparent" Offset="0" /> 
        <GradientStop Color="Black" Offset="1" /> 
       </LinearGradientBrush> 
      </Canvas.Background> 
     </Canvas> 
    </Canvas> 

謝謝您的支持。