change from C++ code to C#
-
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..^^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
-
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
-
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
You don't need to delete it, but since
Image
implementsIDisposable
(and for good reason), you should callDispose
on theImage
instance after you're done with it.Microsoft MVP, Visual C# My Articles
-
You don't need to delete it, but since
Image
implementsIDisposable
(and for good reason), you should callDispose
on theImage
instance after you're done with it.Microsoft MVP, Visual C# My Articles
-
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..^^
.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
-
.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