【发布时间】:2016-08-08 06:26:49
【问题描述】:
基本上我不知道为什么我会得到一个 Parameter not Valid 异常,因为从我通过 google 了解到的情况来看,这是一个常见的异常,它让您无法了解幕后实际发生的事情,或者本质上导致错误的原因.
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;
}
任何关于这个问题的帮助都会非常感谢你:)
【问题讨论】:
-
那么找到重复的,假设它存在,然后让它和其他的格式一样?
标签: c# graphics bitmap dimensions onpaint