0

I'm trying to convert an Halcon object to a bitmap and found this piece of code online: https://github.com/Joncash/HanboAOMClassLibrary/blob/master/Hanbo.Helper/ImageConventer.cs

/// <summary>
/// Halcon Image .NET Bitmap
/// </summary>
/// <param name="halconImage"></param>
/// <returns></returns>
public static Bitmap ConvertHalconImageToBitmap(HObject halconImage, bool isColor)
{
    if (halconImage == null)
    {
        throw new ArgumentNullException("halconImage");
    }

    HTuple pointerRed = null;
    HTuple pointerGreen = null;
    HTuple pointerBlue = null;
    HTuple type;
    HTuple width;
    HTuple height;

    // Halcon
    var pixelFormat = (isColor) ? PixelFormat.Format32bppRgb : PixelFormat.Format8bppIndexed;
    if (isColor)
        HOperatorSet.GetImagePointer3(halconImage, out pointerRed, out pointerGreen, out pointerBlue, out type, out width, out height);
    else
        HOperatorSet.GetImagePointer1(halconImage, out pointerBlue, out type, out width, out height);


    Bitmap bitmap = new Bitmap((Int32)width, (Int32)height, pixelFormat);

    BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
    int bytes = Math.Abs(bmpData.Stride) * bitmap.Height;
    byte[] rgbValues = new byte[bytes];


    IntPtr ptrB = new IntPtr(pointerBlue);
    IntPtr ptrG = IntPtr.Zero;
    IntPtr ptrR = IntPtr.Zero;
    if (pointerGreen != null) ptrG = new IntPtr(pointerGreen);
    if (pointerRed != null) ptrR = new IntPtr(pointerRed);


    int channels = (isColor) ? 3 : 1;

    // Stride
    int strideTotal = Math.Abs(bmpData.Stride);
    int unmapByes = strideTotal - ((int)width * channels);
    for (int i = 0, offset = 0; i < bytes; i += channels, offset++)
    {
        if ((offset + 1) % width == 0)
        {
            i += unmapByes;
        }

        rgbValues[i] = Marshal.ReadByte(ptrB, offset); //where I get the accesviolation
        if (isColor)
        {
            rgbValues[i + 1] = Marshal.ReadByte(ptrG, offset);
            rgbValues[i + 2] = Marshal.ReadByte(ptrR, offset);
        }

    }

    Marshal.Copy(rgbValues, 0, bmpData.Scan0, bytes);
    bitmap.UnlockBits(bmpData);
    return bitmap;
}

But when I try to run it, it get a AccesViolationExeption when reading or writing to protected memory. Anyone know why?

I've debugged and made sure that the IntPtr are not null

Armitius
  • 1
  • 1
  • Are you sure the image input is a 32bppRGB if colour is selected or 8bppIndexed if not selected? You can get this error if for example the image is a 24bpp image and something tries to access byte 4 of the last pixel (which is outside the array and therefore could cause access violation) – Steven Wood Apr 17 '23 at 14:58

0 Answers0