/ src / modules / MouseUtils / MouseJump.Common / Imaging / StaticImageRegionCopyService.cs
StaticImageRegionCopyService.cs
 1  // Copyright (c) Microsoft Corporation
 2  // The Microsoft Corporation licenses this file to you under the MIT license.
 3  // See the LICENSE file in the project root for more information.
 4  
 5  using System.Drawing.Drawing2D;
 6  
 7  using MouseJump.Common.Models.Drawing;
 8  
 9  namespace MouseJump.Common.Imaging;
10  
11  /// <summary>
12  /// Implements an IImageRegionCopyService that uses the specified image as the copy source.
13  /// This is used for testing the DrawingHelper rather than as part of the main application.
14  /// </summary>
15  public sealed class StaticImageRegionCopyService : IImageRegionCopyService
16  {
17      public StaticImageRegionCopyService(Image sourceImage)
18      {
19          this.SourceImage = sourceImage ?? throw new ArgumentNullException(nameof(sourceImage));
20      }
21  
22      private Image SourceImage
23      {
24          get;
25      }
26  
27      /// <summary>
28      /// Copies the source region from the static source image
29      /// to the target region on the specified Graphics object.
30      /// </summary>
31      public void CopyImageRegion(
32          Graphics targetGraphics,
33          RectangleInfo sourceBounds,
34          RectangleInfo targetBounds)
35      {
36          // prevent the background bleeding through into screen images
37          // (see https://github.com/mikeclayton/FancyMouse/issues/44)
38          targetGraphics.PixelOffsetMode = PixelOffsetMode.Half;
39          targetGraphics.InterpolationMode = InterpolationMode.NearestNeighbor;
40  
41          targetGraphics.DrawImage(
42              image: this.SourceImage,
43              destRect: targetBounds.ToRectangle(),
44              srcRect: sourceBounds.ToRectangle(),
45              srcUnit: GraphicsUnit.Pixel);
46      }
47  }