Joncash / HanboAOMClassLibrary

Hanbo AOM Class Library
15 stars 12 forks source link

AccessViolation in HanboAOMClassLibrary/Hanbo.Helper/ImageConventer.cs #1

Open KettuDevelopment opened 5 years ago

KettuDevelopment commented 5 years ago

Hello,

thanks for this Halcon Helper Libary. U made my Day. Maybe u know a solution for my Problem?

image

Ill upload my current TestProject in my Git.

derco0n commented 4 years ago

Same Problem here. I assume this has something to do with either a newer operation system (Windows 10), a security patch or a newer .Net-Framework...

derco0n commented 4 years ago

Same Problem here. I assume this has something to do with either a newer operation system (Windows 10), a security patch or a newer .Net-Framework...

It is not needed to do it like this. In fact the constructor if Drawing.Image can take height, width, stride, pixelformat and pointer)

derco0n commented 4 years ago

Same Problem here. I assume this has something to do with either a newer operation system (Windows 10), a security patch or a newer .Net-Framework...

It is not needed to do it like this. In fact the constructor if Drawing.Image can take height, width, stride, pixelformat and pointer)

something like the code below:

public static ColorPalette greyscale_8bpp_Palette() {
Bitmap bmp = new Bitmap(1, 1, PixelFormat.Format8bppIndexed); ColorPalette cp = bmp.Palette; Color[] entries = cp.Entries; for (int c = 0; c < 255; c++) { entries[c] = Color.FromArgb(c, c, c); } return cp; }

public static Bitmap ConvertHalconImageToBitmap(HImage halconImage, bool isColor) { HTuple type, width, height; IntPtr ptr; Bitmap img = null; int bitsperpixel; PixelFormat pf;

if (isColor) { //Colorized (Not tested...)
HImage interleaved = halconImage.InterleaveChannels("argb", "match", 255); //if colorized
ptr = interleaved.GetImagePointer1(out type, out width, out height); bitsperpixel = 32; pf = PixelFormat.Format32bppArgb; } else { // Grey ptr = halconImage.GetImagePointer1(out type, out width, out height); bitsperpixel = 8; pf = PixelFormat.Format8bppIndexed;
}

// https://medium.com/@oleg.shipitko/what-does-stride-mean-in-image-processing-bba158a72bcd int stride = 4 ((width bitsperpixel + 31) / 32);

img = new Bitmap(width, height, stride, pf, ptr); //Generate a new Bitmap from the data in the HIMage-memory-space

//Set the image to a 8-Bit-per-pixel grayscale color palette
if (bitsperpixel == 8) { // If we wouldn't do this here, the image would contain 8 bits per pixel but in random colors. img.Palette = greyscale_8bpp_Palette(); } //img.Save("C:\Temp\test.bmp", ImageFormat.Bmp); //DEBUG saves the final image return img; }