title: DrawImage fails to position sliced images correctly
body: <p>For a couple of days now I've tried to figure out why my nine-slice code does not work as expected. As far as I can see, there seems to be an issue with the Graphics.DrawImage method which handles my nine slice images incorrectly. So my problem is how to compensate for the incorrect scaling that is performed when running my code on the compact framework. I might add that this code of course works perfectly when running in the full framework environment. The problem only occurs when scaling the image to a larger image not the other way around. Here is the snippet:</p>
  
  <pre><code>public class NineSliceBitmapSnippet
  {
      private Bitmap m_OriginalBitmap;
  
      public int CornerLength { get; set; }
  
      /// &lt;summary&gt;
      /// Initializes a new instance of the NineSliceBitmapSnippet class.
      /// &lt;/summary&gt;
      public NineSliceBitmapSnippet(Bitmap bitmap)
      {
          CornerLength = 5;
          m_OriginalBitmap = bitmap;
      }
  
      public Bitmap ScaleSingleBitmap(Size size)
      {
          Bitmap scaledBitmap = new Bitmap(size.Width, size.Height);
          int[] horizontalTargetSlices = Slice(size.Width);
          int[] verticalTargetSlices = Slice(size.Height);
  
          int[] horizontalSourceSlices = Slice(m_OriginalBitmap.Width);
          int[] verticalSourceSlices = Slice(m_OriginalBitmap.Height);
  
          using (Graphics graphics = Graphics.FromImage(scaledBitmap))
          {
              using (Brush brush = new SolidBrush(Color.Fuchsia))
              {
                  graphics.FillRectangle(brush, new Rectangle(0, 0, size.Width, size.Height));
              }
  
              int horizontalTargetOffset = 0;
              int verticalTargetOffset = 0;
  
              int horizontalSourceOffset = 0;
              int verticalSourceOffset = 0;
  
              for (int x = 0; x &lt; horizontalTargetSlices.Length; x++)
              {
                  verticalTargetOffset = 0;
                  verticalSourceOffset = 0;
                  for (int y = 0; y &lt; verticalTargetSlices.Length; y++)
                  {
                      Rectangle destination = new Rectangle(horizontalTargetOffset, verticalTargetOffset, horizontalTargetSlices[x], verticalTargetSlices[y]);
                      Rectangle source = new Rectangle(horizontalSourceOffset, verticalSourceOffset, horizontalSourceSlices[x], verticalSourceSlices[y]);
  
                      graphics.DrawImage(m_OriginalBitmap, destination, source, GraphicsUnit.Pixel);
  
                      verticalTargetOffset += verticalTargetSlices[y];
                      verticalSourceOffset += verticalSourceSlices[y];
                  }
  
                  horizontalTargetOffset += horizontalTargetSlices[x];
                  horizontalSourceOffset += horizontalSourceSlices[x];
              }
          }
          return scaledBitmap;
      }
  
      public int[] Slice(int length)
      {
          int cornerLength = CornerLength;
  
          if (length &lt;= (cornerLength * 2))
              throw new Exception("Image to small for sliceing up");
  
          int[] slices = new int[3];
          slices[0] = cornerLength;
          slices[1] = length - (2 * cornerLength);
          slices[2] = cornerLength;
  
          return slices;
      }
  }
  </code></pre>
  
  <p>So, my question is, does anybody now how I could compensate the incorrect scaling? </p>
  
  <p>/Dan</p>