bit
bit

Reputation: 963

CreateBitmapSourceFromMemorySection Denied Access

I'm trying to build an InteropBitmap through a simple image file I have on disk, however it doesn't works. I get UnauthorizedAccessException.

My file is a simple JPEG 1024*768 with 24bpp.

The code:

int width = 1024;
int height = 768;
byte[] data = File.readAllBytes("C:\\myImage.jpg");
PixelFormat format = PixelFormats.Rgb24
uint bufferLength = (uint)data.Length;
uint bpp = (uint)(format.BitsPerPixel / 8);

IntPtr section = NativeHelper.CreateFileMapping(new IntPtr(-1), IntPtr.Zero, XNativeMethods.PAGE_READWRITE, 0, bufferLength * bpp, null);
IntPtr map = NativeHelper.MapViewOfFile(vm.section, XNativeMethods.FILE_MAP_ALL_ACCESS, 0, 0, bufferLength);


Marshal.Copy(data, 0, vm.map, data.Length);
int stride = (width * format.BitsPerPixel) / 8;


// Exception here -> UnauthorizedAccessException
var myImage = System.Windows.Interop.Imaging.CreateBitmapSourceFromMemorySection(section, width, height, format, stride, 0) as InteropBitmap;

How can I solve?

Upvotes: 0

Views: 876

Answers (0)

Related Questions