我在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應用程序的直方圖表示的任何代碼或其他一切都是非常感謝。
爲什麼不使用第三方控件? http://www.telerik.com/products/windows-8/controls/chart.aspx – 2013-02-28 02:57:36
嘗試使用WriteableBitmapEx CodePlex項目 - 它實際上非常容易使用,並且擺脫了您需要做的大量工作。 http://writeablebitmapex.codeplex.com/ – 2013-02-28 07:26:52
感謝WriteableBitmap擴展,我已經在我的項目的其他部分使用過,但是...我很笨拙,我完全忘記了那個完成我的工作的DrawLine方法。謝謝! – VasileF 2013-03-01 11:29:17