public static long[] GetHistogramRGB(Bitmap b)
{
long[] myHistogramBlue = new long[256];
long[] myHistogramGreen = new long[256];
long[] myHistogramRed = new long[256];
BitmapData bmData = null;
try
{
//Lock it fixed with 32bpp
bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
int scanline = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
unsafe
{
byte* p = (byte*)(void*)Scan0;
int nWidth = b.Width;
int nHeight = b.Height;
for (int y = 0; y < nHeight; y++)
{
for (int x = 0; x < nWidth; x++)
{
long Temp = 0;
Temp += p[0];
myHistogramBlue[Temp]++;
long Temp2 = 0;
Temp2 += p[1];
myHistogramGreen[Temp2]++;
long Temp3 = 0;
Temp3 += p[2];
myHistogramRed[Temp3]++;
//we do not need to use any offset, we always can increment by pixelsize when
//locking in 32bppArgb - mode
p += 4;
}
}
}
b.UnlockBits(bmData);
}
catch
{
try
{
b.UnlockBits(bmData);
}
catch
{
}
}
List<long[]> l = new List<long[]>();
l.Add(myHistogramBlue);
l.Add(myHistogramGreen);
l.Add(myHistogramRed);
return l;
}
在最后一行返回 l; 我收到错误:
Cannot implicitly convert type 'System.Collections.Generic.List<long[]>' to 'long[]'
我该如何解决?
我试着这样做:
public static long[] GetHistogramRGB(Bitmap b)
{
long[] myHistogramBlue = new long[256];
long[] myHistogramGreen = new long[256];
long[] myHistogramRed = new long[256];
BitmapData bmData = null;
try
{
//Lock it fixed with 32bpp
bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
int scanline = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
unsafe
{
byte* p = (byte*)(void*)Scan0;
int nWidth = b.Width;
int nHeight = b.Height;
for (int y = 0; y < nHeight; y++)
{
for (int x = 0; x < nWidth; x++)
{
long Temp = 0;
Temp += p[0];
myHistogramBlue[Temp]++;
long Temp2 = 0;
Temp2 += p[1];
myHistogramGreen[Temp2]++;
long Temp3 = 0;
Temp3 += p[2];
myHistogramRed[Temp3]++;
//we do not need to use any offset, we always can increment by pixelsize when
//locking in 32bppArgb - mode
p += 4;
}
}
}
b.UnlockBits(bmData);
}
catch
{
try
{
b.UnlockBits(bmData);
}
catch
{
}
}
var l = new Dictionary<string, long[]>();
l.Add("blue", myHistogramBlue);
l.Add("green", myHistogramGreen);
l.Add("red", myHistogramRed);
return l;
}
Error 2 Cannot implicitly convert type 'System.Collections.Generic.Dictionary<string,long[]>' to 'long[]'
我现在在另一个类中使用这个函数调用,如下所示:
long[] HistogramsValuesRGB = Form1.GetHistogramRGB(original_bmp);
这不会给出任何错误,错误在于返回 l 时 Form1 中的函数本身;