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 の関数自体にあります。