Ejemplo n.º 1
0
        private static Guid GetEncoderId(BitmapFileFormat bitmapFileFormat)
        {
            switch (bitmapFileFormat)
            {
            case BitmapFileFormat.Bmp:
                return(BitmapEncoder.BmpEncoderId);

            case BitmapFileFormat.Png:
                return(BitmapEncoder.PngEncoderId);

            case BitmapFileFormat.Jpeg:
                return(BitmapEncoder.JpegEncoderId);

            case BitmapFileFormat.Tiff:
                return(BitmapEncoder.TiffEncoderId);

            case BitmapFileFormat.Gif:
                return(BitmapEncoder.GifEncoderId);

            case BitmapFileFormat.JpegXR:
                return(BitmapEncoder.JpegXREncoderId);
            }

            return(BitmapEncoder.PngEncoderId);
        }
Ejemplo n.º 2
0
        private static CanvasBitmapFileFormat MapToCanvasBitmapFileFormat(BitmapFileFormat bitmapFileFormat)
        {
            switch (bitmapFileFormat)
            {
            case BitmapFileFormat.Bmp:
                return(CanvasBitmapFileFormat.Bmp);

            case BitmapFileFormat.Png:
                return(CanvasBitmapFileFormat.Png);

            case BitmapFileFormat.Jpeg:
                return(CanvasBitmapFileFormat.Jpeg);

            case BitmapFileFormat.Tiff:
                return(CanvasBitmapFileFormat.Tiff);

            case BitmapFileFormat.Gif:
                return(CanvasBitmapFileFormat.Gif);

            case BitmapFileFormat.JpegXR:
                return(CanvasBitmapFileFormat.JpegXR);

            default: return(CanvasBitmapFileFormat.Auto);
            }
        }
Ejemplo n.º 3
0
        public FastBitmap(string filename)
        {
            if (string.IsNullOrEmpty(filename))
            {
                throw new ArgumentNullException(nameof(filename));
            }

            var bmp = BitmapFileFormat.FromFile(filename);

            if (!bmp.IsValid)
            {
                throw new ImagingException("Invalid File Format! " + filename);
            }

            if (bmp.IsUpsideDown)
            {
                bmp.ReverseScanLines();
            }

            byte[] bytes = bmp.GetBytes();

            _bitmapInfoHeader = Marshal.AllocHGlobal(bytes.Length);
            _dibSection       = _bitmapInfoHeader + (int)bmp.PixelArrayOffset;

            _pixels = (byte *)_dibSection.ToPointer();

            Marshal.Copy(bytes, 0, _bitmapInfoHeader, bytes.Length);

            Width  = bmp.ImageWidth;
            Height = Math.Abs(bmp.ImageHeight);

            Stride = (((bmp.ImageWidth * bmp.BitsPerPixel) + 31) / 32) * 4;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Saves the cropped image to a stream with the specified format.
        /// </summary>
        /// <param name="stream">The target stream.</param>
        /// <param name="bitmapFileFormat">the specified format.</param>
        /// <returns>Task</returns>
        public async Task SaveAsync(IRandomAccessStream stream, BitmapFileFormat bitmapFileFormat)
        {
            if (SourceImage == null)
            {
                return;
            }
            var bitmapEncoder = await BitmapEncoder.CreateAsync(WriteableBitmapExtensions.GetEncoderId(bitmapFileFormat), stream);

            await WriteableBitmapExtensions.CropImageAsync(SourceImage, _currentCroppedRect, bitmapEncoder);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Save the cropped image to a file.
 /// </summary>
 /// <param name="imageFile">The target file.</param>
 /// <returns></returns>
 public async Task SaveAsync(StorageFile imageFile, BitmapFileFormat bitmapFileFormat)
 {
     if (SourceImage == null)
     {
         return;
     }
     using (var fileStream = await imageFile.OpenAsync(FileAccessMode.ReadWrite, StorageOpenOptions.None))
     {
         await SaveAsync(fileStream, bitmapFileFormat);
     }
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Saves the cropped image to a stream with the specified format.
        /// </summary>
        /// <param name="stream">The target stream.</param>
        /// <param name="bitmapFileFormat">the specified format.</param>
        /// <param name="keepRectangularOutput">Whether to always keep rectangular output.</param>
        /// <returns>Task</returns>
        public async Task SaveAsync(IRandomAccessStream stream, BitmapFileFormat bitmapFileFormat, bool keepRectangularOutput = false)
        {
            if (Source == null)
            {
                return;
            }

            if (keepRectangularOutput || CropShape == CropShape.Rectangular)
            {
                await CropImageAsync(Source, stream, _currentCroppedRect, bitmapFileFormat);

                return;
            }

            await CropImageWithShapeAsync(Source, stream, _currentCroppedRect, bitmapFileFormat, CropShape);
        }
Ejemplo n.º 7
0
        static void Main(string[] args)
        {
            var bmp = BitmapFileFormat.FromFile("red.bmp");

            bmp.ReverseScanLines();

            bmp.Save("other.bmp");

            //using (var bmp = new FastBitmap("red.bmp"))
            //{
            //    Console.WriteLine(bmp.GetPixel(5, 5).Compare(new Pixel(0,0,0)));
            //    Console.WriteLine(bmp.GetPixel(9, 6).ToString());
            //    Console.WriteLine(bmp.GetPixel(2, 9).ToString());
            //    Console.WriteLine(bmp.GetPixel(3, 4).ToString());
            //}

            //Console.ReadLine();
        }
Ejemplo n.º 8
0
        private static async Task CropImageWithShapeAsync(WriteableBitmap writeableBitmap, IRandomAccessStream stream, Rect croppedRect, BitmapFileFormat bitmapFileFormat, CropShape cropShape)
        {
            var device       = CanvasDevice.GetSharedDevice();
            var clipGeometry = CreateClipGeometry(device, cropShape, new Size(croppedRect.Width, croppedRect.Height));

            if (clipGeometry == null)
            {
                return;
            }

            CanvasBitmap sourceBitmap = null;

            using (var randomAccessStream = new InMemoryRandomAccessStream())
            {
                await CropImageAsync(writeableBitmap, randomAccessStream, croppedRect, bitmapFileFormat);

                sourceBitmap = await CanvasBitmap.LoadAsync(device, randomAccessStream);
            }

            using (var offScreen = new CanvasRenderTarget(device, (float)croppedRect.Width, (float)croppedRect.Height, 96f))
            {
                using (var drawingSession = offScreen.CreateDrawingSession())
                    using (var markCommandList = new CanvasCommandList(device))
                    {
                        using (var markDrawingSession = markCommandList.CreateDrawingSession())
                        {
                            markDrawingSession.FillGeometry(clipGeometry, Colors.Black);
                        }

                        var alphaMaskEffect = new AlphaMaskEffect
                        {
                            Source    = sourceBitmap,
                            AlphaMask = markCommandList
                        };
                        drawingSession.DrawImage(alphaMaskEffect);
                        alphaMaskEffect.Dispose();
                    }

                clipGeometry.Dispose();
                sourceBitmap.Dispose();
                var pixelBytes    = offScreen.GetPixelBytes();
                var bitmapEncoder = await BitmapEncoder.CreateAsync(GetEncoderId(bitmapFileFormat), stream);

                bitmapEncoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, offScreen.SizeInPixels.Width, offScreen.SizeInPixels.Height, 96.0, 96.0, pixelBytes);
                await bitmapEncoder.FlushAsync();
            }
        }
Ejemplo n.º 9
0
        private static async Task CropImageAsync(WriteableBitmap writeableBitmap, IRandomAccessStream stream, Rect croppedRect, BitmapFileFormat bitmapFileFormat)
        {
            croppedRect.X = Math.Max(croppedRect.X, 0);
            croppedRect.Y = Math.Max(croppedRect.Y, 0);
            var x      = (uint)Math.Floor(croppedRect.X);
            var y      = (uint)Math.Floor(croppedRect.Y);
            var width  = (uint)Math.Floor(croppedRect.Width);
            var height = (uint)Math.Floor(croppedRect.Height);

            using (var sourceStream = writeableBitmap.PixelBuffer.AsStream())
            {
                var buffer = new byte[sourceStream.Length];
                await sourceStream.ReadAsync(buffer, 0, buffer.Length);

                var bitmapEncoder = await BitmapEncoder.CreateAsync(GetEncoderId(bitmapFileFormat), stream);

                bitmapEncoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, (uint)writeableBitmap.PixelWidth, (uint)writeableBitmap.PixelHeight, 96.0, 96.0, buffer);
                bitmapEncoder.BitmapTransform.Bounds = new BitmapBounds
                {
                    X      = x,
                    Y      = y,
                    Width  = width,
                    Height = height
                };
                await bitmapEncoder.FlushAsync();
            }
        }
Ejemplo n.º 10
0
 /// <summary>
 /// This method exports the max possible view of the InfiniteCanvas drawings as offScreen drawings that can be converted to image.
 /// Max is calculated using CanvasDevice.MaximumBitmapSizeInPixels
 /// </summary>
 /// <param name="stream">The target stream.</param>
 /// <param name="bitmapFileFormat">the specified format.</param>
 /// <returns>Task</returns>
 public async Task SaveBitmapAsync(IRandomAccessStream stream, BitmapFileFormat bitmapFileFormat)
 {
     var offScreen = _drawingSurfaceRenderer.ExportMaxOffScreenDrawings();
     await offScreen.SaveAsync(stream, MapToCanvasBitmapFileFormat(bitmapFileFormat));
 }
        private async Task CropImageAsync(WriteableBitmap writeableBitmap, IRandomAccessStream stream, Rect croppedRect, BitmapFileFormat bitmapFileFormat)
        {
            RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
            await renderTargetBitmap.RenderAsync(_imageGrid);

            //var kWidth = (double)renderTargetBitmap.PixelWidth / writeableBitmap.PixelWidth;
            //var kHeight = (double)renderTargetBitmap.PixelHeight / writeableBitmap.PixelHeight;

            var width = (int)Math.Floor(croppedRect.Width);

            croppedRect.X = Math.Max(croppedRect.X, 0);
            croppedRect.Y = Math.Max(croppedRect.Y, 0);
            var x = (int)Math.Floor(croppedRect.X);
            var y = (int)Math.Floor(croppedRect.Y);

            var height = (int)Math.Floor(croppedRect.Height);

            var pixelBuffer = await renderTargetBitmap.GetPixelsAsync();

            //using (var sourceStream = pixelBuffer.AsStream())
            //{
            //    var buffer = new byte[sourceStream.Length];
            //    await sourceStream.ReadAsync(buffer, 0, buffer.Length);
            //    var bitmapEncoder = await BitmapEncoder.CreateAsync(GetEncoderId(bitmapFileFormat), stream);
            //    bitmapEncoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, (uint)renderTargetBitmap.PixelWidth, (uint)renderTargetBitmap.PixelHeight, DisplayInformation.GetForCurrentView().LogicalDpi, DisplayInformation.GetForCurrentView().LogicalDpi, pixelBuffer.ToArray());
            //    bitmapEncoder.BitmapTransform.Bounds = new BitmapBounds
            //    {
            //        X = x,
            //        Y = y,
            //        Width = width,
            //        Height = height
            //    };
            //    await bitmapEncoder.FlushAsync();
            //}


            writeableBitmap = writeableBitmap.RotateFree(ImageRotation);
            writeableBitmap = writeableBitmap.Crop(x, y, width, height);
            //writeableBitmap.WriteTga(stream.AsStream());


            using (var sourceStream = writeableBitmap.PixelBuffer.AsStream())
            {
                var buffer = new byte[sourceStream.Length];
                await sourceStream.ReadAsync(buffer, 0, buffer.Length);

                var encoder = await BitmapEncoder.CreateAsync(GetEncoderId(bitmapFileFormat), stream);

                encoder.BitmapTransform.ScaledHeight = (uint)(ScaledImageValue * writeableBitmap.PixelHeight);
                encoder.BitmapTransform.ScaledWidth  = (uint)(ScaledImageValue * writeableBitmap.PixelWidth);
                encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, (uint)writeableBitmap.PixelWidth, (uint)writeableBitmap.PixelHeight, 96, 96, buffer);

                //    //var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
                //    //encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, (uint)writeableBitmap.PixelWidth, (uint)writeableBitmap.PixelHeight, 96,96, buffer);
                //    //await encoder.FlushAsync();
                //    //    //if (width > bitmapEncoder.BitmapTransform.ScaledWidth)
                //    //    //{
                //    //    //    width = bitmapEncoder.BitmapTransform.ScaledWidth;
                //    //    //}

                //    //    //if (height > bitmapEncoder.BitmapTransform.ScaledHeight)
                //    //    //{
                //    //    //    height = bitmapEncoder.BitmapTransform.ScaledHeight;
                //    //    //}
                //    //encoder.BitmapTransform.Bounds = new BitmapBounds
                //    //{
                //    //    X = x,
                //    //    Y = y,
                //    //    Width = width,
                //    //    Height = height
                //    //};
                await encoder.FlushAsync();
            }
        }