I have a C++/CLI dll and one of the methods requires a pointer and its buffer size as its parameters.
// Example..
uint LoadImageFromMemory(const unsigned char * const BufferPtr, const int BufferSize);
Bitmap Img = Bitmap.FromFile(@"C:/my.jpg") as Bitmap;
.
.
// Convert the 'Img' to byte* pointer.
byte[] TempByte = new byte[TempStorage.Height * TempStorage.Width * 3];
byte[] ActualByte = null;
System.Drawing.Imaging.BitmapData m_pImageData = null;
// Converting Bitmap to Byte array...
try
{
m_pImageData = TempStorage.LockBits(new Rectangle(0, 0, TempStorage.Width, TempStorage.Height),
System.Drawing.Imaging.ImageLockMode.ReadWrite,
System.Drawing.Imaging.PixelFormat.Format24bppRgb);
System.IntPtr pScan = m_pImageData.Scan0; // 5
int nStride = m_pImageData.Stride;// 6
ActualByte = new byte[m_pImageData.Height * m_pImageData.Width * 3];
unsafe
{
byte* pBuffer = (byte*)(void*)pScan;// 7
int noffset = nStride - TempStorage.Width * 3;
int count = 0;
for (int nY = 0; nY < TempStorage.Height; nY++)
{
for (int nX = 0; nX < TempStorage.Width; nX++)
{
ActualByte[count] = pBuffer[0];
ActualByte[count + 1] = pBuffer[1];
ActualByte[count + 2] = pBuffer[2];
count += 3;
pBuffer += 3;
}
pBuffer += noffset;
}
}
}
finally
{
if (m_pImageData != null)
{
TempStorage.UnlockBits(m_pImageData);
}
}
.
.
// At the end...
.
.
fixed (byte* ptrAdd = ActualByte)
{
// this second parameter ( below ) is problematic...
rst = CppAPI.LoadImageFromMemory(ptrAdd, ?????);
}
How can I get the buffer size?
Since I have no idea of what exactly the buffer size means here, I tried to pass these values into the second parameter, wild-guessing.
Unfortunately, none of them worked.
I have the image, its bitmap object and its pointer in C#. Aren't these sufficient to get the buffer size?
After I read the comments Ken White mentioned, it comes into my mind that converting job itself could be wrong in the first place.
So I got rid of this code below
try
{
m_pImageData = TempStorage.LockBits(new Rectangle(0, 0, TempStorage.Width, TempStorage.Height),
System.Drawing.Imaging.ImageLockMode.ReadWrite,
System.Drawing.Imaging.PixelFormat.Format24bppRgb);
System.IntPtr pScan = m_pImageData.Scan0; // 5
int nStride = m_pImageData.Stride;// 6
ActualByte = new byte[m_pImageData.Height * m_pImageData.Width * 3];
unsafe
{
byte* pBuffer = (byte*)(void*)pScan;// 7
int noffset = nStride - TempStorage.Width * 3;
int count = 0;
for (int nY = 0; nY < TempStorage.Height; nY++)
{
for (int nX = 0; nX < TempStorage.Width; nX++)
{
ActualByte[count] = pBuffer[0];
ActualByte[count + 1] = pBuffer[1];
ActualByte[count + 2] = pBuffer[2];
count += 3;
pBuffer += 3;
}
pBuffer += noffset;
}
}
}
finally
{
if (m_pImageData != null)
{
TempStorage.UnlockBits(m_pImageData);
}
}
and decided to use MemoryStream
instead.
using (MemoryStream ms = new MemoryStream())
{
Img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
ActualByte = ms.ToArray();
}
It works fine now and the second parameter value was supposed to be the byte array length, as Ken White said in his comment.
// This should be...
CppAPI.LoadImageFromMemory(ptrAdd, ?????);
// Like this..
CppAPI.LoadImageFromMemory(ptrAdd, ActualByte.length);
You defined the array as a byte array, so it is exactly as large as you defined it. m_pImageData.Height * m_pImageData.Width * 3
bytes long.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.