2016-11-12 41 views
0

如何在C#中設置圖形顏色?
SetColor方法不起作用。如何在C#中設置圖形顏色#

如何將以下Java代碼轉換爲C#?

private Graphics g1; 

g1.setColor(Color.getHSBColor(h, 0.8f, b)); 
Color col = Color.getHSBColor(h, 0.8f, b); 
int red = col.getRed(); 
int green = col.getGreen(); 
int blue = col.getBlue(); 
+1

'Color'和'Graphics'上的這些函數名看起來像是來自java類,但你的問題被標記爲C#,它的語言是什麼? –

+0

這是C#代碼,你確定嗎? –

+0

我認爲你使用j ++的'java.awt.Graphics';)。 –

回答

0

C#中的繪圖模型略有不同。您不必在圖形對象上設置資源(如顏色),而是創建資源,如PenBrush,這些資源具有屬性,例如Color。然後使用這些對象在圖像上繪圖。

Graphics g = Graphics.FromImage(someBitMap); //create a graphics object for an existing BitMap 
Color c = new Color(255,0,0); //a "red" color 
Pen p = new Pen(c); //create a Pen using the Red color from earlier 
p.Width = 5; //Pen is 5 pixels wide 

g.DrawLine(p, 0,0,100,100); //draw a diagonal line 

//Get rbg values. 
int r = c.R; 
int g = c.G; 
int b = c.B; 

//It is VERY important to call `.Dispose()` on GDI objects. They contain unmanaged system resources that can, and will, leak if you don't. 
g.Dispose() 
p.Dispose() 

它看起來像你可能試圖將色彩從HSB色彩空間轉換爲RGB。不幸的是,.NET本身不支持。以下博客文章有一些代碼可以幫助你:https://blogs.msdn.microsoft.com/cjacks/2006/04/12/converting-from-hsb-to-rgb-in-net/

+0

非常感謝布拉德利。 –