CreateGrayscaleImage() public static method

Create and initialize new 8 bpp grayscale image.
The method creates new 8 bpp grayscale image and initializes its palette. Grayscale image is represented as Format8bppIndexed image with palette initialized to 256 gradients of gray color.
public static CreateGrayscaleImage ( int width, int height ) : Bitmap
width int Image width.
height int Image height.
return System.Drawing.Bitmap
Example #1
0
        public unsafe Bitmap ToBitmap()
        {
            if (houghMap == null)
            {
                throw new ApplicationException("Hough transformation was not done yet.");
            }
            int        length     = houghMap.GetLength(1);
            int        length2    = houghMap.GetLength(0);
            Bitmap     bitmap     = Image.CreateGrayscaleImage(length, length2);
            BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, length, length2), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
            int        num        = bitmapData.Stride - length;
            float      num2       = 255f / (float)maxMapIntensity;
            byte *     ptr        = (byte *)bitmapData.Scan0.ToPointer();

            for (int i = 0; i < length2; i++)
            {
                int num3 = 0;
                while (num3 < length)
                {
                    *ptr = (byte)System.Math.Min(255, (int)(num2 * (float)houghMap[i, num3]));
                    num3++;
                    ptr++;
                }
                ptr += num;
            }
            bitmap.UnlockBits(bitmapData);
            return(bitmap);
        }
        public unsafe Bitmap ToBitmap()
        {
            if (this.houghMap == null)
            {
                throw new ApplicationException("Hough transformation was not done yet.");
            }
            int        length     = this.houghMap.GetLength(1);
            int        height     = this.houghMap.GetLength(0);
            Bitmap     bitmap     = Image.CreateGrayscaleImage(length, height);
            BitmapData bitmapdata = bitmap.LockBits(new Rectangle(0, 0, length, height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
            int        num3       = bitmapdata.Stride - length;
            float      num4       = 255f / ((float)this.maxMapIntensity);
            byte *     numPtr     = (byte *)bitmapdata.Scan0.ToPointer();

            for (int i = 0; i < height; i++)
            {
                int num6 = 0;
                while (num6 < length)
                {
                    numPtr[0] = (byte)Math.Min(0xff, (int)(num4 * this.houghMap[i, num6]));
                    num6++;
                    numPtr++;
                }
                numPtr += num3;
            }
            bitmap.UnlockBits(bitmapdata);
            return(bitmap);
        }
Example #3
0
        public unsafe Bitmap ToManagedImage(bool makeCopy)
        {
            Bitmap bitmap = null;

            try
            {
                if (!makeCopy)
                {
                    bitmap = new Bitmap(width, height, stride, pixelFormat, imageData);
                    if (pixelFormat == PixelFormat.Format8bppIndexed)
                    {
                        Image.SetGrayscalePalette(bitmap);
                    }
                }
                else
                {
                    bitmap = ((pixelFormat == PixelFormat.Format8bppIndexed) ? Image.CreateGrayscaleImage(width, height) : new Bitmap(width, height, pixelFormat));
                    BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, pixelFormat);
                    int        num        = bitmapData.Stride;
                    int        count      = System.Math.Min(stride, num);
                    byte *     ptr        = (byte *)bitmapData.Scan0.ToPointer();
                    byte *     ptr2       = (byte *)imageData.ToPointer();
                    if (stride != num)
                    {
                        for (int i = 0; i < height; i++)
                        {
                            SystemTools.CopyUnmanagedMemory(ptr, ptr2, count);
                            ptr  += num;
                            ptr2 += stride;
                        }
                    }
                    else
                    {
                        SystemTools.CopyUnmanagedMemory(ptr, ptr2, stride * height);
                    }
                    bitmap.UnlockBits(bitmapData);
                }
                return(bitmap);
            }
            catch (Exception)
            {
                bitmap?.Dispose();
                throw new InvalidImagePropertiesException("The unmanaged image has some invalid properties, which results in failure of converting it to managed image.");
            }
        }
Example #4
0
        public unsafe Bitmap ToBitmap()
        {
            Bitmap     bitmap     = Image.CreateGrayscaleImage(width, height);
            BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
            int        num        = bitmapData.Stride - width;
            double     num2       = fourierTransformed ? System.Math.Sqrt(width * height) : 1.0;
            byte *     ptr        = (byte *)bitmapData.Scan0.ToPointer();

            for (int i = 0; i < height; i++)
            {
                int num3 = 0;
                while (num3 < width)
                {
                    *ptr = (byte)System.Math.Max(0.0, System.Math.Min(255.0, data[i, num3].Magnitude * num2 * 255.0));
                    num3++;
                    ptr++;
                }
                ptr += num;
            }
            bitmap.UnlockBits(bitmapData);
            return(bitmap);
        }
Example #5
0
        public unsafe Bitmap ToBitmap()
        {
            Bitmap     bitmap     = Image.CreateGrayscaleImage(this.width, this.height);
            BitmapData bitmapdata = bitmap.LockBits(new Rectangle(0, 0, this.width, this.height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
            int        num        = bitmapdata.Stride - this.width;
            double     num2       = this.fourierTransformed ? Math.Sqrt((double)(this.width * this.height)) : 1.0;
            byte *     numPtr     = (byte *)bitmapdata.Scan0.ToPointer();

            for (int i = 0; i < this.height; i++)
            {
                int num4 = 0;
                while (num4 < this.width)
                {
                    numPtr[0] = (byte)Math.Max(0.0, Math.Min((double)255.0, (double)((this.data[i, num4].get_Magnitude() * num2) * 255.0)));
                    num4++;
                    numPtr++;
                }
                numPtr += num;
            }
            bitmap.UnlockBits(bitmapdata);
            return(bitmap);
        }
        public unsafe Bitmap ToManagedImage()
        {
            Bitmap     bitmap     = (this.pixelFormat == System.Drawing.Imaging.PixelFormat.Format8bppIndexed) ? Image.CreateGrayscaleImage(this.width, this.height) : new Bitmap(this.width, this.height, this.pixelFormat);
            BitmapData bitmapdata = bitmap.LockBits(new Rectangle(0, 0, this.width, this.height), ImageLockMode.ReadWrite, this.pixelFormat);
            int        stride     = bitmapdata.Stride;
            int        count      = Math.Min(this.stride, stride);
            byte *     dst        = (byte *)bitmapdata.Scan0.ToPointer();
            byte *     src        = (byte *)this.imageData.ToPointer();

            for (int i = 0; i < this.height; i++)
            {
                SystemTools.CopyUnmanagedMemory(dst, src, count);
                dst += stride;
                src += this.stride;
            }
            bitmap.UnlockBits(bitmapdata);
            return(bitmap);
        }