我对 c# 很陌生。
我有这个代码:
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;
}
我对 wbm 变量有一些内存问题。如何在此函数之外创建变量,然后仅在进入该函数时才更新其参数?谢谢你。