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中它自己的函数;
答案 0 :(得分:6)
您的l
是一个数组列表,但结构的返回类型是一个数组。
如果您需要返回单个数组:
List<long> l = new List<long>();
l.AddRange(myHistogramBlue);
l.AddRange(myHistogramGreen);
l.AddRange(myHistogramRed);
return l.ToArray();
如果您需要返回数组的列表,请将返回类型从long[]
更改为List<long[]>
。虽然在这种情况下,我可能会使用Dictionary<string,long[]>
来存储每个数组的名称:
var l = new Dictionary<string,long[]>();
l.Add("blue", myHistogramBlue);
l.Add("green", myHistogramGreen);
l.Add("red", myHistogramRed);
答案 1 :(得分:1)
你的方法应该返回一个longs数组(long[]
)。
您正在尝试返回long数组的列表(List<long[]>
)。
你可以:
A)更改方法签名以返回List<long[]>
或
B)使用SelectMany()
将long列表变为longs数组。
即。
return l.SelectMany(v => v).ToArray();
修改强>
C)@Oded建议:)
答案 2 :(得分:1)
您的返回类型与您的函数类型不同:
functiontype: long[]
return type: List<long[]>
更改其中一个以匹配另一个。
答案 3 :(得分:1)
取决于您的订购需求......
for(int i = 0; i < myHistogramBlue.Length; i++)
{
l.Add(myHistogramBlue[i]);
l.Add(myHistogramGreen[i]);
l.Add(myHistogramRed[i]);
}
return l.toArray();`
答案 4 :(得分:0)
您的返回值为long[]
,而您的返回值为List<long[]>
将声明更改为:
public static List<long[]> GetHistogramRGB(Bitmap b)
答案 5 :(得分:0)
您应该返回l.ToArray();
代码long[] , List<long[]>
中的返回类型不兼容。
答案 6 :(得分:-1)
您正在尝试返回
List<long[]>
as
long[]
替换您的代码:
List<long> l = new List<long>();
l.Add(myHistogramBlue);
l.Add(myHistogramGreen);
l.Add(myHistogramRed);
return l.ToArray();