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. Managed C++/CLI
  4. Saving Bitmap image data to HGLOBAL [modified]

Saving Bitmap image data to HGLOBAL [modified]

Scheduled Pinned Locked Moved Managed C++/CLI
graphicscsharpwinformsdebuggingperformance
34 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.
  • A alleyes 0

    I made a small change: void* pMyGlobal = GlobalLock(MyHGlobal); UnmanagedMemoryStream^ ms = gcnew UnmanagedMemoryStream((unsigned char*)(pMyGlobal), GlobalSize(gPtrImage)); MyImage->Save(ms, System::Drawing::Imaging::ImageFormat::Bmp); It builds but then I get ExternalException was unhandled in GDI+

    M Offline
    M Offline
    Mark Salsbery
    wrote on last edited by
    #23

    alleyes wrote:

    It builds but then I get ExternalException was unhandled in GDI+

    I can't debug that for you from here, but I suspect not enough allocated on the HGLOBAL. So maybe something like this:

    MemoryStream^ ms = gcnew MemoryStream();
    MyImage->Save(ms, System::Drawing::Imaging::ImageFormat::Bmp);
    if (GlobalSize(MyHGlobal) < ms->Length)
    {
        GlobalRealloc(MyHGlobal, ms->Length, 0);
    }
    void \*pMyGlobalMemory = GlobalLock(MyHGlobal);
    UnmanagedMemoryStream^ ums = gcnew UnmanagedMemoryStream((unsigned char \*)pMyGlobalMemory, GlobalSize(MyHGlobal));
    ms->WriteTo(ums);
    GlobalUnlock(MyHGlobal);
    

    Mark Salsbery Microsoft MVP - Visual C++ :java:

    A 2 Replies Last reply
    0
    • M Mark Salsbery

      alleyes wrote:

      It builds but then I get ExternalException was unhandled in GDI+

      I can't debug that for you from here, but I suspect not enough allocated on the HGLOBAL. So maybe something like this:

      MemoryStream^ ms = gcnew MemoryStream();
      MyImage->Save(ms, System::Drawing::Imaging::ImageFormat::Bmp);
      if (GlobalSize(MyHGlobal) < ms->Length)
      {
          GlobalRealloc(MyHGlobal, ms->Length, 0);
      }
      void \*pMyGlobalMemory = GlobalLock(MyHGlobal);
      UnmanagedMemoryStream^ ums = gcnew UnmanagedMemoryStream((unsigned char \*)pMyGlobalMemory, GlobalSize(MyHGlobal));
      ms->WriteTo(ums);
      GlobalUnlock(MyHGlobal);
      

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      A Offline
      A Offline
      alleyes 0
      wrote on last edited by
      #24

      Mark Salsbery wrote:

      I can't debug that for you from here

      No of course not. I was suspecting perhaps an image already in memory was the issue and maybe that needed a dispose method called, not sure. Thanks for the re-size tip :-D

      1 Reply Last reply
      0
      • M Mark Salsbery

        alleyes wrote:

        It builds but then I get ExternalException was unhandled in GDI+

        I can't debug that for you from here, but I suspect not enough allocated on the HGLOBAL. So maybe something like this:

        MemoryStream^ ms = gcnew MemoryStream();
        MyImage->Save(ms, System::Drawing::Imaging::ImageFormat::Bmp);
        if (GlobalSize(MyHGlobal) < ms->Length)
        {
            GlobalRealloc(MyHGlobal, ms->Length, 0);
        }
        void \*pMyGlobalMemory = GlobalLock(MyHGlobal);
        UnmanagedMemoryStream^ ums = gcnew UnmanagedMemoryStream((unsigned char \*)pMyGlobalMemory, GlobalSize(MyHGlobal));
        ms->WriteTo(ums);
        GlobalUnlock(MyHGlobal);
        

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        A Offline
        A Offline
        alleyes 0
        wrote on last edited by
        #25

        NotSupportedException unhandled. Stream does not support writing? Since when? :confused:

        M 1 Reply Last reply
        0
        • A alleyes 0

          NotSupportedException unhandled. Stream does not support writing? Since when? :confused:

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #26

          Sorry try another constructor: ...gcnew UnmanagedMemoryStream((unsigned char *)pMyGlobalMemory, GlobalSize(MyHGlobal), GlobalSize(MyHGlobal), FileAccess.ReadWrite);

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          A 1 Reply Last reply
          0
          • M Mark Salsbery

            Sorry try another constructor: ...gcnew UnmanagedMemoryStream((unsigned char *)pMyGlobalMemory, GlobalSize(MyHGlobal), GlobalSize(MyHGlobal), FileAccess.ReadWrite);

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            A Offline
            A Offline
            alleyes 0
            wrote on last edited by
            #27

            Ugh! Still getting an AccessViolation Exception. That can either mean not enough memory available or is that this memory can't be written to. I see the CanWrite, CanSeek flags are TRUE. Capacity and Length are both the same - 1658934

            modified on Thursday, January 21, 2010 9:22 AM

            M 1 Reply Last reply
            0
            • A alleyes 0

              Ugh! Still getting an AccessViolation Exception. That can either mean not enough memory available or is that this memory can't be written to. I see the CanWrite, CanSeek flags are TRUE. Capacity and Length are both the same - 1658934

              modified on Thursday, January 21, 2010 9:22 AM

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #28

              Did you use FileAccess.ReadWrite? I'm only guessing here since I don't know what line the exception occurs on...

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              A 1 Reply Last reply
              0
              • M Mark Salsbery

                Did you use FileAccess.ReadWrite? I'm only guessing here since I don't know what line the exception occurs on...

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                A Offline
                A Offline
                alleyes 0
                wrote on last edited by
                #29

                Hi, This is what I have so far:

                MemoryStream^ ms = gcnew MemoryStream();
                MyImage->Save(ms, System::Drawing::Imaging::ImageFormat::Bmp);
                ms->Seek(0, SeekOrigin::Begin);

                if (GlobalSize(MyGlobal) < ms->Length)
                {
                GlobalReAlloc(MyGlobal, (LONG)ms->Length, 0);
                }

                void* pMyGlobal = (UCHAR*)MyGlobal;
                UnmanagedMemoryStream^ ums = gcnew UnmanagedMemoryStream((UCHAR*)pMyGlobal, GlobalSize(MyGlobal), GlobalSize(MyGlobal), FileAccess::ReadWrite);
                ums->Position = 0;
                ms->WriteTo(ums);

                It occurs on the line: ms->WriteTo(ums); This is even without setting the Position of the streams. I did this because just before the Call to WriteTo, the position was set to Length.

                M 1 Reply Last reply
                0
                • A alleyes 0

                  Hi, This is what I have so far:

                  MemoryStream^ ms = gcnew MemoryStream();
                  MyImage->Save(ms, System::Drawing::Imaging::ImageFormat::Bmp);
                  ms->Seek(0, SeekOrigin::Begin);

                  if (GlobalSize(MyGlobal) < ms->Length)
                  {
                  GlobalReAlloc(MyGlobal, (LONG)ms->Length, 0);
                  }

                  void* pMyGlobal = (UCHAR*)MyGlobal;
                  UnmanagedMemoryStream^ ums = gcnew UnmanagedMemoryStream((UCHAR*)pMyGlobal, GlobalSize(MyGlobal), GlobalSize(MyGlobal), FileAccess::ReadWrite);
                  ums->Position = 0;
                  ms->WriteTo(ums);

                  It occurs on the line: ms->WriteTo(ums); This is even without setting the Position of the streams. I did this because just before the Call to WriteTo, the position was set to Length.

                  M Offline
                  M Offline
                  Mark Salsbery
                  wrote on last edited by
                  #30

                  Where did this come from? void* pMyGlobal = (UCHAR*)MyGlobal; Casting a handle to a pointer is bad. For an HGLOBAL it's bad unless you know for sure GMEM_FIXED was used to allocate the memory. Much safer to use GlobalLock to get the pointer.

                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                  A 1 Reply Last reply
                  0
                  • M Mark Salsbery

                    Where did this come from? void* pMyGlobal = (UCHAR*)MyGlobal; Casting a handle to a pointer is bad. For an HGLOBAL it's bad unless you know for sure GMEM_FIXED was used to allocate the memory. Much safer to use GlobalLock to get the pointer.

                    Mark Salsbery Microsoft MVP - Visual C++ :java:

                    A Offline
                    A Offline
                    alleyes 0
                    wrote on last edited by
                    #31

                    Sorry I threw you there. The lock is already done earlier during the creating of the original images. Didn't make sense to call another GlobalLock on it again. The call to GlobalAlloc is GlobalAlloc(GMEM_MOVEABLE, Size) You have to call GlobalLock on the existing memory again?

                    M 2 Replies Last reply
                    0
                    • A alleyes 0

                      Sorry I threw you there. The lock is already done earlier during the creating of the original images. Didn't make sense to call another GlobalLock on it again. The call to GlobalAlloc is GlobalAlloc(GMEM_MOVEABLE, Size) You have to call GlobalLock on the existing memory again?

                      M Offline
                      M Offline
                      Mark Salsbery
                      wrote on last edited by
                      #32

                      alleyes wrote:

                      The lock is already done earlier during the creating of the original images. Didn't make sense to call another GlobalLock on it again. The call to GlobalAlloc is GlobalAlloc(GMEM_MOVEABLE, Size)

                      Then you need to use the pointer returned by the GlobalLock() call.

                      Mark Salsbery Microsoft MVP - Visual C++ :java:

                      1 Reply Last reply
                      0
                      • A alleyes 0

                        Sorry I threw you there. The lock is already done earlier during the creating of the original images. Didn't make sense to call another GlobalLock on it again. The call to GlobalAlloc is GlobalAlloc(GMEM_MOVEABLE, Size) You have to call GlobalLock on the existing memory again?

                        M Offline
                        M Offline
                        Mark Salsbery
                        wrote on last edited by
                        #33

                        alleyes wrote:

                        Didn't make sense to call another GlobalLock on it again.

                        There's nothing wrong with locking again as long as you have an equal number of unlock calls. GlobalRealloc on a locked handle can only reallocate memory in the same place so has more chance of failing, since there may not be enough room in that place. For best results the handle should be unlocked before reallocating then locked again when you need the pointer.

                        Mark Salsbery Microsoft MVP - Visual C++ :java:

                        A 1 Reply Last reply
                        0
                        • M Mark Salsbery

                          alleyes wrote:

                          Didn't make sense to call another GlobalLock on it again.

                          There's nothing wrong with locking again as long as you have an equal number of unlock calls. GlobalRealloc on a locked handle can only reallocate memory in the same place so has more chance of failing, since there may not be enough room in that place. For best results the handle should be unlocked before reallocating then locked again when you need the pointer.

                          Mark Salsbery Microsoft MVP - Visual C++ :java:

                          A Offline
                          A Offline
                          alleyes 0
                          wrote on last edited by
                          #34

                          Sincere thanks Mark :-D When I have the time, I think I'll do all this in managed code by Marshalling the native HGLOBAL. But for now it works well.

                          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