I'm kind new to c#.
I have this code:
public static BitmapSource FromNativePointer(IntPtr pData, int w, int h, int ch)
{
System.Windows.Media.PixelFormat format = System.Windows.Media.PixelFormats.Default;
if (ch == 1) format = System.Windows.Media.PixelFormats.Gray8; //grey scale image 0-255
if (ch == 3) format = System.Windows.Media.PixelFormats.Bgr24; //RGB
if (ch == 4) format = System.Windows.Media.PixelFormats.Bgr32; //RGB + alpha
WriteableBitmap wbm = new WriteableBitmap(w, h, (double)96, (double)96, format, null);
CopyMemory(wbm.BackBuffer, pData, (uint)(w * h * ch));
wbm.Lock();
wbm.AddDirtyRect(new Int32Rect(0, 0, wbm.PixelWidth, wbm.PixelHeight));
wbm.Unlock();
return wbm;
}
I'm having some memory problem with the wbm variable. How can I create the variable outside this function and then update its parameters only when I get in the function? Thank you.