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; ImageSizeAddress++; } // THIS IS THE LINE THAT FAILS Marshal.Copy(BitmapBytes, 0, ViewFile, Convert.ToInt32(BMPSize)); BitmapBytes = null; DIBImageSize = null; FileMappingCreated = false; }

Recommended Answers

All 5 Replies

please use CODE tags to post code. It makes it a LOT easier to read through your code :)

Sorry...
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;
ImageSizeAddress++;
}

// THIS IS THE LINE THAT FAILS
Marshal.Copy(BitmapBytes, 0, ViewFile, Convert.ToInt32(BMPSize));

BitmapBytes = null;
DIBImageSize = null;
FileMappingCreated = false;
}

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;
}

I've just resized the bmp from 355k to 467k and I get this violation. Anything below 355k will be fine. If on the first use, the bitmap is, say, 900k then as long as I don't resize above that, everything works fine. This is called numerous times by the app and only fails when resizing to a bigger bitmap from the initial size at start up

I don't exactly know what you are doing with all this code, but it occurs to me that you save the bitmap to a stream, assign your BitmapBytes byte array from the stream array ( byte[] BitmapBytes = stream.ToArray(); ), then you are trying to copy the bigger (resized) image into the same size BitmapBytes array, which is not allocated large enough. Try resizing it to the larger size ( BitmapBytes = new byte[BMPSize] ) prior to the Marshal.Copy call.

I make no assumptions about what you are doing with all this code, only that the error you are getting is being caused by an attempt to write memory outside the allocated array size.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.