Dispose() protected method

protected Dispose ( bool disposing ) : void
disposing bool
return void
    public void Save(string filename, SkiaSharp.SKEncodedImageFormat format)
    {
        if (Disposed)
        {
            return;
        }
        SkiaSharp.SKBitmap bitmap = new SkiaSharp.SKBitmap(this.Width, this.Height, false);
        int w        = Width;
        int h        = Height;
        var colorPtr = this.GetColors();

        for (int y = 0; y < h; y++)
        {
            for (int x = 0; x < w; x++)
            {
                var col = *(colorPtr + x + (h - 1 - y) * h);
                bitmap.SetPixel(x, y, new SkiaSharp.SKColor(col.R, col.G, col.B, col.A));
            }
        }
        var img  = SkiaSharp.SKImage.FromBitmap(bitmap);
        var data = img.Encode(format, 100);

        System.IO.File.WriteAllBytes(filename, data.ToArray());
        img.Dispose();
        bitmap.Dispose();
    }
Example #2
0
        public static SKBitmap FromImage(SKImage image)
        {
            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }

            var info = new SKImageInfo(image.Width, image.Height, SKImageInfo.PlatformColorType, image.AlphaType);
            var bmp  = new SKBitmap(info);

            if (!image.ReadPixels(info, bmp.GetPixels(), info.RowBytes, 0, 0))
            {
                bmp.Dispose();
                bmp = null;
            }
            return(bmp);
        }
Example #3
0
        public static SKBitmap Decode(SKCodec codec, SKImageInfo bitmapInfo)
        {
            if (codec == null)
            {
                throw new ArgumentNullException(nameof(codec));
            }

            var bitmap = new SKBitmap(bitmapInfo);
            var result = codec.GetPixels(bitmapInfo, bitmap.GetPixels(out var length));

            if (result != SKCodecResult.Success && result != SKCodecResult.IncompleteInput)
            {
                bitmap.Dispose();
                bitmap = null;
            }
            return(bitmap);
        }
        public BitmapHelper(string path)
        {
            Bitmap bitmap;

            try
            {
                bitmap = Bitmap.Decode(File.OpenRead(path));
            }
            catch (Exception)
            {
                bitmap = new Bitmap(10, 10);
                for (int i = 0; i < 10; i++)
                {
                    for (int w = 0; w < 10; w++)
                    {
                        bitmap.SetPixel(i, w, SKColors.White);
                    }
                }
            }


            IntPtr ptr   = bitmap.GetPixels();
            int    bytes = Math.Abs(bitmap.RowBytes) * bitmap.Height;

            byte[] rgbValues = new byte[bytes];
            System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
            colors = new SKColor[bitmap.Width, bitmap.Height];
            for (int counter = 0; counter < rgbValues.Length; counter += 4)
            {
                int i_am = counter / 4;
                colors[i_am % bitmap.Width, i_am / bitmap.Width]
                    =
                        new SKColor(
                            rgbValues[counter + 2],
                            rgbValues[counter + 1],
                            rgbValues[counter + 0],
                            rgbValues[counter + 3]);
            }
            bitmap.Dispose();
        }
Example #5
0
		public static SKBitmap Decode (SKCodec codec, SKImageInfo bitmapInfo)
		{
			if (codec == null) {
				throw new ArgumentNullException (nameof (codec));
			}

			// construct a color table for the decode if necessary
			SKColorTable colorTable = null;
			int colorCount = 0;
			if (bitmapInfo.ColorType == SKColorType.Index8)
			{
				colorTable = new SKColorTable ();
			}

			// read the pixels and color table
			var bitmap = new SKBitmap (bitmapInfo, colorTable);
			IntPtr length;
			var result = codec.GetPixels (bitmapInfo, bitmap.GetPixels (out length), colorTable, ref colorCount);
			if (result != SKCodecResult.Success && result != SKCodecResult.IncompleteInput) {
				bitmap.Dispose ();
				bitmap = null;
			}
			return bitmap;
		}
Example #6
0
		public SKBitmap Copy (SKColorType colorType)
		{
			var destination = new SKBitmap ();
			if (!SkiaApi.sk_bitmap_copy (Handle, destination.Handle, colorType)) {
				destination.Dispose ();
				destination = null;
			}
			return destination;
		}
Example #7
0
        private static Image ExtractBestImageFromIco(byte[] pb)
        {
            List <GfxImage> l = UnpackIco(pb);

            if ((l == null) || (l.Count == 0))
            {
                return(null);
            }

            long qMax = 0;

            foreach (GfxImage gi in l)
            {
                if (gi.Width == 0)
                {
                    gi.Width = 256;
                }
                if (gi.Height == 0)
                {
                    gi.Height = 256;
                }

                qMax = Math.Max(qMax, (long)gi.Width * (long)gi.Height);
            }

            byte[] pbHdrPng = new byte[] {
                0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A
            };
            byte[] pbHdrJpeg = new byte[] { 0xFF, 0xD8, 0xFF };

            Image imgBest = null;
            int   bppBest = -1;

            foreach (GfxImage gi in l)
            {
                if (((long)gi.Width * (long)gi.Height) < qMax)
                {
                    continue;
                }

                byte[] pbImg = gi.Data;
                Image  img   = null;
                try
                {
                    if ((pbImg.Length > pbHdrPng.Length) &&
                        MemUtil.ArraysEqual(pbHdrPng,
                                            MemUtil.Mid <byte>(pbImg, 0, pbHdrPng.Length)))
                    {
                        img = GfxUtil.LoadImage(pbImg);
                    }
                    else if ((pbImg.Length > pbHdrJpeg.Length) &&
                             MemUtil.ArraysEqual(pbHdrJpeg,
                                                 MemUtil.Mid <byte>(pbImg, 0, pbHdrJpeg.Length)))
                    {
                        img = GfxUtil.LoadImage(pbImg);
                    }
                    else
                    {
                        using (MemoryStream ms = new MemoryStream(pb, false))
                        {
                            using (Icon ico = new Icon(ms, gi.Width, gi.Height))
                            {
                                img = ico.ToBitmap();
                            }
                        }
                    }
                }
                catch (Exception) { Debug.Assert(false); }

                if (img == null)
                {
                    continue;
                }

                if ((img.Width < gi.Width) || (img.Height < gi.Height))
                {
                    Debug.Assert(false);
                    img.Dispose();
                    continue;
                }

                int bpp = GetBitsPerPixel(img.PixelFormat);
                if (bpp > bppBest)
                {
                    if (imgBest != null)
                    {
                        imgBest.Dispose();
                    }

                    imgBest = img;
                    bppBest = bpp;
                }
                else
                {
                    img.Dispose();
                }
            }

            return(imgBest);
        }