Windows Develop Bookmark and Share   
 index > Windows Forms Sample Applications > Image Modifications
 

Image Modifications

Hi All,

Here i am writing the code to resize the image size. Whenever we change the size like 640 * 480 pixels like.... then change the imaeg size of panel or picture box control (image is saved to this picture box/panel). And if save this file in .jpeg, .png.....then selecting the option from combobox.

And in another page iwant code tochang the colur of an image to black and white and gray, to change the image quality.

I am doing this in Windows application with c#.net. Send code to this requirement plz.

Thanks in advance.

Sahasra  Tuesday, March 25, 2008 7:35 AM

Hi,

1). You can set the SizeMode property of the PictureBox to AutoSize, then whenever the image size changes, the PictureBox will adjust its size accordingly.

2). You can save the image with the format you need by ImageFormat enumeration. Something like this:

this.pictureBox1.Image.Save(@"c:\test2\a.png", ImageFormat.Png);

3). For making the image gray, one approach is to loop through the pixels in the bitmap,retrieve the color on the specified pixel, and change it using some algorithm, for example, we can do someting like this to gray the image

Code Snippet

void button1_Click(object sender, EventArgs e)

{

Bitmap bmp = new Bitmap(this.pictureBox1.Image.Width,

this.pictureBox1.Image.Height);

Bitmap img = this.pictureBox1.Image as Bitmap;

for (int h = 0; h < bmp.Height; h++)

{

for (int w = 0; w < bmp.Width; w++)

{

Color c = img.GetPixel(w, h);

Color newColor = Color.FromArgb(

(int)(0.5 * c.R + 0.3 * c.G + 0.2 * c.B),

(int)(0.5 * c.R + 0.3 * c.G + 0.2 * c.B),

(int)(0.5 * c.R + 0.3 * c.G + 0.2 * c.B));

bmp.SetPixel(w, h, newColor);

}

}

this.pictureBox1.Image = bmp;

}



Best Regards

Zhi-xin Ye

Zhi-Xin Ye  Monday, March 31, 2008 7:11 AM

Hi,

1). You can set the SizeMode property of the PictureBox to AutoSize, then whenever the image size changes, the PictureBox will adjust its size accordingly.

2). You can save the image with the format you need by ImageFormat enumeration. Something like this:

this.pictureBox1.Image.Save(@"c:\test2\a.png", ImageFormat.Png);

3). For making the image gray, one approach is to loop through the pixels in the bitmap,retrieve the color on the specified pixel, and change it using some algorithm, for example, we can do someting like this to gray the image

Code Snippet

void button1_Click(object sender, EventArgs e)

{

Bitmap bmp = new Bitmap(this.pictureBox1.Image.Width,

this.pictureBox1.Image.Height);

Bitmap img = this.pictureBox1.Image as Bitmap;

for (int h = 0; h < bmp.Height; h++)

{

for (int w = 0; w < bmp.Width; w++)

{

Color c = img.GetPixel(w, h);

Color newColor = Color.FromArgb(

(int)(0.5 * c.R + 0.3 * c.G + 0.2 * c.B),

(int)(0.5 * c.R + 0.3 * c.G + 0.2 * c.B),

(int)(0.5 * c.R + 0.3 * c.G + 0.2 * c.B));

bmp.SetPixel(w, h, newColor);

}

}

this.pictureBox1.Image = bmp;

}



Best Regards

Zhi-xin Ye

Zhi-Xin Ye  Monday, March 31, 2008 7:11 AM

Hi thanks for your reply.

But i didn't get my exact requirement. My query is to chenge the image size. First what i am doing is i am assigning the image to picturebox. And next if we change the size of an image the i am entering the new width and height in textboxes then click on the Apply button, here these new height and width will be set to Picture box image. And this requirement i want in percentages also. Please help me in this query.

Thank you .

Sahasra  Tuesday, April 01, 2008 7:05 AM

The AutoSize mode won't work for you?

Zhi-Xin Ye  Tuesday, April 01, 2008 7:10 AM

plz Give the solution for my query. Thanx
Sahasra  Tuesday, April 01, 2008 11:47 AM

You can use google to search for other answers

Custom Search

More Threads

• TaskVision Source Repair/Uninstall/Reinstall Woes
• Download sample windows application projects
• C# Version??
• MSMQ version
• Adding Fields
• UI Patterns
• Problem to see new species..
• object cannt be disposed
• Research on Terrarium...Please Help me ASAP!
• How TO Auto Update From mySelf TaskVisionUpdates Site?