Read all images from a folder

Hello , I have the next sequence of code, and i want to read all images from folder, but in this stage of code I have an error at this row:Bitmap ^ bmpPicture = gcnew Bitmap(strFilename);
It i not finish because I don;t know exacly how to loop after all images from folder. If somebody can help me I would be grateful. Thank you.

System::IO::DirectoryInfo^ directoryInfo = System::IO::Directory::GetParent("..\\");
// System::String^ strDirPath = directoryInfo->FullName;
// folderBrowserDialog1->SelectedPath = strDirPath;
System::String^ strName;
if (this->folderBrowserDialog1->ShowDialog() == Windows::Forms::DialogResult::OK)
{
System::String^ strFilename = folderBrowserDialog1->SelectedPath;
array<System::String^>^ strMultipleImages = System::IO::Directory::GetFiles(strFilename, "*.jpg");
int nNumberOfImages = strMultipleImages->Length;
Bitmap ^ bmpPicture = gcnew Bitmap(strFilename);
H = bmpPicture->Height; W = bmpPicture->Width;
Bitmap ^ bmpPictureGray = gcnew Bitmap(W, H);
Color clrPixel;
double rC, gC, bC;
int** Ylbp_l = alocMemMatInt(W, H);

for (int i = 1; i < W - 1; i++)
{
for (int j = 1; j < H - 1; j++)
{
clrPixel = bmpPicture->GetPixel(i, j);

rC = clrPixel.ToArgb() & 0x000000FF;
gC = (clrPixel.ToArgb() & 0x0000FF00) >> 8;
bC = (clrPixel.ToArgb() & 0x00FF0000) >> 16;

Y[i][j] = int(rC*0.299 + gC*0.587 + bC*0.114);

bmpPictureGray->SetPixel(i, j, Color::FromArgb(Y[i][j], Y[i][j], Y[i][j]));
}
}

pictureBox2->Left = 20; pictureBox2->Size = System::Drawing::Size(W, H);
pictureBox2->Image = bmpPictureGray; pictureBox2->Visible = true;
}
}
Try this:
1
2
3
4
5
6
array<System::String^>^ strMultipleImages = System::IO::Directory::GetFiles(strFilename, "*.jpg");
for each (String^ strFilename in strMultipleImages)
{
   Bitmap ^ bmpPicture = gcnew Bitmap(strFilename);   
   // use bmpPicture here
}
Hello, thank's for help. Now i try to open all images but unfortunetly i can open just the first img from folder. Below can see the code that i Use.
private: System::Void openAllImagesToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
nSlideOrImg = 0;
timer1->Enabled = false;
int***allimg;
System::IO::DirectoryInfo^ directoryInfo = System::IO::Directory::GetParent("..\\");
// System::String^ strDirPath = directoryInfo->FullName;
// folderBrowserDialog1->SelectedPath = strDirPath;

if (this->folderBrowserDialog1->ShowDialog() == Windows::Forms::DialogResult::OK)
{
String^ strFilename = folderBrowserDialog1->SelectedPath;
array<System::String^>^ strMultipleImages = System::IO::Directory::GetFiles(strFilename, "*.jpg");
for each (String^ strFilename in strMultipleImages)
{
Bitmap ^ bmpPicture = gcnew Bitmap(strFilename);
// use bmpPicture here

nNumberOfImages = strMultipleImages->Length;

H = bmpPicture->Height; W = bmpPicture->Width;
Bitmap ^ bmpPictureGray = gcnew Bitmap(W, H);
Color clrPixel;
double rC, gC, bC;
for (int k = 0; k < nNumberOfImages; k++){
Y = new int*[W];
for (int i = 0; i < W; i++)
{
Y[i] = new int[H];
for (int j = 0; j < H; j++)
{
clrPixel = bmpPicture->GetPixel(i, j);

rC = clrPixel.ToArgb() & 0x000000FF;
gC = (clrPixel.ToArgb() & 0x0000FF00) >> 8;
bC = (clrPixel.ToArgb() & 0x00FF0000) >> 16;

Y[i][j] = int(rC*0.299 + gC*0.587 + bC*0.114);

bmpPictureGray->SetPixel(i, j, Color::FromArgb(Y[i][j], Y[i][j], Y[i][j]));

}
}

}
pictureBox2->Left = 20; pictureBox2->Size = System::Drawing::Size(W, H);
pictureBox2->Image = bmpPictureGray; pictureBox2->Visible = true;
timer1->Enabled = true;

}
}
}


private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) {
nSlideOrImg++;

if (nSlideOrImg == nNumberOfImages)
{
nSlideOrImg = 0;
}
}
Your code looks a bit confusing for me. Can you tell us what you actually want to do?
I want to display in picturebox all pictures from a folder, one by une using a timer. The images are converted to gray. I think for after K is put wrong if I don't use it I diplay first picture from folder but in this case doesn't work, I don' t receive errors but running of program, after i choose the folder from where I want to dispay pictures it remain in this loop and anything is not happen.
I think the right way to do it is to store the image filenames as a form variable as well as the current index. In the event handler for openAllImagesToolStripMenuItem_Click you just get the filenames, store them in the array and set the current index to 0 and enable the timer.

On the timer event you get the current filename, load and convert the bitmap and display it in the picture box, finally you increment the current index. Since converting the bitmap might take some time - depending on the resolution and size - it might be worth to save it for future use.

It would be also a good idea to convert the bitmap in a separate function, so it will be easier to use it in another program. It also makes to code more readable.

Hope this helps.
Topic archived. No new replies allowed.