'System.ArgumentException' when trying to use SetPixel (x, y, Color)

家住魔仙堡 提交于 2019-12-13 08:29:03

问题


I am trying to create a white picture (greyscale format).

So here is the code I use:

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" is an existing Bitmap image. I am playing with its channels. The principle is to create a 1 pixel image in greyscale, give this pixel the white color and then enlarge it to the good size.

But I have this as a result:

"An unhandled exception of type 'System.ArgumentException' occurred in System.Drawing.dll"

I tried Color.White but it isn't allowed for greyscale or indexed formats.

What are my other options to fix this?


回答1:


Why not to do it without converting to greyscale?

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

Simply done by placing colour channels in the desired order




回答2:


I'll post all the code. It's currently ugly I know but it's for a quick need. I am converting normal maps from the usual purple format to the orange format. It's just channels swapping.

I am using the AForge framework to do this more easily. I extract needed channels (only the red to swap) from the input pictures. I clone it, that adds an alpha channel too to the RGB entry. Then I modify the channels of the copy to get the new image.

The problem is for the red and blue channels that need new pitch white and pitch black images.

I need to create an image either in "Format8bppIndexed" (which would be nice) or at worse in "Format16bppGrayScale". SetPixel doesn't accept indexed format.

//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");


来源:https://stackoverflow.com/questions/34649745/system-argumentexception-when-trying-to-use-setpixel-x-y-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!