C# 内存访问冲突
该代码运行良好,可以多次调用而不会出现任何问题。 但是,如果调整位图大小(变大),则会出现访问冲突。 如果 bimap 变小,情况就不是这样了。
我可以确认 BMPSize & BitmapBytes 数组大小始终进行计数。
任何人都可以对此有所了解吗?
public void SetBitmap(Bitmap bmp)
{
UInt32 BMPSize = Convert.ToUInt32(bmp.Height * bmp.Width * 4);
BMPSize += 0x36;
if (!FileMappingCreated)
{
MemoryFileHandle = CreateFileMapping((IntPtr)0, (IntPtr)0,
PageProtection.ReadWrite, 0, BMPSize, SharedName);
if (MemoryFileHandle != null)
{
SetNamedSecurityInfo(SharedName, SE_OBJECT_TYPE.SE_KERNEL_OBJECT, SECURITY_INFORMATION.DACL_SECURITY_INFORMATION,
IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
ViewFile = MapViewOfFile(MemoryFileHandle, FILE_MAP_WRITE, 0, 0, 0);
if (MemoryFileHandle == IntPtr.Zero)
{
CloseHandle(MemoryFileHandle);
}
else
{
FileMappingCreated = true;
}
}
}
MemoryStream stream = new MemoryStream();
bmp.Save(stream, ImageFormat.Bmp);
byte[] BitmapBytes = stream.ToArray();
// BMP SIZE Value 4bytes long see internet for DIB structure.
byte[] DIBImageSize = null;
int ImageSizeAddress = 0x22;
DIBImageSize = BitConverter.GetBytes(Convert.ToInt32(BMPSize));
// put DIBImageSize into array starting at address 0x22
for (int i = 0; i < DIBImageSize.Length; i++)
{
BitmapBytes[ImageSizeAddress] = DIBImageSize[i];
ImageSizeAddress++;
}
// THIS IS THE LINE THAT FAILS
Marshal.Copy(BitmapBytes, 0, ViewFile, Convert.ToInt32(BMPSize));
BitmapBytes = null;
DIBImageSize = null;
FileMappingCreated = false;
}
非常感谢大家。
派罗·保罗
This code works fine and can be called numerous times without any problems.
However, if the bitmap is resized (made bigger), I get an access violation.
This is not the case if the bimap is made smaller.
I can confirm that the BMPSize & BitmapBytes array size tally at all times.
Can anyone spread any light on this, please?
public void SetBitmap(Bitmap bmp)
{
UInt32 BMPSize = Convert.ToUInt32(bmp.Height * bmp.Width * 4);
BMPSize += 0x36;
if (!FileMappingCreated)
{
MemoryFileHandle = CreateFileMapping((IntPtr)0, (IntPtr)0,
PageProtection.ReadWrite, 0, BMPSize, SharedName);
if (MemoryFileHandle != null)
{
SetNamedSecurityInfo(SharedName, SE_OBJECT_TYPE.SE_KERNEL_OBJECT, SECURITY_INFORMATION.DACL_SECURITY_INFORMATION,
IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
ViewFile = MapViewOfFile(MemoryFileHandle, FILE_MAP_WRITE, 0, 0, 0);
if (MemoryFileHandle == IntPtr.Zero)
{
CloseHandle(MemoryFileHandle);
}
else
{
FileMappingCreated = true;
}
}
}
MemoryStream stream = new MemoryStream();
bmp.Save(stream, ImageFormat.Bmp);
byte[] BitmapBytes = stream.ToArray();
// BMP SIZE Value 4bytes long see internet for DIB structure.
byte[] DIBImageSize = null;
int ImageSizeAddress = 0x22;
DIBImageSize = BitConverter.GetBytes(Convert.ToInt32(BMPSize));
// put DIBImageSize into array starting at address 0x22
for (int i = 0; i < DIBImageSize.Length; i++)
{
BitmapBytes[ImageSizeAddress] = DIBImageSize[i];
ImageSizeAddress++;
}
// THIS IS THE LINE THAT FAILS
Marshal.Copy(BitmapBytes, 0, ViewFile, Convert.ToInt32(BMPSize));
BitmapBytes = null;
DIBImageSize = null;
FileMappingCreated = false;
}
Many thanks to all.
PyroPaul
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,位图大小还受到显示卡性能的限制。我们通常不会超过任何大于 1024 x 1024 像素的纹理/位图尺寸。如果您的位图大于此大小,则会出现一些奇怪的错误。
你的位图有多大?尝试将其切成碎片以一块一块地装载。
As I know, the bitmap size is also limited by the capability of the display card. We normally do not exceed any texture/ bitmap size larger than 1024 x 1024px. Some strange errors will occur if your bitmap is larger than this size.
How big is your Bitmap? Try to cut it into pieces to load it piece by piece.
这是三个参数中的两个。
That's two out of three parameters.