【问题标题】:'System.ArgumentException' when trying to use SetPixel (x, y, Color)尝试使用 SetPixel (x, y, Color) 时出现“System.ArgumentException”
【发布时间】:2016-04-11 12:35:07
【问题描述】:

我正在尝试创建一张白色图片(灰度格式)。

所以这是我使用的代码:

Bitmap bmp = new Bitmap(1, 1,        
System.Drawing.Imaging.PixelFormat.Format16bppGrayScale);
bmp.SetPixel(0, 0, Color.FromArgb(255, 255, 255));
bmp = new Bitmap(bmp, i.Width, i.Height);

“I”是现有的位图图像。我正在玩它的频道。 原理是创建一个1像素的灰度图,给这个像素点白色,然后放大到合适的尺寸。

但结果是这样的:

“'System.ArgumentException' 类型的未处理异常发生在 System.Drawing.dll"

我尝试了Color.White,但它不允许用于灰度或索引格式。

我还有哪些其他选项可以解决此问题?

【问题讨论】:

  • 实际上并非所有枚举像素格式都受支持。其余的代码很奇怪——为什么不明确你想要的颜色???
  • PixelFormat 枚举具有实际未完全实现的像素格式。像 16bppGrayScale。很久以前,GDI+ 的设计假设是随着显示技术的改进,一些像素格式将变得可用。这并没有发生,液晶显示器实际上比 CRT 倒退了一大步,而且还没有接近追赶上来。确实存在支持 16bpp 灰度的监视器,您可以在当地医院的放射科找到它们。当您收到账单时,您就会知道它们的成本。

标签: c# bitmap drawing grayscale argumentexception


【解决方案1】:

为什么不转换成灰度不做呢?

Bitmap A = new Bitmap(i.Width, i.Height);
for (int x = 0; x < i.Width; x++)
    for (int y = 0; y < i.Height; y++)
    {
        Color C = i.GetPixel(x, y);
        Color WD = Color.FromArgb(C.R, 255, C.G, 0);
        A.SetPixel(x, y, WD);
    }

只需按所需顺序放置颜色通道即可

【讨论】:

  • 我在亲自调试你的代码,好像少了什么。
  • 你给 i 变量赋值了吗?这是我从 .bmp 文件打开的位图。否则你的答案,我已经看过了。这就是让我想到使用 color.FromArgb 的原因。
  • 我正在加载带有图像的i,是的。似乎有一个名为 PaletteFlag 的枚举,它似乎从成员中定义了颜色的使用方式,无论如何我在位图类中找不到设置这些标志的属性。
  • 感谢您的回复。抱歉,我对编辑有点搞砸了。我不能删除自己无用的编辑吗?是的,我刚刚看到了调色板标志。但是它可以用来做什么呢?
  • :您可以重新编辑您的帖子或将其删除。我现在告诉你,我们应该彻底改变这个问题,bmp.GetPixel(0, 0); 也抛出了同样的异常。
【解决方案2】:

我将发布所有代码。我知道它目前很丑,但它是为了快速需要。 我正在将法线贴图从通常的紫色格式转换为橙色格式。这只是频道交换。

我正在使用 AForge 框架来更轻松地执行此操作。我从输入图片中提取所需的通道(只有红色要交换)。我克隆它,它也为 RGB 条目添加了一个 alpha 通道。然后我修改副本的通道以获得新的图像。

问题在于需要新的纯白色和纯黑色图像的红色和蓝色通道。

我需要在“Format8bppIndexed”中创建一个图像(这会很好),或者在“Format16bppGrayScale”中创建一个更糟糕的图像。 SetPixel 不接受索引格式。

//Opening the normal map (RGB)

Bitmap i = AForge.Imaging.Image.FromFile("C:/Users/KuroTenshi/WD/normalblue/07_bloom_doublemetaldoor_d_high_n.bmp");

//Extract the red channel, that will be put in an alpha channel

ExtractChannel filterR = new ExtractChannel(RGB.R);
Bitmap r = filterR.Apply(i);

//Clone the input image to have a base for the output (it's converted to ARGB)

Bitmap j = AForge.Imaging.Image.Clone(i);

//Extract channels to modify

ExtractChannel filterR2 = new ExtractChannel(RGB.R);
ExtractChannel filterB2 = new ExtractChannel(RGB.B);
ExtractChannel filterA2 = new ExtractChannel(RGB.A);
Bitmap r2 = filterR2.Apply(j);
Bitmap b2 = filterB2.Apply(j);
Bitmap a2 = filterA2.Apply(j);

//Putting input's red channel into output's alpha channel

a2 = r;

//Creating a white Bitmap for the output's red channel

Bitmap bmp = new Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format16bppGrayScale);
bmp.SetPixel(0, 0, Color.FromArgb(255, 255, 255));
bmp = new Bitmap(bmp, i.Width, i.Height);

r2 = bmp;

//Creating a black Bitmap for the output's blue channel

Bitmap bmp2 = new Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format16bppGrayScale);
bmp2.SetPixel(0, 0, Color.FromArgb(0,0,0));
bmp2 = new Bitmap(bmp2, i.Width, i.Height);

b2 = bmp2;

//Replace channels

ReplaceChannel replaceFilter = new ReplaceChannel(RGB.A, a2);
replaceFilter.ApplyInPlace(j);
replaceFilter = new ReplaceChannel(RGB.R, r2);
replaceFilter.ApplyInPlace(j);
replaceFilter = new ReplaceChannel(RGB.B, b2);
replaceFilter.ApplyInPlace(j);

//Save output


j.Save("C:/Users/KuroTenshi/WD/normalblue/07_bloom_doublemetaldoor_d_high_n2.bmp");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    相关资源
    最近更新 更多