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. change from C++ code to C#

change from C++ code to C#

Scheduled Pinned Locked Moved C#
csharpc++helpquestion
7 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.
  • R Offline
    R Offline
    Rulala
    wrote on last edited by
    #1

    Hi All.. I have some code in c++ and change to c# as below: uint dImageSize; byte pImage; byte pImagePtr; //statment **pImage = (byte)malloc(dImageSize);** //statment **free(m_pImage);** Any one could help me? Thank you. hello ALL..^^

    C 1 Reply Last reply
    0
    • R Rulala

      Hi All.. I have some code in c++ and change to c# as below: uint dImageSize; byte pImage; byte pImagePtr; //statment **pImage = (byte)malloc(dImageSize);** //statment **free(m_pImage);** Any one could help me? Thank you. hello ALL..^^

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Rulala wrote: pImage = (byte)malloc(dImageSize); This is C, do you know why C++ was not used ? malloc and free are used to allocate memory in C, in C++, new and delete are used. In C#, delete is not required, and the code would look like this: using System.Drawing; Image image = new Image(); That's it. Image.FromFile could be used instead to create an image in memory from a file. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder

      R H 2 Replies Last reply
      0
      • C Christian Graus

        Rulala wrote: pImage = (byte)malloc(dImageSize); This is C, do you know why C++ was not used ? malloc and free are used to allocate memory in C, in C++, new and delete are used. In C#, delete is not required, and the code would look like this: using System.Drawing; Image image = new Image(); That's it. Image.FromFile could be used instead to create an image in memory from a file. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder

        R Offline
        R Offline
        Rulala
        wrote on last edited by
        #3

        Hi Christian , Thank you for your instruction. About Image image = new Image(); If I need to define fixed size of memory(dImageSize). How do I need to do? Thank again.^^ Hello All , I'm rulala.

        1 Reply Last reply
        0
        • C Christian Graus

          Rulala wrote: pImage = (byte)malloc(dImageSize); This is C, do you know why C++ was not used ? malloc and free are used to allocate memory in C, in C++, new and delete are used. In C#, delete is not required, and the code would look like this: using System.Drawing; Image image = new Image(); That's it. Image.FromFile could be used instead to create an image in memory from a file. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          You don't need to delete it, but since Image implements IDisposable (and for good reason), you should call Dispose on the Image instance after you're done with it.

          Microsoft MVP, Visual C# My Articles

          R 1 Reply Last reply
          0
          • H Heath Stewart

            You don't need to delete it, but since Image implements IDisposable (and for good reason), you should call Dispose on the Image instance after you're done with it.

            Microsoft MVP, Visual C# My Articles

            R Offline
            R Offline
            Rulala
            wrote on last edited by
            #5

            Hi Heath , Thank you for your reply . I'm sorry that I didn't have detailed description. How do I provide fixed size of buffer(memory) for code to use? If I create Image() , Can I control memory size to Image? Thank again. hello ALL..^^

            H 1 Reply Last reply
            0
            • R Rulala

              Hi Heath , Thank you for your reply . I'm sorry that I didn't have detailed description. How do I provide fixed size of buffer(memory) for code to use? If I create Image() , Can I control memory size to Image? Thank again. hello ALL..^^

              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #6

              .NET is called a "managed environment" because the CLR manages the memory - you should not. The .NET Framework class library is there to encapsulate all this for you, and you should never assume a certain number of bytes for an image since different pixel formats and options can change that. If you want, you can P/Invoke unmanaged APIs and go back to managing all the memory yourself, but then what's the point of using a managed language like C# to target the CLR or another CLI implementation like Mono?

              Microsoft MVP, Visual C# My Articles

              R 1 Reply Last reply
              0
              • H Heath Stewart

                .NET is called a "managed environment" because the CLR manages the memory - you should not. The .NET Framework class library is there to encapsulate all this for you, and you should never assume a certain number of bytes for an image since different pixel formats and options can change that. If you want, you can P/Invoke unmanaged APIs and go back to managing all the memory yourself, but then what's the point of using a managed language like C# to target the CLR or another CLI implementation like Mono?

                Microsoft MVP, Visual C# My Articles

                R Offline
                R Offline
                Rulala
                wrote on last edited by
                #7

                Thank you very much . You provide very good instruction. Thank you...^^ hello ALL..^^

                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