0

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

我的文件是 24bpp 的简单 JPEG 1024*768。

编码:

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;

我该如何解决?

4

0 回答 0