Without seeing where
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
, you should always unlock your bits.
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,
, but since you just created the image all pixels will be black. And immediately after you overwrite the values in
Try changing your method to this (this assumes that these methods are inside your form and that it's called Form1);
delegate void Refresh();
Refresh^ d = gcnew Refresh(this, &Form1::PictureRefreshInternal);
Random^ rnd = gcnew Random();
int once = 0;
image1 = gcnew Bitmap(500, 1000,
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 );
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
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,
Updated; Added a wrapper method to invoke this back to the UI thread.
Updated; Fixed missing delegate type name.