2013-02-27 101 views
0

我在WinRT應用程序中創建圖像的直方圖表示時遇到了問題。我想製作的圖像包括紅色,綠色,藍色和亮度四個直方圖。WinRT中RGB值的直方圖繪製

我的主要問題是如何實際繪製該直方圖的圖片,以便我可以將其顯示在屏幕上。我的代碼到目前爲止是相當...凌亂,我已經搜索了很多這個主題,大部分我的結果包括Java中的代碼,我試圖以某種方式在C#中翻譯它,但API是非常不同的...已經從AForge以及企圖但這是的WinForms ...

這裏是我的亂碼,我知道這很糟糕,但我努力使這項工作首先:

public static WriteableBitmap CreateHistogramRepresentation(long[] histogramData, HistogramType type) 
    { 
     //I'm trying to determine a max height of a histogram bar, so 
     //I could determine a max height of the image that then I'll remake it 
     //at a lower resolution : 
     var max = histogramData[0]; 

     //Determine the max value, the highest bar in the histogram, the initial height of the image. 
     for (int i = 0; i < histogramData.Length; i++) 
     { 
      if (histogramData[i] > max) 
       max = histogramData[i]; 
     } 

     var bitmap = new WriteableBitmap(256, 500); 

     //Set a color to draw with according to the type of the histogram : 
     var color = Colors.White; 
     switch (type) 
     { 
      case HistogramType.Blue : 
       { 
        color = Colors.RoyalBlue; 
        break; 
       } 
      case HistogramType.Green: 
       { 
        color = Colors.OliveDrab; 
        break; 
       } 
      case HistogramType.Red: 
       { 
        color = Colors.Firebrick; 
        break; 
       } 
      case HistogramType.Luminosity: 
       { 
        color = Colors.DarkSlateGray; 
        break; 
       } 
     } 

     //Compute a scaler to scale the bars to the actual image dimensions : 
     var scaler = 1; 
     while (max/scaler > 500) 
     { 
      scaler++; 
     } 

     var stream = bitmap.PixelBuffer.AsStream(); 
     var streamBuffer = new byte[stream.Length]; 

     //Make a white image initially : 
     for (var i = 0; i < streamBuffer.Length; i++) 
     { 
      streamBuffer[i] = 255; 
     } 

     //Color the image : 
     for (var i = 0; i < 256; i++) // i = column 
     { 
      for (var j = 0; j < histogramData[i]/scaler; j++) // j = line 
      { 
       streamBuffer[j*256*4 + i] = color.B; //the image has a 256-pixel width 
       streamBuffer[j*256*4 + i + 1] = color.G; 
       streamBuffer[j*256*4 + i + 2] = color.R; 
       streamBuffer[j*256*4 + i + 2] = color.A; 
      } 
     } 

     //Write the Pixel Data into the Pixel Buffer of the future Histogram image : 
     stream.Seek(0, 0); 
     stream.Write(streamBuffer, 0, streamBuffer.Length); 

     return bitmap.Flip(WriteableBitmapExtensions.FlipMode.Horizontal); 
    } 

這將創建一個非常糟糕直方圖表示,它甚至沒有用相應的顏色着色......它工作不正常,我正在努力修復它...

如果你可以用一個鏈接你可能知道WinRT應用程序的直方圖表示的任何代碼或其他一切都是非常感謝。

+0

爲什麼不使用第三方控件? http://www.telerik.com/products/windows-8/controls/chart.aspx – 2013-02-28 02:57:36

+0

嘗試使用WriteableBitmapEx CodePlex項目 - 它實際上非常容易使用,並且擺脫了您需要做的大量工作。 http://writeablebitmapex.codeplex.com/ – 2013-02-28 07:26:52

+0

感謝WriteableBitmap擴展,我已經在我的項目的其他部分使用過,但是...我很笨拙,我完全忘記了那個完成我的工作的DrawLine方法。謝謝! – VasileF 2013-03-01 11:29:17

回答

0

儘管您可以像JP Alioto指出的那樣使用圖表控件,但直方圖傾向於代表批次的數據。在你的例子中,你正在渲染256條* 4軸(R,G,B,L)。製圖控制的問題在於,他們通常喜歡收集水合數據的集合(或數組),這些數據是他們繪製的,並且他們傾向於保留在記憶中。像你的直方圖需要在內存中有1024個對象(256 * 4),並作爲一個整體傳遞給圖表。這只是不利於內存管理。

當然的選擇是自己畫。但正如你發現的那樣,逐個像素的繪製可能會有點痛苦。在我看來,最好的答案是贊同Shahar,並建議您在CodePlex上使用WriteableBitmapEx。

http://writeablebitmapex.codeplex.com

WriteableBitmapEx包括用於繪製的形狀像,是非常非常快的直線和矩形的方法。您可以在枚舉數據時繪製數據(而不必一次將所有數據全部存儲在內存中),結果是一個不錯的緊湊圖像,該圖像已經「位圖緩存」(這意味着它呈現得非常快,因爲它沒有在每一幀上重新繪製)。

開發支持,設計支持和更多的真棒善良的人:http://bit.ly/winappsupport