Ejemplo n.º 1
0
        public static Bitmap Pad(Bitmap image, int newWidth, int newHeight)
        {
            int width  = image.Width;
            int height = image.Height;

            /*
             * It is always guaranteed that,
             *
             *      width < newWidth
             *
             *          and
             *
             *      height < newHeight
             */
            if ((width < newWidth && height < newHeight) ||
                (width < newWidth && height == newHeight) ||
                (width == newWidth && height < newHeight))
            {
                Bitmap paddedImage = Grayscale.CreateGrayscaleImage(newWidth, newHeight);

                BitmapLocker inputImageLocker  = new BitmapLocker(image);
                BitmapLocker paddedImageLocker = new BitmapLocker(paddedImage);

                inputImageLocker.Lock();
                paddedImageLocker.Lock();

                int startPointX = (int)Math.Ceiling((double)(newWidth - width) / (double)2) - 1;
                int startPointY = (int)Math.Ceiling((double)(newHeight - height) / (double)2) - 1;

                for (int y = startPointY; y < (startPointY + height); y++)
                {
                    for (int x = startPointX; x < (startPointX + width); x++)
                    {
                        int xxx = x - startPointX;
                        int yyy = y - startPointY;

                        paddedImageLocker.SetPixel(x, y, inputImageLocker.GetPixel(xxx, yyy));



                        string str = string.Empty;
                    }
                }

                paddedImageLocker.Unlock();
                inputImageLocker.Unlock();

                return(paddedImage);
            }
            else if (width == newWidth && height == newHeight)
            {
                return(image);
            }
            else
            {
                throw new Exception("Pad() -- threw an exception");
            }
        }
Ejemplo n.º 2
0
        public static Bitmap ToBitmap(double[,] input)
        {
            int width  = input.GetLength(0);
            int height = input.GetLength(1);

            Bitmap output = Grayscale.CreateGrayscaleImage(width, height);

            BitmapData data = output.LockBits(new Rectangle(0, 0, width, height),
                                              ImageLockMode.WriteOnly,
                                              output.PixelFormat);

            int pixelSize = System.Drawing.Image.GetPixelFormatSize(PixelFormat.Format8bppIndexed) / 8;

            int offset = data.Stride - width * pixelSize;

            double Min = 0.0;
            double Max = 255.0;

            unsafe
            {
                byte *address = (byte *)data.Scan0.ToPointer();

                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        double v = 255 * (input[x, y] - Min) / (Max - Min);

                        byte value = unchecked ((byte)v);

                        for (int c = 0; c < pixelSize; c++, address++)
                        {
                            *address = value;
                        }
                    }

                    address += offset;
                }
            }

            output.UnlockBits(data);

            return(output);
        }