/// <summary> /// Reprojects a <see cref="Bitmap"/> and corresponding georeferenced metadata to /// a new coordinate system. /// </summary> /// <param name="sourceReference">The georeferenced metadata of <paramref name="sourceTile"/>.</param> /// <param name="sourceTile">The tile image.</param> /// <param name="targetReference">Output: The reprojected georeferenced metadata.</param> /// <param name="targetTile">Output: The reprojected tile image.</param> public void Reproject(WorldFile sourceReference, Bitmap sourceTile, out WorldFile targetReference, out Bitmap targetTile) { if (source == null || target == null || source.Equals(target)) { targetReference = sourceReference; targetTile = sourceTile; return; } Rectangle targetTileExtentInPixelCoordinates = GetTargetExtentInPixelCoordinates(sourceReference, sourceTile); // Get the intersection with the current viewport targetTileExtentInPixelCoordinates.Intersect(mapArgs.ImageRectangle); // Is it empty, don't return anything if (targetTileExtentInPixelCoordinates.Width == 0 || targetTileExtentInPixelCoordinates.Height == 0) { targetTile = null; targetReference = null; return; } int offsetX = targetTileExtentInPixelCoordinates.X; int offsetY = targetTileExtentInPixelCoordinates.Y; // Prepare the result tile targetTile = new Bitmap(targetTileExtentInPixelCoordinates.Size.Width, targetTileExtentInPixelCoordinates.Size.Height, PixelFormat.Format32bppArgb); using (Graphics g = Graphics.FromImage(targetTile)) { g.Clear(Color.Transparent); } ColorAccess sourceTileColorAccess = ColorAccess.Create(sourceTile); ColorAccess targetTileColorAccess = ColorAccess.Create(targetTile); // Copy values to output buffer Size sourceTileSize = sourceTile.Size; for (var i = 0; i < targetTile.Height; i++) { foreach (Tuple <Point, Point> ppair in GetValidPoints(offsetY + i, offsetY, offsetX, offsetX + targetTile.Width, sourceReference, sourceTileSize)) { Color c = sourceTileColorAccess[ppair.Item1.X, ppair.Item1.Y]; targetTileColorAccess[ppair.Item2.X, ppair.Item2.Y] = c; } } // Copy to output tile targetTileColorAccess.SetBufferToImageAtOriginalLocation(targetTile); // Compute the reference Extent outExtent = mapArgs.PixelToProj(targetTileExtentInPixelCoordinates); targetReference = new WorldFile( outExtent.Width / targetTileExtentInPixelCoordinates.Width, 0, 0, -outExtent.Height / targetTileExtentInPixelCoordinates.Height, outExtent.X, outExtent.Y); }
public void SetBufferToImageAtOriginalLocation_BitmapNull_ThrowArgumentNullException() { // Setup ColorAccess colorAccess = ColorAccess.Create(Resources.acorn); // Call TestDelegate call = () => colorAccess.SetBufferToImageAtOriginalLocation(null); // Assert string paramName = Assert.Throws <ArgumentNullException>(call).ParamName; Assert.AreEqual("bitmap", paramName); }
public void SetBufferToImageAtOriginalLocation_AfterModificationsToBuffer_TargetImageUpdated() { // Setup ColorAccess colorAccess = ColorAccess.Create(Resources.acorn); for (var i = 0; i < 16; i++) { colorAccess[i, i] = Color.Red; colorAccess[i, 15 - i] = Color.Red; } using (var targetImage = (Bitmap)Resources.acorn.Clone()) { // Call colorAccess.SetBufferToImageAtOriginalLocation(targetImage); // Assert TestHelper.AssertImagesAreEqual(Resources.acornWithCross, targetImage); } }