【问题标题】:Why am I getting a 'parameter not valid' exception?为什么我会收到“参数无效”异常?
【发布时间】: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


【解决方案1】:

为了解决这个问题,我采取了以下措施:

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;
    }

【讨论】:

    猜你喜欢
    • 2011-11-17
    • 2011-10-08
    • 2013-11-08
    • 2013-02-10
    • 2014-07-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-15
    • 1970-01-01
    相关资源
    最近更新 更多