hi please help, c++ windows form code PictureBox

Hesham AbdMalek Ramadan 1 Reputation point
2021-12-14T22:28:22.247+00:00

hello need help, need to display a Mat image into a PictureBox my code goes like this:

private: System::Void Open_Click_1(System::Object^ sender, System::EventArgs^ e)
{
openFileDialog1->Filter = "Images Files | *.JPG; *.PNG";
if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
{
MessageBox::Show(openFileDialog1->FileName, "Path:");

		IntPtr p = Runtime::InteropServices::Marshal::StringToHGlobalAnsi(openFileDialog1->FileName);  
		const char* path = (const char*)p.ToPointer();  

		src = imread(path, 0);	}  

I tried this:

//the code above
. . .

src = imread(path, 0);

		System::Drawing::Graphics^ graphics = pictureBox1->CreateGraphics();  
		System::IntPtr ptr(src.ptr());  
		System::Drawing::Bitmap^ b = gcnew System::Drawing::Bitmap(src.cols, src.rows, src.step, System::Drawing::Imaging::PixelFormat::Format24bppRgb, ptr);  
		System::Drawing::RectangleF rect(0, 0, pictureBox1->Width, pictureBox1->Height);  
		graphics->DrawImage(b, rect);  

}

the output:

157691-2021-12-15.png

As shown the same image 3 times beside each other how to change that to show the image once or is there a better way to do it, thanks.

@Viorel , thank you sir

Developer technologies C++
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 122.5K Reputation points
    2021-12-14T22:40:11.893+00:00

    Before experimenting with OpenCV, maybe use a simpler method:

    pictureBox1->ImageLocation = openFileDialog1->FileName;
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.