Esempio n. 1
0
        public BitmapImage Correct(BitmapImage image, double value)
        {
            Bitmap correctImage = BitmapConventer.ToBitmap(image);

            GC.Collect();
            GC.WaitForPendingFinalizers();

            int imageWidth  = correctImage.Width;
            int imageHeight = correctImage.Height;

            Rectangle  rect    = new Rectangle(0, 0, imageWidth, imageHeight);
            BitmapData bmpData = correctImage.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
            IntPtr     ptr     = bmpData.Scan0;
            int        bytes   = Math.Abs(bmpData.Stride) * imageHeight;

            byte[] pixelBytes = new byte[bytes];
            System.Runtime.InteropServices.Marshal.Copy(ptr, pixelBytes, 0, bytes);


            Parallel.For(0, bytes,
                         new ParallelOptions {
                MaxDegreeOfParallelism = Environment.ProcessorCount
            },
                         i =>
            {
                pixelBytes[i] = GetCorrectPixel(pixelBytes[i], value);
            });

            System.Runtime.InteropServices.Marshal.Copy(pixelBytes, 0, ptr, bytes);
            correctImage.UnlockBits(bmpData);
            return(BitmapConventer.ToBitmapImage(correctImage));
        }
Esempio n. 2
0
        public BitmapImage GetIndexImage(Layer[] map, Palette palette = Palette.Green)
        {
            try
            {
                if (colorPalette != null)
                {
                    colorPalette.SetPalette(palette);
                }
                int imageWidth  = map[0].image.PixelWidth;
                int imageHeight = map[0].image.PixelWidth;

                // Минимальный размер изображение
                for (int i = 1; i < map.Length; i++)
                {
                    if (imageHeight > map[i].image.PixelHeight)
                    {
                        imageHeight = map[i].image.PixelHeight;
                    }
                    if (imageWidth > map[i].image.PixelWidth)
                    {
                        imageWidth = map[i].image.PixelWidth;
                    }
                }

                Bitmap   indexMap      = new Bitmap(imageWidth, imageHeight);
                Bitmap[] mapImage      = new Bitmap[map.Length];
                Color[]  mapImagePixel = new Color[map.Length];
                for (int i = 0; i < map.Length; i++)
                {
                    mapImage[i] = new Bitmap(BitmapConventer.ToBitmap(map[i].image), imageWidth, imageHeight);
                }

                for (int i = 0; i < imageWidth; i++)
                {
                    for (int j = 0; j < imageHeight; j++)
                    {
                        for (int k = 0; k < map.Length; k++)
                        {
                            mapImagePixel[k] = mapImage[k].GetPixel(i, j);
                        }
                        indexMap.SetPixel(i, j, GetIndex(mapImagePixel));
                    }
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();

                return(BitmapConventer.ToBitmapImage(indexMap));
            }
            catch
            {
                MessageBox.Show("Произошла внуренняя ошибка расчета", "Ошибка", MessageBoxButton.OK,
                                MessageBoxImage.Error);
                GC.Collect();
                GC.WaitForPendingFinalizers();
                return(null);
            }
        }