基本上我不知道为什么我得到一个参数无效的例外,因为从我通过谷歌学到的,这是一个常见的例外,让你无法洞察幕后实际发生的事情,或者什么是基本上导致错误
public void DecompileMap() {
tileArray = new Image[rawImageData.Width / 64, rawImageData.Height / 64];
Bitmap copiedImage = rawImageData.Clone() as Bitmap;
Bitmap buffer = (Bitmap)Image.FromFile(Form1.AssemblyDirectory + @"\Content\Images\blank.png");
Graphics tempG = Graphics.FromImage(buffer);
GraphicsUnit units = GraphicsUnit.Pixel;
tempG.CompositingMode = CompositingMode.SourceCopy;
int row = 0;
int col = 0;
for (int j = 0; j < copiedImage.Height; j+=64) {
col = 0;
for (int i = 0; i < copiedImage.Width; i+=64) {
tempG.DrawImage(copiedImage, 0, 0, new Rectangle(new Point(j, i), new Size(IMAGE_DIMENSIONS, IMAGE_DIMENSIONS)), units);
tileArray[row, col] = buffer;
if (GetMostUsedColor((Bitmap)tileArray[row, col]).R == 255 && GetMostUsedColor((Bitmap)tileArray[row, col]).G == 255 && GetMostUsedColor((Bitmap)tileArray[row, col]).B == 255) {
hitboxes.Add(new Rectangle(new Point(j, i), new Size(buffer.Width, buffer.Height)));
}
col++;
}
row++;
}
}
public static IEnumerable<Color> GetPixels(Bitmap bitmap) {
for (int x = 0; x < bitmap.Width; x++) {// I am getting my error here at bitmap.width.
for (int y = 0; y < bitmap.Height; y++) {
Color pixel = bitmap.GetPixel(x, y);
yield return pixel;
}
}
}
public static Color GetMostUsedColor(Bitmap bmp) {
using (var bitmap = bmp) {
var colorsWithCount =
GetPixels(bitmap)
.GroupBy(color => color)
.Select(grp =>
new {
Color = grp.Key,
Count = grp.Count()
})
.OrderByDescending(x => x.Count)
.Take(1);
foreach (var colorWithCount in colorsWithCount) {
return colorWithCount.Color;
}
}
return Color.Black;
}
对此问题的任何帮助都会非常好,谢谢:)
答案 0 :(得分:0)
为了解决这个问题,我所做的是:
public static Color GetMostUsedColor(Bitmap bmp) {
using (var bitmap = bmp) {
var colorsWithCount =
GetPixels(bitmap)
.GroupBy(color => color)
.Select(grp =>
new {
Color = grp.Key,
Count = grp.Count()
})
.OrderByDescending(x => x.Count)
.Take(1);
foreach (var colorWithCount in colorsWithCount) {
return colorWithCount.Color;
}
}
return Color.Black;
}
并添加了第一行;
public static Color GetMostUsedColor(Bitmap bmp) {
Bitmap b = bmp.Clone() as Bitmap; //This line fixes it
using (var bitmap = b) {// Then edit the previous var named: bmp to b.
var colorsWithCount =
GetPixels(bitmap)
.GroupBy(color => color)
.Select(grp =>
new {
Color = grp.Key,
Count = grp.Count()
})
.OrderByDescending(x => x.Count)
.Take(1);
foreach (var colorWithCount in colorsWithCount) {
return colorWithCount.Color;
}
}
return Color.Black;
}