0

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?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
bit
  • 934
  • 1
  • 11
  • 32
  • Here is simpler solution to get a BitampSource: http://stackoverflow.com/questions/3418323/bitmapsource-from-file – HABJAN Apr 18 '14 at 12:42
  • Sure, but I need to use InteropBitmap for many other purpose. – bit Apr 18 '14 at 12:44
  • Take a look at samples here: http://social.msdn.microsoft.com/Forums/vstudio/en-US/a8d86cd0-10cc-4349-aa9c-e62d7d066508/createbitmapsourcefrommemorysection?forum=wpf – HABJAN Apr 18 '14 at 12:52
  • Shouldn't you use `map` rather than `section` as the first argument to `CreateBitmapSourceFromMemorySection`? – 500 - Internal Server Error Apr 18 '14 at 13:30
  • I think the problem is that my array size (array size oj jpeg file) is not equivalent to value of 1024*768, it's smaller than this. – bit Apr 18 '14 at 13:46

0 Answers0