C# 内存访问冲突

发布于 2024-08-07 07:49:41 字数 1980 浏览 3 评论 0原文

该代码运行良好,可以多次调用而不会出现任何问题。 但是,如果调整位图大小(变大),则会出现访问冲突。 如果 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等风来 2024-08-14 07:49:41

据我所知,位图大小还受到显示卡性能的限制。我们通常不会超过任何大于 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.

很酷不放纵 2024-08-14 07:49:41

可以确认BMPSize &
BitmapBytes 数组大小完全统计
次。

这是三个参数中的两个。

can confirm that the BMPSize &
BitmapBytes array size tally at all
times.

That's two out of three parameters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文