C# CreateBitmapSourceFromMemorySection拒绝访问
我正试图通过磁盘上的一个简单映像文件来构建一个InteropBitmap,但是它不起作用。我得到未经授权的访问例外 我的文件是一个简单的JPEG 1024*768,24bpp 守则:C# CreateBitmapSourceFromMemorySection拒绝访问,c#,wpf,interopbitmapimage,C#,Wpf,Interopbitmapimage,我正试图通过磁盘上的一个简单映像文件来构建一个InteropBitmap,但是它不起作用。我得到未经授权的访问例外 我的文件是一个简单的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
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;
我该如何解决呢?这里有一个更简单的解决方案来获取BitampSource:当然,但我需要将InteropBitmap用于许多其他用途。看看这里的示例:您不应该使用
map
而不是section
作为CreateBitmapSourceFromMemorySection
的第一个参数吗?我认为问题在于我的数组大小(数组大小oj jpeg文件)不等于1024*768的值,它小于此值。