2016-01-07 55 views
0

我想創建一個白色圖片(灰度格式)。'System.ArgumentException'當試圖使用SetPixel(x,y,Color)

因此,這裏是我使用的代碼:

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

「我」是一個現有的位圖圖像。我正在玩它的頻道。 原理是創建一個灰度1像素的圖像,給這個像素的白色,然後放大到良好的大小。

但是我有這樣的結果:

「類型 'System.ArgumentException' 未處理的異常發生在 System.Drawing.dll程序」

我試圖Color.White但它不是不允許使用灰度或索引格式。

我還有哪些其他方法可以解決這個問題?

+0

並非所有枚舉的像素格式實際上都支持。其餘的代碼很奇怪 - 爲什麼不清楚你想要的顏色? – TaW

+1

PixelFormat枚舉的像素格式實際上並未完全實現。像16bppGrayScale一樣。回想起來,GDI +的設計假設隨着顯示技術的改進,一些像素格式將變得可用。這種情況並沒有發生,液晶顯示器實際上距CRT顯着一步之遙,還沒有接近尾聲。支持16bpp灰度的監視器確實存在,您可以在當地醫院的放射科找到它們。你知道他們收到賬單後的花費。 –

回答

0

爲什麼不這樣做而不轉換爲灰度?

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

簡單地通過將顏色通道所需的順序

+0

我親自調試你的代碼,似乎有缺少的東西。 –

+0

你給我的變量值嗎?這是一個從.bmp文件打開的位圖。 否則你的答案,我已經看過它。這就是我使用color.FromArgb的想法。 – KuroTenshi

+0

我正在用圖像加載「我」,是的。 似乎有一個名爲PaletteFlag的枚舉,它從成員中似乎定義瞭如何使用顏色,無論如何我無法在位圖類中找到屬性來設置這些標誌。 –

0

我會後所有的代碼來完成。目前我知道這很難看,但這是爲了快速需要。 我正常的地圖從通常的紫色格式轉換爲橙色格式。這只是渠道交換。

Converting normal map from purple format to orange format

我使用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"); 
相關問題