I am buliding a GUI around an algorithm. For this purpose I have a Window component which contains a Image component inside it.
The content inside the Image component (imgHolder) is given by a BitmapImage object (_image), which for the purpose of this example is initialized as such:
_image = new BitmapImage();
_image.BeginInit();
_image.CacheOption = BitmapCacheOption.OnLoad;
_image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
_image.UriSource = new Uri("c:\\a1.bmp");
_image.EndInit();
imgHolder.Source = _image;
The main program containing the Window will perform a call to one of the algorithm’s functions from within the Matlab DLL file. This will run one iteration of the algorithm, by the end of which it will write the results to the local bmp file from above (“c:\a1.bmp”). Consequently the underlying data for the image has been updated and I would like for this to be reflected in the imgHolder component. For this purpose I am simply duplicating the code above whenever I return from the function.
The problem with this approach is that the algorithm will have around 100 iterations per run so this would have to happen 100 times. It just doesn’t seem right. I would be creating 100 different bitmap images all sourced from the same file.
Researching this I found out that one could use memorystream instead but I wonder whether this would be a drastical improvement or not as it seems that I would still have to create 100 images as the algorithm executes. Is it not possible to directly modify the underlying buffer somehow? I hear that GetPixel and SetPixel are not very efficient as they set a lock each time they are called. Is it possible perhaps to do this using the same _image object? If this was not a local file, I could’ve used a no cache option and it should’ve worked “by itself”. Having a no cache option with the file being local did not work because of the lock.
Does anyone have a better idea?
Thanks!
I haven’t quite understood if you do have access to the bitmap’s raw data or not, or if it is always written to file. Since you mention SetPixel i guess you have.
So you could perhaps use a WritableBitmap as
_image.Sourceand cyclically update its content by one of its WritePixels methods.I just tried the following example which updates the image on every mouse move with a simple pattern.
XAML:
Code: