我在 WinRT 应用程序中创建图像的直方图表示时遇到问题。我想要制作的包括图像的红色、绿色、蓝色、亮度的四个直方图。
我的主要问题是如何实际绘制该直方图的图片,以便我可以在屏幕上显示它。到目前为止,我的代码非常...凌乱,我为这个主题搜索了很多,主要是我的结果由 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 应用程序的直方图表示的代码或其他所有内容,我们将不胜感激。