I'm currently working on a vision-based system using Halcon. After successful processing, I'd like the cropped out gray value images in HImage-Format to be converted into Bitmaps. The function I have (in "managed" area) kind of achieves that but the Bitmap turns out to be distorted. How do I solve this issue?
HImage image_temp;
HRegion ROI;
HTuple pointer, type, width, height;
ROI.GenRectangle1(row1, col1, row2, col2);
image_temp = sourceImg->ReduceDomain(ROI);
image_temp = image_temp.CropDomain();
GetImagePointer1(image_temp, &pointer, &type, &width, &height);
if (image_temp.CountObj() < 1)
{
throw gcnew Exception("ERROR: Failed attempt at HImage2Bitmap");
}
Imaging::ColorPalette^ palette;
Bitmap^ curBitmap = gcnew Bitmap((Int32)width, (Int32)height, Imaging::PixelFormat::Format8bppIndexed);
Drawing::Rectangle^ rect = gcnew Drawing::Rectangle(0, 0, width, height);
Imaging::BitmapData^ imageData = curBitmap->LockBits(*rect, Imaging::ImageLockMode::ReadOnly, curBitmap->PixelFormat);
int PixelSize = Drawing::Bitmap::GetPixelFormatSize(imageData->PixelFormat) / 8;
//Define the Buffer used to store image data
auto buffer = gcnew cli::array<byte>(curBitmap->Width * curBitmap->Height);
//Copy image data into Buffer
Runtime::InteropServices::Marshal::Copy((IntPtr)&pointer, buffer, 0, buffer->Length);
//byte* ImgBuf = (byte*)&buffer;
pin_ptr<byte> ImgBuf = (byte*)&buffer;
IntPtr^ ptr = gcnew IntPtr(ImgBuf);
Bitmap^ bitmap = gcnew Drawing::Bitmap(curBitmap->Width, curBitmap->Height, curBitmap->Width, Imaging::PixelFormat::Format8bppIndexed, *ptr);
palette = bitmap->Palette;
for (int Index = 0; Index <= byte::MaxValue; Index++)
{
palette->Entries[Index] = Drawing::Color::FromArgb(byte::MaxValue, Index, Index, Index);
}
bitmap->Palette = palette;
bitmap->Save("D:\\bitmap_test.bmp");
return bitmap;
The code tested in C# (by using an unsafe block and fixing the byte-pointer) and the write_image() function of Halcon deliver the following Bitmap, which is how it should look like:
And the bitmap created by the same function in C++/CLI looks like this: