Grayscale Image...( plz let me know how can i use this method????)

Posted by Sp under Regular Expressions on 8/18/2010 | Points: 10 | Views : 2356 | Status : [Member] | Replies : 2
I am following this link:
http://www.switchonthecode.com/tutorials/csharp-tutorial-convert-a-color-image-to-grayscale
public static Bitmap MakeGrayscale(Bitmap original)
{
//make an empty bitmap the same size as original
Bitmap newBitmap = new Bitmap(original.Width, original.Height);

for (int i = 0; i < original.Width; i++)
{
for (int j = 0; j < original.Height; j++)
{
//get the pixel from the original image
Color originalColor = original.GetPixel(i, j);

//create the grayscale version of the pixel
int grayScale = (int)((originalColor.R * .3) + (originalColor.G * .59)
+ (originalColor.B * .11));

//create the color object
Color newColor = Color.FromArgb(grayScale, grayScale, grayScale);

//set the new image's pixel to the grayscale version
newBitmap.SetPixel(i, j, newColor);
}
}

return newBitmap;
}
I AM PASSING VALUES TO THIS METHOD AS:

protected void grayscale_Click(object sender, EventArgs e)
{
//Get the path of image file

string path = Image1.ImageUrl;

//create bitmap from path and pass as argument to function makegryscale2

Bitmap b = MakeGrayscale(new Bitmap(path.Substring(2)));

//overwrite with saving at same path

b.Save(path);

//set it imageurl

Image1.ImageUrl = path;

}
BUT it is giving me an error, the error says Invalid Parameter. plz let me know how can i use this method????




Responses

Posted by: Raja on: 8/19/2010 [Member] Starter | Points: 25

Up
0
Down
Can you debug and see at which line its throwing error ?

or Copy-paste the error from browser here.

Regards,
Raja, USA

Sp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sp on: 8/19/2010 [Member] Starter | Points: 25

Up
0
Down
Bitmap b = MakeGrayscale(new Bitmap(path.Substring(2)));
It says the parameter supplied is invalid....

I tried another way also,..
I am dynamically creating a grayscale image, now i want to store it to a location and pass the reference of that location as image url to 'ImgGrayscale'(Image control).
here is what i am doing...
protected void Button3_Click(object sender, EventArgs e)
{
System.Drawing.Image img = System.Drawing.Image.FromFile("D:\\ImageSavingAndRetrieval\\Images\\thank-you.jpg");
System.Drawing.Bitmap CopyImage = (System.Drawing.Bitmap)img.Clone();
//After That Find It's Width & Height...
int ImgWidth = CopyImage.Size.Width;
int ImgHeight = CopyImage.Size.Height;
//Now Convert The Image :
for (int j = 0; j < ImgHeight; j++)
{
for (int i = 0; i < ImgWidth; i++)
{
System.Drawing.Color MyColor;
MyColor = CopyImage.GetPixel(i, j);
CopyImage.SetPixel(i, j, System.Drawing.Color.FromArgb(
(MyColor.R + MyColor.G + MyColor.B) / 3,
(MyColor.R + MyColor.G + MyColor.B) / 3,
(MyColor.R + MyColor.G + MyColor.B) / 3));
}
}
System.Drawing.Image img2= CopyImage ;



}

how to proceed.. also, i am statically providing the path of the image coz i did not find a way to provide the complete path otherwise.....fetching imageurl property of the image control did not help in this case...

Sp, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response