CreateBitmapSourceFromMemorySection拒绝访问

时间:2014-04-18 12:23:58

标签: c# wpf interopbitmapimage

我正在尝试通过磁盘上的简单图像文件构建InteropBitmap,但它不起作用。我收到UnauthorizedAccessException。

我的文件是一个简单的JPEG 1024 * 768,带有24bpp。

代码:

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;

我该如何解决?

0 个答案:

没有答案