public static void Clear(this BitmapContext context, ColorInt color)
        {
            int colr = color.ToPreMultAlphaColor();

            int[] pixels = context.Pixels;
            int   w      = context.Width;
            int   h      = context.Height;
            int   len    = w * ARGB_SIZE;

            // Fill first line
            for (int x = 0; x < w; x++)
            {
                pixels[x] = colr;
            }

            // Copy first line
            int blockHeight = 1;
            int y           = 1;

            while (y < h)
            {
                BitmapContext.BlockCopy(context, 0, context, y * len, blockHeight * len);
                y          += blockHeight;
                blockHeight = Math.Min(2 * blockHeight, h - y);
            }
        }
Exemple #2
0
 /// <summary>
 /// Copies color information from an ARGB byte array into this WriteableBitmap starting at a specific buffer index.
 /// </summary>
 /// <param name="bmp">The WriteableBitmap.</param>
 /// <param name="offset">The starting index in the buffer.</param>
 /// <param name="count">The number of bytes to copy from the buffer.</param>
 /// <param name="buffer">The color buffer as byte ARGB values.</param>
 /// <returns>The WriteableBitmap that was passed as parameter.</returns>
 public static BitmapBuffer FromByteArray(this BitmapBuffer bmp, byte[] buffer, int offset, int count)
 {
     using (BitmapContext context = bmp.GetBitmapContext())
     {
         BitmapContext.BlockCopy(buffer, offset, context, 0, count);
         return(bmp);
     }
 }
 /// <summary>
 /// Clones the specified WriteableBitmap.
 /// </summary>
 /// <param name="bmp">The WriteableBitmap.</param>
 /// <returns>A copy of the WriteableBitmap.</returns>
 public static BitmapBuffer Clone(this BitmapBuffer bmp)
 {
     using (BitmapContext srcContext = bmp.GetBitmapContext(ReadWriteMode.ReadOnly))
     {
         BitmapBuffer result = BitmapBufferFactory.New(srcContext.Width, srcContext.Height);
         using (var destContext = result.GetBitmapContext())
         {
             BitmapContext.BlockCopy(srcContext, 0, destContext, 0, srcContext.Length * ARGB_SIZE);
         }
         return(result);
     }
 }
        /// <summary>
        /// Creates a new resized WriteableBitmap.
        /// </summary>
        /// <param name="bmp">The WriteableBitmap.</param>
        /// <param name="width">The new desired width.</param>
        /// <param name="height">The new desired height.</param>
        /// <param name="interpolation">The interpolation method that should be used.</param>
        /// <returns>A new WriteableBitmap that is a resized version of the input.</returns>
        public static BitmapBuffer Resize(this BitmapBuffer bmp, int width, int height, Interpolation interpolation)
        {
            using (BitmapContext srcContext = bmp.GetBitmapContext(ReadWriteMode.ReadOnly))
            {
                int[] pd = Resize(srcContext, srcContext.Width, srcContext.Height, width, height, interpolation);

                BitmapBuffer result = BitmapBufferFactory.New(width, height);
                using (BitmapContext dstContext = result.GetBitmapContext())
                {
                    BitmapContext.BlockCopy(pd, 0, dstContext, 0, ARGB_SIZE * pd.Length);
                }
                return(result);
            }
        }
Exemple #5
0
        /// <summary>
        /// Copies the Pixels from the WriteableBitmap into a ARGB byte array starting at a specific Pixels index.
        /// </summary>
        /// <param name="bmp">The WriteableBitmap.</param>
        /// <param name="offset">The starting Pixels index.</param>
        /// <param name="count">The number of Pixels to copy, -1 for all</param>
        /// <returns>The color buffer as byte ARGB values.</returns>
        public static byte[] ToByteArray(this BitmapBuffer bmp, int offset, int count)
        {
            using (BitmapContext context = bmp.GetBitmapContext(ReadWriteMode.ReadOnly))
            {
                if (count == -1)
                {
                    // Copy all to byte array
                    count = context.Length;
                }

                int    len    = count * ARGB_SIZE;
                byte[] result = new byte[len]; // ARGB
                BitmapContext.BlockCopy(context, offset, result, 0, len);
                return(result);
            }
        }
        /// <summary>
        /// Creates a new cropped WriteableBitmap.
        /// </summary>
        /// <param name="bmp">The WriteableBitmap.</param>
        /// <param name="x">The x coordinate of the rectangle that defines the crop region.</param>
        /// <param name="y">The y coordinate of the rectangle that defines the crop region.</param>
        /// <param name="width">The width of the rectangle that defines the crop region.</param>
        /// <param name="height">The height of the rectangle that defines the crop region.</param>
        /// <returns>A new WriteableBitmap that is a cropped version of the input.</returns>
        public static BitmapBuffer Crop(this BitmapBuffer bmp, int x, int y, int width, int height)
        {
            using (BitmapContext srcContext = bmp.GetBitmapContext(ReadWriteMode.ReadOnly))
            {
                int srcWidth  = srcContext.Width;
                int srcHeight = srcContext.Height;

                // If the rectangle is completely out of the bitmap
                if (x > srcWidth || y > srcHeight)
                {
                    return(BitmapBufferFactory.New(0, 0));
                }

                // Clamp to boundaries
                if (x < 0)
                {
                    x = 0;
                }
                if (x + width > srcWidth)
                {
                    width = srcWidth - x;
                }
                if (y < 0)
                {
                    y = 0;
                }
                if (y + height > srcHeight)
                {
                    height = srcHeight - y;
                }

                // Copy the pixels line by line using fast BlockCopy
                BitmapBuffer result = BitmapBufferFactory.New(width, height);
                using (BitmapContext destContext = result.GetBitmapContext())
                {
                    for (int line = 0; line < height; line++)
                    {
                        int srcOff = ((y + line) * srcWidth + x) * ARGB_SIZE;
                        int dstOff = line * width * ARGB_SIZE;
                        BitmapContext.BlockCopy(srcContext, srcOff, destContext, dstOff, width * ARGB_SIZE);
                    }
                    return(result);
                }
            }
        }