disposing or killing a reference to a Bitmap

  • Thread starter Thread starter abhi.menon
  • Start date Start date
A

abhi.menon

I have a "Plot" button the code associated with which (when Clicked) is
as follows
private: System::Void showPic_Click(System::Object * sender,
System::EventArgs * e)
{
/* code to use Matlab to create a 'bmp' and save to

C:\tmp\matlabFromNET_TRY2\somefile.bmp
*/

Bitmap* MyImage;
MyImage =new Bitmap("C:\\tmp\\matlabFromNET_TRY2\\somefile.bmp");
pictureBox1->Image = MyImage;

}
What basicaly happens is everytime the button is clicked the file
'somefile.bmp' is modified by a Matlab code peice at the start pf the
function, and this file is reloaded into a pictureBox named
'pictureBox1'. The issue I am having is that the first time the button
is clicked the expected image is loaded, but the second time the button
is clicked the old image remains. This is because the bmp is locked
when its loaded into the picture box and no other user or the Matlab
code at the beginning can save changes to the bmp when the button is
clicked again. How would I free the Bitmap at the end of the function
to ensure that the next time this button is clicked, Matlab will be
able to make changes to the 'bmp'
 
What basicaly happens is everytime the button is clicked the file
'somefile.bmp' is modified by a Matlab code peice at the start pf the
function, and this file is reloaded into a pictureBox named
'pictureBox1'. The issue I am having is that the first time the button
is clicked the expected image is loaded, but the second time the button
is clicked the old image remains. This is because the bmp is locked
when its loaded into the picture box and no other user or the Matlab
code at the beginning can save changes to the bmp when the button is
clicked again. How would I free the Bitmap at the end of the function
to ensure that the next time this button is clicked, Matlab will be
able to make changes to the 'bmp'

A simple solution would be to create a secondary bitmap, and copy the Matlab
bitmap into it.
You would have the overhead of an extra bitmap, but none of the locking
issues.

Then you can simply Dispose the image at the end of your functioncall.

--

Kind regards,
Bruno van Dooren
(e-mail address removed)
Remove only "_nos_pam"
 
sound advice!
worked perfectly fine...n was so simple!(one of those times when u
think...damn why didnt I think of tht!)

Thanks..

if someone could use the code here it is
private: System::Void showPic_Click(System::Object * sender,
System::EventArgs * e)
{

Bitmap* MyImage, *MyImage2;
MyImage =new
Bitmap("C:\\tmp\\matlabFromNET_TRY2\\somefile.bmp");
MyImage2=new Bitmap(MyImage);
pictureBox1->Image = MyImage2;
MyImage->Dispose();
}

----------------------------------------------------------
 
Back
Top