Live Chat Icon For mobile
Live Chat Icon

How do I draw an image with a gray scale?

Platform: WinForms| Category: Bitmaps and Images

Here is some code:


// Not the closest grayscale representation in the RGB space, but
// pretty close.
// Closest would be the cubic root of the product of the RGB colors,
// but that cannot be represented in a ColorMatrix.
public void DrawGrayedImage(Graphics g, Image image, int left,
	int top)
{ 
	ImageAttributes ia = new ImageAttributes(); 
	ColorMatrix cm = new ColorMatrix(); 

	// 1/3 on the top 3 rows and 3 columns
	cm.Matrix00 = 1/3f; 
	cm.Matrix01 = 1/3f; 
	cm.Matrix02 = 1/3f; 
	cm.Matrix10 = 1/3f; 
	cm.Matrix11 = 1/3f; 
	cm.Matrix12 = 1/3f; 
	cm.Matrix20 = 1/3f; 
	cm.Matrix21 = 1/3f; 
	cm.Matrix22 = 1/3f; 

	ia.SetColorMatrix(cm); 

	g.DrawImage(image, new Rectangle(left, top, image.Width, 
		image.Height), 0, 0, image.Width, image.Height, 
		GraphicsUnit.Pixel, ia); 
} 

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.