Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Creating a Bitmap from IntPtr to unmanaged memory: issues [modified]

Creating a Bitmap from IntPtr to unmanaged memory: issues [modified]

Scheduled Pinned Locked Moved C#
questiongraphicsdata-structuresperformancehelp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jon Hulatt
    wrote on last edited by
    #1

    Hi all, I'm trying to do some managed directshow stuff and i'm a bit stuck on some Bitmap things. I'm trying to make a Bitmap in a callback where I am presented with an IntPtr to a block of unmanaged memory which contains my pixel data. I can create the bitmap just fine with one of the Bitmap constructor overloads:-

    Bitmap b = new Bitmap(
    720,
    576,
    -2160,
    PixelFormat.Format24bppRgb,
    (IntPtr)(ip.ToInt32() + 1244160 - 2160)
    );

    (stride and the IntPtr itself are hacked around a bit because my pixel data scans left-right,bottom-top. but that's not important right now). When i've done this, I can create and use the bitmap. However, the Bitmap does not copy the data, and as such when I free the memory for the bitmap data (I have to do this - this is unsafe code), the bitmap is corrupt. Edit: i've also tried the Bitmap.Clone() method- but the new bitmap still references the unmanaged data. So, i've been looking for a way of copying the pixel data into a managed type so that i can keep a reference to it for as long as I need it, and then let the GC free it. But i'm struggling on how to do this. I can easily copy the data to a byte[] array (with Marshal.Copy), but then how can I create a bitmap from the byte array? Or can anyone else see an alternative way of doing this? thanks Jon

    using System.Beer;

    -- modified at 9:18 Thursday 15th June, 2006

    G 1 Reply Last reply
    0
    • J Jon Hulatt

      Hi all, I'm trying to do some managed directshow stuff and i'm a bit stuck on some Bitmap things. I'm trying to make a Bitmap in a callback where I am presented with an IntPtr to a block of unmanaged memory which contains my pixel data. I can create the bitmap just fine with one of the Bitmap constructor overloads:-

      Bitmap b = new Bitmap(
      720,
      576,
      -2160,
      PixelFormat.Format24bppRgb,
      (IntPtr)(ip.ToInt32() + 1244160 - 2160)
      );

      (stride and the IntPtr itself are hacked around a bit because my pixel data scans left-right,bottom-top. but that's not important right now). When i've done this, I can create and use the bitmap. However, the Bitmap does not copy the data, and as such when I free the memory for the bitmap data (I have to do this - this is unsafe code), the bitmap is corrupt. Edit: i've also tried the Bitmap.Clone() method- but the new bitmap still references the unmanaged data. So, i've been looking for a way of copying the pixel data into a managed type so that i can keep a reference to it for as long as I need it, and then let the GC free it. But i'm struggling on how to do this. I can easily copy the data to a byte[] array (with Marshal.Copy), but then how can I create a bitmap from the byte array? Or can anyone else see an alternative way of doing this? thanks Jon

      using System.Beer;

      -- modified at 9:18 Thursday 15th June, 2006

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      If you create a new blank Bitmap, you can use LockBits to pin the bitmap data and get a pointer to it. Could you use that with Marshal.Copy to copy the unmanaged data into the bitmap? --- b { font-weight: normal; }

      F 1 Reply Last reply
      0
      • G Guffa

        If you create a new blank Bitmap, you can use LockBits to pin the bitmap data and get a pointer to it. Could you use that with Marshal.Copy to copy the unmanaged data into the bitmap? --- b { font-weight: normal; }

        F Offline
        F Offline
        Florian Storck
        wrote on last edited by
        #3

        Guffa wrote:

        If you create a new blank Bitmap, you can use LockBits to pin the bitmap data and get a pointer to it. Could you use that with Marshal.Copy to copy the unmanaged data into the bitmap?

        Ups, seems that my reply notification didn't worked... Meanwhile it seems to work using an IntPtr for initialising, but as far as I can see, the new instance allocates also memory, which is completetly unnecessary because the bitmap data are already in an unmanaged buffer, where the IntPtr points to. My initial idea was to avoid any copying issues for performance reasons and just define the bitmap format and set the pointer directly to the image data. This seems to be a lot more complicated used the managed environment. I'm not sure if it would work if the IntPtr would point to a managed location. Any suggestions are welcome. Bye, Florian

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups