By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,515 Members | 1,339 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,515 IT Pros & Developers. It's quick & easy.

C# Image Processing

P: 1
Folks,

I'm a really rubbish programmer and have just started C#!!

I have been tasked with creating a program that will open an image file to a picturebox and then read the values for each pixel in that file into an array. Each image I will be using is in greyscale, so its values will be 0-255. I Understand that I must use the LockBits method, as PictureBox stores a file location as a string.

In essence, I am totally lost what to do next. The code I have below is the rough foundations of the solution to my problem (I hope).

private void OpenFile()
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Bitmap Files (*.bmp)|*.bmp";
ofd.Title = "Type File";

if (ofd.ShowDialog() == DialogResult.Cancel)
return;
try
{
pictureBox1.ImageLocation = ofd.FileName;
ofd.Dispose();
generateArray(pictureBox1.ImageLocation);
}
...
...

private void generateArray(String fileToDisplay)
{
int[] pixelArray=null;
//insert code
BitmapData sourceData = bmpImage.LockBits(new Rectangle(0, 0, bmpImage.Width, bmpImage.Height), ImageLockMode.ReadOnly, bmpImage.PixelFormat);
//insert code
}



Can anyone help me finish this off so when I open a new openfiledialog, and select an image, the program will read each pixel in the image, and store its value of between 0-255 in the array?! Any help is greatly appreciated!!!

Many thanks,
Stuart.
Apr 25 '07 #1
Share this Question
Share on Google+
1 Reply


TRScheel
Expert 100+
P: 638
Folks,

I'm a really rubbish programmer and have just started C#!!

I have been tasked with creating a program that will open an image file to a picturebox and then read the values for each pixel in that file into an array. Each image I will be using is in greyscale, so its values will be 0-255. I Understand that I must use the LockBits method, as PictureBox stores a file location as a string.

In essence, I am totally lost what to do next. The code I have below is the rough foundations of the solution to my problem (I hope).

private void OpenFile()
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Bitmap Files (*.bmp)|*.bmp";
ofd.Title = "Type File";

if (ofd.ShowDialog() == DialogResult.Cancel)
return;
try
{
pictureBox1.ImageLocation = ofd.FileName;
ofd.Dispose();
generateArray(pictureBox1.ImageLocation);
}
...
...

private void generateArray(String fileToDisplay)
{
int[] pixelArray=null;
//insert code
BitmapData sourceData = bmpImage.LockBits(new Rectangle(0, 0, bmpImage.Width, bmpImage.Height), ImageLockMode.ReadOnly, bmpImage.PixelFormat);
//insert code
}



Can anyone help me finish this off so when I open a new openfiledialog, and select an image, the program will read each pixel in the image, and store its value of between 0-255 in the array?! Any help is greatly appreciated!!!

Many thanks,
Stuart.
The bitmap object has a function called GetPixel (I believe...) and it passes the x, y of the pixel and returns the corresponding color. You can then use that color to create a greyscale of it.

EDIT: Also, the BitMap object's constructor can be passed a file location like you did above
Apr 25 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.