Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# CreateBitmapSourceFromMemorySection拒绝访问_C#_Wpf_Interopbitmapimage - Fatal编程技术网

C# CreateBitmapSourceFromMemorySection拒绝访问

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

我正试图通过磁盘上的一个简单映像文件来构建一个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.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的值,它小于此值。