public void TestValidCopyPixels() { Bitmap bitmap1 = GenerateRandomBitmap(64, 64); Bitmap bitmap2 = new Bitmap(64, 64); FastBitmap.CopyPixels(bitmap1, bitmap2); AssertBitmapEquals(bitmap1, bitmap2, "After a successful call to CopyPixels(), both bitmaps must be equal down to the pixel level"); }
/// <summary> /// Copies this layer's contents from the given bitmap. /// If the layer's dimensions don't match the passed bitmap's dimensions, an ArgumentException is raised /// </summary> /// <param name="bitmap">The bitmap to copy to this layer</param> /// <exception cref="ArgumentException">The bitmap's dimensions don't match this layer's dimensions</exception> public void CopyFromBitmap(Bitmap bitmap) { if (bitmap.Width != LayerBitmap.Width || bitmap.Height != LayerBitmap.Height) { throw new ArgumentException(@"The provided bitmap's dimensions don't match this bitmap's dimensions", nameof(bitmap)); } // Copy the pixels FastBitmap.CopyPixels(bitmap, LayerBitmap); }
/// <summary> /// Updates the preview visualization for this BaseFilterView /// </summary> private void UpdateVisualization() { if (_bitmapOriginal == null) { return; } FastBitmap.CopyPixels(_bitmapOriginal, _bitmapPreview); foreach (FilterContainer container in _filterContainers) { container.ApplyFilter(_bitmapPreview); } zpb_preview.Invalidate(); }
/// <summary> /// Registers the pixels of the given bitmap as the redo bitmap /// </summary> /// <param name="newBitmap">The bitmap whose pixels will be used as the redo bitmap</param> /// <param name="cloneBitmap">Whether to clone the bitmap instead of only assigning it</param> public void SetNewBitmap(Bitmap newBitmap, bool cloneBitmap = true) { if (_newBitmap != null && cloneBitmap) { _newBitmap.Dispose(); } if (cloneBitmap) { _newBitmap = new Bitmap(newBitmap.Width, newBitmap.Height, PixelFormat.Format32bppArgb); FastBitmap.CopyPixels(newBitmap, _newBitmap); } else { _newBitmap = newBitmap; } }
public void TestInvalidCopyPixels() { Bitmap bitmap1 = new Bitmap(64, 64, PixelFormat.Format24bppRgb); Bitmap bitmap2 = new Bitmap(64, 64, PixelFormat.Format1bppIndexed); if (FastBitmap.CopyPixels(bitmap1, bitmap2)) { Assert.Fail("Trying to copy two bitmaps of different bitdepths should not be allowed"); } bitmap1 = new Bitmap(64, 64, PixelFormat.Format32bppArgb); bitmap2 = new Bitmap(66, 64, PixelFormat.Format32bppArgb); if (FastBitmap.CopyPixels(bitmap1, bitmap2)) { Assert.Fail("Trying to copy two bitmaps of different sizes should not be allowed"); } }
/// <summary> /// Performs a Paste operation /// </summary> public void Paste() { Stream str = Clipboard.GetData("PNG") as Stream; Bitmap bit; if (str != null) { bit = Image.FromStream(str) as Bitmap; if (bit == null) { str.Dispose(); return; } Bitmap temp = new Bitmap(bit.Width, bit.Height, PixelFormat.Format32bppArgb); FastBitmap.CopyPixels(bit, temp); bit.Dispose(); bit = temp; str.Dispose(); } else { bit = Clipboard.GetImage() as Bitmap; } if (bit != null) { FinishOperation(true); OperationType = SelectionOperationType.Paste; // Get the top-left pixel to place the selection at Point loc = GetAbsolutePoint(new PointF(0, 0)); StartOperation(new Rectangle(loc.X, loc.Y, bit.Width, bit.Height), bit); } pictureBox.MarkModified(); }
/// <summary> /// Returns the composed Bitmap for this frame /// </summary> /// <returns>The composed bitmap for this frame</returns> public Bitmap GetComposedBitmap() { if (!_initialized) { throw new InvalidOperationException(@"The frame was not initialized prior to this action"); } Bitmap composedBitmap = new Bitmap(_width, _height, PixelFormat.Format32bppArgb); FastBitmap.CopyPixels(_layers[0].LayerBitmap, composedBitmap); // Compose the layers by blending all the pixels from each layer into the final image for (int i = 1; i < _layers.Count; i++) { ImageUtilities.FlattenBitmaps(composedBitmap, _layers[i].LayerBitmap, true); } return(composedBitmap); }
public void TestSameBitmapCopyPixelsException() { Bitmap bitmap = new Bitmap(64, 64); FastBitmap.CopyPixels(bitmap, bitmap); }
// // OnPaint event handler // protected override void OnPaint(PaintEventArgs pe) { // Draw the under image if (_underImage != null) { pe.Graphics.TranslateTransform(-offsetPoint.X, -offsetPoint.Y); pe.Graphics.ScaleTransform(scale.X, scale.Y); pe.Graphics.PixelOffsetMode = PixelOffsetMode.Half; pe.Graphics.InterpolationMode = ImageInterpolationMode; // Apply the decorators Bitmap copy = _underImage; if (_pictureBoxDecorators.Count > 0) { copy = _underImage.Clone(new Rectangle(0, 0, _underImage.Width, _underImage.Height), _underImage.PixelFormat); foreach (PictureBoxDecorator decorator in _pictureBoxDecorators) { decorator.DecorateUnderBitmap(copy); } } pe.Graphics.DrawImage(copy, 0, 0); pe.Graphics.ResetTransform(); } if (Image != null) { UpdateGraphicsTransform(pe.Graphics); // Reset the buffer back to the original input bitmap state FastBitmap.CopyPixels(Bitmap, _buffer); // Clip to the image's boundaries pe.Graphics.IntersectClip(new RectangleF(0, 0, Image.Width, Image.Height)); Region clip = pe.Graphics.Clip; // Render the current paint tool if (EditingEnabled) { _currentPaintTool.Paint(pe); } // Draw the actual image if (_displayImage) { foreach (PictureBoxDecorator decorator in _pictureBoxDecorators) { decorator.DecorateMainBitmap(_buffer); } // Draw the buffer now pe.Graphics.DrawImage(_buffer, 0, 0); } // Draw the over image if (_overImage != null) { // Apply the decorators Bitmap copy = _overImage; if (_pictureBoxDecorators.Count > 0) { copy = (_overImage.Clone(new Rectangle(0, 0, _overImage.Width, _overImage.Height), _overImage.PixelFormat)); foreach (PictureBoxDecorator decorator in _pictureBoxDecorators) { decorator.DecorateOverBitmap(copy); } } pe.Graphics.DrawImage(copy, Point.Empty); } // Reset the clipping and draw the grid if (_displayGrid && scale.X > 4 && scale.Y > 4) { pe.Graphics.Clip = clip; pe.Graphics.ResetTransform(); Pen pen = Pens.Gray; float xOff = (-offsetPoint.X) % scale.X; float yOff = (-offsetPoint.Y) % scale.Y; // Draw the horizontal lines for (float y = yOff; y < Math.Min(Height, (Image.Height * scale.Y)); y += scale.Y) { pe.Graphics.DrawLine(pen, 0, y, (int)(Image.Width * scale.X), y); } // Draw the vertical lines for (float x = xOff; x < Math.Min(Width, (Image.Width * scale.X)); x += scale.X) { pe.Graphics.DrawLine(pen, x, 0, x, (int)(Image.Height * scale.Y)); } } } else { // Draw the over image if (_overImage == null) { return; } Bitmap copy = _overImage; if (_pictureBoxDecorators.Count > 0) { copy = _overImage.Clone(new Rectangle(0, 0, _overImage.Width, _overImage.Height), _overImage.PixelFormat); foreach (PictureBoxDecorator decorator in _pictureBoxDecorators) { decorator.DecorateUnderBitmap(copy); } } pe.Graphics.DrawImage(copy, new Point()); } // Paint the current paint tool's foreground if (EditingEnabled) { pe.Graphics.ResetTransform(); UpdateGraphicsTransform(pe.Graphics); _currentPaintTool.PaintForeground(pe); } }