Пример #1
0
        private static BitmapLayer ChangeBackgroundColor(BitmapLayer source, Color color)
        {
            Color controlColor = new Color();

            int bytesPerPixel = (source.Bitmap.Format.BitsPerPixel + 7) / 8;
            int stride        = bytesPerPixel * source.Bitmap.PixelWidth;

            byte[] array = new byte[stride * source.Bitmap.PixelHeight];
            source.Bitmap.CopyPixels(array, stride, 0);
            for (int i = 0; i < array.Length; i += 4)
            {
                if ((array[i] == controlColor.B &&
                     array[i + 1] == controlColor.G &&
                     array[i + 2] == controlColor.R &&
                     array[i + 3] == controlColor.A))
                {
                    array[i]     = color.B;
                    array[i + 1] = color.G;
                    array[i + 2] = color.R;
                    array[i + 3] = color.A;
                }
            }
            source.Bitmap.WritePixels(new Int32Rect(0, 0, source.Bitmap.PixelWidth, source.Bitmap.PixelHeight), array, stride, 0);
            return(source);
        }
Пример #2
0
        public static List <BitmapLayer> SyncMask(List <BitmapLayer> bitmapLayers, BrushType brush, Color color, int diameter, int opacity)
        {
            BitmapLayer bitmapLayer = new BitmapLayer(bitmapLayers[0].LayerHeight, bitmapLayers[0].LayerWidth);

            bitmapLayer.UpdateMask(brush, color, diameter, opacity);
            for (int i = 0; i < bitmapLayers.Count; i++)
            {
                bitmapLayers[i].Stride     = bitmapLayer.Stride;
                bitmapLayers[i].MaskBitmap = bitmapLayer.MaskBitmap;
                bitmapLayers[i].Mask       = bitmapLayer.Mask;
            }
            return(bitmapLayers);
        }
Пример #3
0
        public static void SaveImageWithFormat(BitmapLayer bitmapLayer, string fileName, ImageFileFormat imageFileFormat)
        {
            string fileExtension = imageFileFormat.ToString().ToLower();

            fileName += $".{fileExtension}";
            using (FileStream stream = new FileStream(fileName, FileMode.Create))
            {
                BitmapEncoder encoder = null;
                switch (imageFileFormat)
                {
                case ImageFileFormat.BMP:
                {
                    encoder     = new BmpBitmapEncoder();
                    bitmapLayer = ChangeBackgroundColor(bitmapLayer, Colors.White);
                    break;
                }

                case ImageFileFormat.JPEG:
                {
                    encoder     = new JpegBitmapEncoder();
                    bitmapLayer = ChangeBackgroundColor(bitmapLayer, Colors.White);
                    break;
                }

                case ImageFileFormat.PNG:
                {
                    encoder = new PngBitmapEncoder();
                    break;
                }

                case ImageFileFormat.TIFF:
                {
                    encoder = new TiffBitmapEncoder();
                    break;
                }
                }

                encoder.Frames.Add(BitmapFrame.Create(bitmapLayer.GetWorkspaceImage().Clone()));
                encoder.Save(stream);
            }
        }
Пример #4
0
 private BitmapLayer(BitmapLayer bitmapLayer) : this(bitmapLayer.LayerHeight, bitmapLayer.LayerWidth)
 {
     this.Bitmap = bitmapLayer.Bitmap.Clone();
 }