65.9K
CodeProject is changing. Read more.
Home

Work with bitmaps faster in C#

starIconstarIconstarIconstarIconstarIcon

5.00/5 (2 votes)

Nov 17, 2011

CPOL
viewsIcon

14571

I got what A.J tried to say. It's about implementing IDisposable in LockBitmap.Basically, it involves changing:public class LockBitmapto: public class LockBitmap : IDisposableThe constructor:public LockBitmap(Bitmap source){ this.source = source;}to:public...

I got what A.J tried to say. It's about implementing IDisposable in LockBitmap. Basically, it involves changing:
public class LockBitmap
to:
public class LockBitmap : IDisposable
The constructor:
public LockBitmap(Bitmap source)
{
    this.source = source;
}
to:
public LockBitmap(Bitmap source)
{
    this.source = source;
    LockBits();
}
and create a new method:
public void Dispose()
{
   UnlockBits();
}
So, the class usage will change from this
LockBitmap lockBitmap = new LockBitmap(bmp);
lockBitmap.LockBits();
(...)
lockBitmap.UnlockBits();
to this
using(LockBitmap lockBitmap = new LockBitmap(bmp))
{
    (...)
}
more elegant, IMO.