Hello there,
I've a little problem.
Imagine the code here:
public partial class Window1 : Window
{
fastbitmap fastbm;
WriteableBitmap bitmapBuffer;
volatile bool IsWorking = false;
public Window1()
{
fastbm = new fastbitmap("Some Image.jpg");
bitmapBuffer = fastbm.toWriteableBitmap();
Photo p = new Photo();
System.Windows.Controls.Image i = p.SetPicture(ref writeablebm);
RenderOptions.SetBitmapScalingMode(i, BitmapScalingMode.HighQuality);
p.HorizontalAlignment = HorizontalAlignment.Center;
p.VerticalAlignment = VerticalAlignment.Center;
this.myGrid.Children.Add(p);
Some.Event += new Some.Event(Someevent);
}
void Someevent(object sender, Eventargs args)
{
if(!IsWorking)
{
MyDelegate instance = new MyDelegate(UpdateBitmap);
this.Dispatcher.Invoke(instance, null);
}
}
delegate void MyDelegate();
void UpdateBitmap()
{
IsWorking = true;
fastbm.dosomething();
fastbm.toWriteableBitmap(ref bitmapBuffer);
IsWorking = false;
}
}
this is just a simplified version of what I'm doing, but this code works.
Unfortunately it's not visible!
The backbuffer of the writeablebitmap is being filled so quickly everytime by the event that the image doesn't have the chance to update.
Probably somewhere in the middle of transferring the backbuffer to the frontbuffer (so it can be displayed) the backbuffer is being changed again.
Also when I create a new WriteableBitmap every time and load it up the Image simply doesn't load fast enough to show.
I'd like to bind the resources together tho, since that's more efficient. So I need to know a way to detect when the Image is finished loading and the "IsWorking" value can be set to false again.
So far I haven't found any variable or event that does this properly.
Does anyone of you know a way to detect when an Image is finished loading and rendering, so I can pause the update till that time.
I would seriously appreciate any help.
Thanks in advance!
edit: fastbitmap functions to write to writeablebitmap (on request)
they use unsafe code since the other way of doing it didn't work out properly... some weird .net documentation imho
public WriteableBitmap toWriteableBitmap()
{
WriteableBitmap bm = new WriteableBitmap(this.Width, this.Height, 300, 300, PixelFormats.Bgr24, null);
unsafe
{
bm.Lock();
int currentPixel = -1;
byte* pStart = (byte*)(void*)bm.BackBuffer;
Colour c;
for (int y = 0; y < Height; y++)
{
for (int x = 0; x < Width; x++)
{
currentPixel++;
c = bmdata[x][y];
*(pStart + currentPixel * 3) = c.B;
*(pStart + currentPixel * 3 + 1) = c.G;
*(pStart + currentPixel * 3 + 2) = c.R;
}
}
bm.AddDirtyRect(new Int32Rect(0, 0, Width, Height));
bm.Unlock();
}
return bm;
}
public void toWriteableBitmap(ref WriteableBitmap bm)
{
if (bm == null)
{
bm = toWriteableBitmap();
return;
}
if (bm.Format != PixelFormats.Bgr24)
throw new Exception("Pixelformat should be Bgr24!");
if (bm.PixelWidth!= this.Width || bm.PixelHeight != this.Height)
throw new Exception("WriteableBitmap doesn't have the same size as fastbitmap");
unsafe
{
bm.Lock();
int currentPixel = -1;
byte* pStart = (byte*)(void*)bm.BackBuffer;
Colour c;
for (int y = 0; y < Height; y++)
{
for (int x = 0; x < Width; x++)
{
currentPixel++;
c = bmdata[x][y];
*(pStart + currentPixel * 3) = c.B;
*(pStart + currentPixel * 3 + 1) = c.G;
*(pStart + currentPixel * 3 + 2) = c.R;
}
}
bm.AddDirtyRect(new Int32Rect(0, 0, Width, Height));
bm.Unlock();
}
}