Without seeing where
mat
comes from, or how it what values it holds it's difficult to say for sure, but I'm guessing it's something fairly simple.
Get rid of the
if(!once)
, you should always unlock your bits.
Call
Invalidate
on your
PictureBox
at the end, that will refresh it.
This statement is weird;
System::Runtime::InteropServices::Marshal::Copy( ptr, rgbValues, 0, bytes );
You copy all the pixels of the newly created image into a buffer,
rgbValues
, but since you just created the image all pixels will be black. And immediately after you overwrite the values in
rgbValues
anyway.
Try changing your method to this (this assumes that these methods are inside your form and that it's called Form1);
delegate void Refresh();
void PictureRefresh()
{
Refresh^ d = gcnew Refresh(this, &Form1::PictureRefreshInternal);
this->Invoke(d);
}
void PictureRefreshInternal()
{
Random^ rnd = gcnew Random();
int once = 0;
Bitmap^ image1;
image1 = gcnew Bitmap(500, 1000,
System::Drawing::Imaging::PixelFormat:: Format24bppRgb);
System::Drawing::Rectangle rect = System::Drawing::Rectangle(0,0,image1->Width,image1->Height);
System::Drawing::Imaging::BitmapData^ bmpData = image1->LockBits( rect,
System::Drawing::Imaging::ImageLockMode::ReadWrite, image1->PixelFormat );
IntPtr ptr = bmpData->Scan0;
int bytes = Math::Abs(bmpData->Stride) * image1->Height;
array<byte>^rgbValues = gcnew array<byte>(bytes);
for ( int counter = 0; counter < rgbValues->Length; counter++ )
{
rgbValues[ counter ] = (byte)rnd->Next();
}
System::Runtime::InteropServices::Marshal::Copy( rgbValues, 0, ptr, bytes );
image1->UnlockBits( bmpData );
pictureBox1->Image= image1;
pictureBox1->Invalidate();
}
That works for me when I call that method on a button click on the form.
If it works for you then the values in
mat
are wrong, if it doesn't work then it's got something to do with you updating the wrong picturebox or not updating at all.
Hope this helps,
Fredrik
Updated; Added a wrapper method to invoke this back to the UI thread.
Updated; Fixed missing delegate type name.