Reference system.drawing.imaging instead. That is the library for image manipulation as described. I'm interested to know why your convert to Gray favours the green channel? You should also replicate alpha channel too just in case.
If it helps, mark as answer - your approval helps with my recovery ;)
cms9651
Member
175 Points
573 Posts
Convert an Image to Grayscale - C#
Dec 30, 2012 07:06 PM|LINK
Hi there, I hope your help.
I need convert an color image to Grayscale with C#.
I tried this code but I have error, why?
thank you
CS1502: ConvertToGrayscale (System.Drawing.Bitmap) has some invalid arguments protected void Page_Load(object sender, EventArgs e) { ....... string Img = "http://www.mywebpage.com/images/" + (Convert.ToString(dr1["myPicture"]); lblImage2.ImageUrl = ConvertToGrayscale(Img); } public Bitmap ConvertToGrayscale(Bitmap source) { Bitmap bm = new Bitmap(source.Width, source.Height); for (int y = 0; y < bm.Height; y++) { for (int x = 0; x < bm.Width; x++) { Color c = source.GetPixel(x, y); int luma = (int)(c.R * 0.3 + c.G * 0.59 + c.B * 0.11); bm.SetPixel(x, y, Color.FromArgb(luma, luma, luma)); } } return bm; }cms9651
Member
175 Points
573 Posts
Re: Convert an Image to Grayscale - C#
Dec 31, 2012 08:29 AM|LINK
I tried this, but I have error:
CS0117: 'System.Web.UI.WebControls.Image' does not contain a definition for 'Image'
Can you help me?
<asp:Image ID="lblImage2" runat="server" /> protected void Page_Load(object sender, EventArgs e) { bitmapmakegrayscale(lblImage2.Image); } private void bitmapmakegrayscale(System.Drawing.Image imagei) { Bitmap image = imagei as Bitmap; Bitmap bp = new Bitmap(image.Width, image.Height); for (int i = 0; i < image.Width; i++) { for (int j = 0; j < image.Height; j++) { Color orgcol = image.GetPixel(i, j); int grayscale = (int)((orgcol.R * .3) + (orgcol.G * .59) + (orgcol.B * .11)); Color newcol = Color.FromArgb(grayscale, grayscale, grayscale); bp.SetPixel(i, j, newcol); } } lblImage2.Image = bp; }prabu.raveen...
Contributor
5020 Points
955 Posts
Re: Convert an Image to Grayscale - C#
Dec 31, 2012 08:41 AM|LINK
Try the below code,
public Bitmap GrayScale(Bitmap Bmp)
{
int rgb;
Color c;
for (int y = 0; y < Bmp.Height; y++)
for (int x = 0; x < Bmp.Width; x++)
{
c = Bmp.GetPixel(x, y);
rgb = (int)((c.R + c.G + c.B) / 3);
Bmp.SetPixel(x, y, Color.FromArgb(rgb, rgb, rgb));
}
return Bmp;
}
cms9651
Member
175 Points
573 Posts
Re: Convert an Image to Grayscale - C#
Dec 31, 2012 08:53 AM|LINK
thank you, but I have the same error:
CS0117: 'System.Web.UI.WebControls.Image' does not contain a definition for 'Image'
<asp:Image ID="lblImage2" runat="server" /> protected void Page_Load(object sender, EventArgs e) { GrayScale(lblImage2.Image); } public Bitmap GrayScale(Bitmap Bmp) { int rgb; Color c; for (int y = 0; y < Bmp.Height; y++) for (int x = 0; x < Bmp.Width; x++) { c = Bmp.GetPixel(x, y); rgb = (int)((c.R + c.G + c.B) / 3); Bmp.SetPixel(x, y, Color.FromArgb(rgb, rgb, rgb)); } return Bmp; }cms9651
Member
175 Points
573 Posts
Re: Convert an Image to Grayscale - C#
Jan 13, 2013 11:30 AM|LINK
Can you help me?
Any suggestions?
kidshaw
Participant
1158 Points
252 Posts
Re: Convert an Image to Grayscale - C#
Feb 05, 2013 09:10 PM|LINK
Neodynamic
Participant
988 Points
283 Posts
Re: Convert an Image to Grayscale - C#
Feb 07, 2013 10:44 AM|LINK
GetPixel & SetPixel methods are overkill! Try this Alternative Version using the ColorMatrix class
DigiMortal
Contributor
5658 Points
939 Posts
MVP
Re: Convert an Image to Grayscale - C#
Feb 12, 2013 04:44 PM|LINK
Here is my version of making grayscale images in .NET: How to create grayscale images on .NET
The code uses GetPixel and SetPixel - you can change it - but you get the idea how to extend the grayscaling functionality.
Also visit my ASP.NET blog or follow me @ Twitter:twitter.com/gpeipman