public void InsertImageFromByteArrayCustomSize()
        {
            //ExStart
            //ExFor:DocumentBuilder.InsertImage(Byte[], Double, Double)
            //ExSummary:Shows how to import an image into a document from a byte array, with a custom size.
            Document        doc     = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);

#if NETSTANDARD2_0 || __MOBILE__
            using (SkiaSharp.SKBitmap bitmap = SkiaSharp.SKBitmap.Decode(ImageDir + "Aspose.Words.gif"))
            {
                using (SkiaSharp.SKFileWStream fs =
                           new SkiaSharp.SKFileWStream(MyDir + "Artifacts/InsertImageFromByteArrayCustomSize.png"))
                {
                    bitmap.PeekPixels().Encode(fs, SKEncodedImageFormat.Png, 100);
                }

                builder.InsertImage(bitmap, ConvertUtil.PixelToPoint(250), ConvertUtil.PixelToPoint(144));
                builder.Document.Save(MyDir + "Artifacts/Image.CreateFromByteArrayCustomSize.doc");
            }
#else
// Prepare a byte array of an image.
            using (Image image = Image.FromFile(ImageDir + "Aspose.Words.gif"))
            {
                using (MemoryStream imageBytes = new MemoryStream())
                {
                    image.Save(imageBytes, ImageFormat.Png);

                    builder.InsertImage(imageBytes, ConvertUtil.PixelToPoint(250), ConvertUtil.PixelToPoint(144));
                    builder.Document.Save(MyDir + @"\Artifacts\Image.CreateFromByteArrayCustomSize.doc");
                }
            }
#endif
            //ExEnd
        }
Exemple #2
0
 public static bool Resize(SKBitmap dst, SKBitmap src, SKBitmapResizeMethod method)
 {
     using (var srcPix = src.PeekPixels())
         using (var dstPix = dst.PeekPixels()) {
             return(SKPixmap.Resize(dstPix, srcPix, method));            // && dst.InstallPixels (dstPix);
         }
 }
Exemple #3
0
		public bool ScalePixels (SKBitmap destination, SKFilterQuality quality)
		{
			if (destination == null) {
				throw new ArgumentNullException (nameof (destination));
			}

			using (var dstPix = destination.PeekPixels ()) {
				return ScalePixels (dstPix, quality);
			}
		}
Exemple #4
0
        public static bool Encode(SKWStream dst, SKBitmap src, SKEncodedImageFormat format, int quality)
        {
            if (dst == null)
            {
                throw new ArgumentNullException(nameof(dst));
            }
            if (src == null)
            {
                throw new ArgumentNullException(nameof(src));
            }

            using (var pixmap = new SKPixmap()) {
                return(src.PeekPixels(pixmap) && Encode(dst, pixmap, format, quality));
            }
        }
Exemple #5
0
        public SKData Encode(SKPixelSerializer serializer)
        {
            if (serializer == null)
            {
                throw new ArgumentNullException(nameof(serializer));
            }

            // try old data
            var encoded = EncodedData;

            if (encoded != null)
            {
                if (serializer.UseEncodedData(encoded.Data, (ulong)encoded.Size))
                {
                    return(encoded);
                }
                else
                {
                    encoded.Dispose();
                    encoded = null;
                }
            }

            // get new data (raster)
            if (!IsTextureBacked)
            {
                using (var pixmap = PeekPixels()) {
                    return(serializer.Encode(pixmap));
                }
            }

            // get new data (texture / gpu)
            // this involves a copy from gpu to cpu first
            if (IsTextureBacked)
            {
                var info = new SKImageInfo(Width, Height, ColorType, AlphaType, ColorSpace);
                using (var temp = new SKBitmap(info))
                    using (var pixmap = temp.PeekPixels()) {
                        if (pixmap != null && ReadPixels(pixmap, 0, 0))
                        {
                            return(serializer.Encode(pixmap));
                        }
                    }
            }

            // some error
            return(null);
        }
        public void InsertImageFromByteArrayRelativePosition()
        {
            //ExStart
            //ExFor:DocumentBuilder.InsertImage(Byte[], RelativeHorizontalPosition, Double, RelativeVerticalPosition, Double, Double, Double, WrapType)
            //ExSummary:Shows how to import an image into a document from a byte array, also using relative positions.
            Document        doc     = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);

#if NETSTANDARD2_0 || __MOBILE__
            using (SkiaSharp.SKBitmap bitmap = SkiaSharp.SKBitmap.Decode(ImageDir + "Aspose.Words.gif"))
            {
                using (SkiaSharp.SKFileWStream fs =
                           new SkiaSharp.SKFileWStream(MyDir + "Artifacts/InsertImageFromByteArrayCustomSize.png"))
                {
                    bitmap.PeekPixels().Encode(fs, SKEncodedImageFormat.Png, 100);
                }

                builder.InsertImage(bitmap, RelativeHorizontalPosition.Margin, 100, RelativeVerticalPosition.Margin, 100, 200, 100, WrapType.Square);
                builder.Document.Save(MyDir + "Artifacts/Image.CreateFromByteArrayCustomSize.doc");
            }
#else
            // Prepare a byte array of an image.
            using (Image image = Image.FromFile(ImageDir + "Aspose.Words.gif"))
            {
                using (MemoryStream imageBytes = new MemoryStream())
                {
                    image.Save(imageBytes, ImageFormat.Png);

                    builder.InsertImage(imageBytes, RelativeHorizontalPosition.Margin, 100,
                                        RelativeVerticalPosition.Margin, 100, 200, 100, WrapType.Square);
                    builder.Document.Save(MyDir + @"\Artifacts\Image.CreateFromByteArrayRelativePosition.doc");
                }
            }
#endif
            //ExEnd
        }
Exemple #7
0
        public bool CopyTo(SKBitmap destination, SKColorType colorType)
        {
            if (destination == null)
            {
                throw new ArgumentNullException(nameof(destination));
            }

            if (!CanCopyTo(colorType))
            {
                return(false);
            }

            var srcPM = PeekPixels();

            if (srcPM == null)
            {
                return(false);
            }

            var dstInfo = srcPM.Info.WithColorType(colorType);

            switch (colorType)
            {
            case SKColorType.Rgb565:
                // CopyTo() is not strict on alpha type. Here we set the src to opaque to allow
                // the call to ReadPixels() to succeed and preserve this lenient behavior.
                if (srcPM.AlphaType != SKAlphaType.Opaque)
                {
                    srcPM = srcPM.WithAlphaType(SKAlphaType.Opaque);
                }
                dstInfo.AlphaType = SKAlphaType.Opaque;
                break;

            case SKColorType.RgbaF16:
                // The caller does not have an opportunity to pass a dst color space.
                // Assume that they want linear sRGB.
                dstInfo.ColorSpace = SKColorSpace.CreateSrgbLinear();
                if (srcPM.ColorSpace == null)
                {
                    // We can't do a sane conversion to F16 without a dst color space.
                    // Guess sRGB in this case.
                    srcPM = srcPM.WithColorSpace(SKColorSpace.CreateSrgb());
                }
                break;
            }

            var tmpDst = new SKBitmap();

            if (!tmpDst.TryAllocPixels(dstInfo))
            {
                return(false);
            }

            var dstPM = tmpDst.PeekPixels();

            if (dstPM == null)
            {
                return(false);
            }

            // We can't do a sane conversion from F16 without a src color space. Guess sRGB in this case.
            if (srcPM.ColorType == SKColorType.RgbaF16 && dstPM.ColorSpace == null)
            {
                dstPM = dstPM.WithColorSpace(SKColorSpace.CreateSrgb());
            }

            // ReadPixels does not yet support color spaces with parametric transfer functions. This
            // works around that restriction when the color spaces are equal.
            if (colorType != SKColorType.RgbaF16 && srcPM.ColorType != SKColorType.RgbaF16 && dstPM.ColorSpace == srcPM.ColorSpace)
            {
                dstPM = dstPM.WithColorSpace(null);
                srcPM = srcPM.WithColorSpace(null);
            }

            if (!srcPM.ReadPixels(dstPM))
            {
                return(false);
            }

            destination.Swap(tmpDst);

            return(true);
        }
Exemple #8
0
        public bool CopyTo(SKBitmap destination, SKColorType colorType)
        {
            // TODO: instead of working on `destination` directly, we should
            //       create a temporary bitmap and then inject the data

            if (destination == null)
            {
                throw new ArgumentNullException(nameof(destination));
            }

            if (!CanCopyTo(colorType))
            {
                return(false);
            }

            SKPixmap srcPM = PeekPixels();

            if (srcPM == null)
            {
                return(false);
            }

            SKImageInfo dstInfo = srcPM.Info.WithColorType(colorType);

            switch (colorType)
            {
            case SKColorType.Rgb565:
                // CopyTo() is not strict on alpha type. Here we set the src to opaque to allow
                // the call to ReadPixels() to succeed and preserve this lenient behavior.
                if (srcPM.AlphaType != SKAlphaType.Opaque)
                {
                    srcPM = srcPM.WithAlphaType(SKAlphaType.Opaque);
                }
                dstInfo.AlphaType = SKAlphaType.Opaque;
                break;

            case SKColorType.RgbaF16:
                // The caller does not have an opportunity to pass a dst color space.
                // Assume that they want linear sRGB.
                dstInfo.ColorSpace = SKColorSpace.CreateSrgbLinear();
                if (srcPM.ColorSpace == null)
                {
                    // We can't do a sane conversion to F16 without a dst color space.
                    // Guess sRGB in this case.
                    srcPM = srcPM.WithColorSpace(SKColorSpace.CreateSrgb());
                }
                break;
            }

            destination.Reset();              // TODO: is this needed?
            if (!destination.TryAllocPixels(dstInfo, colorType == SKColorType.Index8 ? ColorTable : null))
            {
                return(false);
            }

            SKPixmap dstPM = destination.PeekPixels();

            if (dstPM == null)
            {
                return(false);
            }

            // We can't do a sane conversion from F16 without a src color space. Guess sRGB in this case.
            if (srcPM.ColorType == SKColorType.RgbaF16 && dstPM.ColorSpace == null)
            {
                dstPM = dstPM.WithColorSpace(SKColorSpace.CreateSrgb());
            }

            // ReadPixels does not yet support color spaces with parametric transfer functions. This
            // works around that restriction when the color spaces are equal.
            if (colorType != SKColorType.RgbaF16 && srcPM.ColorType != SKColorType.RgbaF16 && dstPM.ColorSpace == srcPM.ColorSpace)
            {
                dstPM = dstPM.WithColorSpace(null);
                srcPM = srcPM.WithColorSpace(null);
            }

            if (!srcPM.ReadPixels(dstPM))
            {
                return(false);
            }

            return(true);
        }