Пример #1
0
        public static Bitmap ToBitmap(Grid g1, Grid g2, int scalefactor, InterpolationMode interpolation)
        {
            uint[] buffer;
            Bitmap bmp    = BitmapHelper.CreateNewManagedBitmap(g1.Width, g1.Height, out buffer);
            float  spread = Math.Min(g1.Width, g1.Height) / (1 << scalefactor);

            float min = -spread;
            float max = spread;

            int width  = bmp.Width;
            int height = bmp.Height;

            int idx = 0;

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    float dst = g1.grid[idx].Dist() - g2.grid[idx].Dist();

                    dst = dst < 0
                        ? -128 * (dst - min) / min
                        : 128 + 128 * dst / max;

                    uint channel = (uint)Math.Max(0, Math.Min(255, dst));
                    uint val     = (channel << 24) | (channel << 16) | (channel << 8) | channel;

                    buffer[idx] = val;
                    idx++;
                }
            }

            return(BitmapHelper.ResizeBitmap(bmp, g1.Width >> scalefactor, g1.Height >> scalefactor, interpolation));
        }
        public static Bitmap ResizeBitmapToFit(Bitmap sourceBitmap, int width, int height, float aspectRatio)
        {
            int renderheight, renderwidth;

            if ((aspectRatio <= 1.0f))
            {
                renderheight = height;
                renderwidth  = (int)(height * aspectRatio);
            }
            else
            {
                renderheight = (int)(width * (1.0f / aspectRatio));
                renderwidth  = width;
            }
            return(BitmapHelper.ResizeBitmap(sourceBitmap, renderwidth, renderheight, InterpolationMode.Bilinear));
        }