我有一些代码可以将 a 复制Bitmap
到 Direct3DTexture
中以呈现视频。当系统负载过重时,我偶尔会AccessViolationException
接到Bitmap.LockBits
.
这是一个非常简单的例子:
// Get a PixelFormat.Format24bppRgb image
Bitmap bitmap24 = getVideoFrame();
// Copy into a 32bpp ARGB texture
BitmapData bitmapData = null;
try
{
// Why am I allowed to do this?
bitmapData = this.bitmap24.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.ReadOnly,
PixelFormat.Format32bppArgb);
// Blit from bitmapData to texture32
Texture texture32 = convertBitmapToTexture(bitmapData);
}
finally
{
if (bitmapData != null)
this.bitmap.UnlockBits(bitmapData);
}
为什么我知道位图格式是可以调用Bitmap.LockBits
和传入?我假设它必须自动从每像素 3 个字节扩展到 4 个。PixelFormat.Format32bppArgb
PixelFormat.Format24bppRgb
LockBits
LockBits
扩展会导致这些异常吗?为什么它只是偶尔发生?