Image conversion (rotation) using matrix results - development time and runtime

I have a 48x48 image that rotates using a transform matrix.

For some reason, the rotated image during development differs from the rotated image at runtime, as you can see from this screenshot (link dead) (development time on the left, runtime on the right):

It may be a little difficult to detect, but if you look closely at the right edge of the blue circle, it's about a pixel, which will be larger in the image on the right. Note that the image is superimposed on the layers - the white glow in the foreground is the part that rotates, and the blue ball in the background is static.

It seems that the image is shifted by 1 pixel at runtime, rotating exactly 90 degrees (as in the screenshot), 180 degrees, and probably also 270 degrees. As far as I can see, the image looks the same with any other rotation angle.

Here is a snippet:

protected static Image RotateImage(Image pImage, Single pAngle)
    {
      Matrix lMatrix = new Matrix();
      lMatrix.RotateAt(pAngle, new PointF(pImage.Width / 2, pImage.Height / 2));
      Bitmap lNewBitmap = new Bitmap(pImage.Width, pImage.Height);
      lNewBitmap.SetResolution(pImage.HorizontalResolution, pImage.VerticalResolution);
      Graphics lGraphics = Graphics.FromImage(lNewBitmap);
      lGraphics.Transform = lMatrix;
      lGraphics.DrawImage(pImage, 0, 0);
      lGraphics.Dispose();
      lMatrix.Dispose();
      return lNewBitmap;
    }

    void SomeMethod()
    {
      // Same results in design-time and run-time:
      PictureBox1.Image = RotateImage(PictureBox2.Image, 18)
      // Different results in design-time and run-time.
      PictureBox1.Image = RotateImage(PictureBox2.Image, 90)
    }

Can anyone explain the reason for this behavior? Or even better, does the solution for getting results at runtime look like the result of development time?

This is important to me because this image is part of the animation that is generated from the code based on a single image, which is then rotated in small steps. During development, the animation looks smooth and enjoyable. At run time, it looks like it is jumping: /

I am using Visual Studio 2005 for Windows Vista Business SP2.

+3
4

- Graphics, , PixelOffsetMode.

+3

? ?

, , , , ( ).

, , .

0

Hm, , :

lMatrix.RotateAt(pAngle, new PointF((pImage.Width + 1)/2.0f, (pImage.Height + 1)/ 2.0f));
0

, , ; .

, ... - , - ? , .

. ... , , :

angle = 90 : img[i][j] = img[j][w-i]
angle = 180: img[i][j] = img[w-i][w-j]
angle = 270: img[i][j] = img[w-j][i]

( , , )

0

Source: https://habr.com/ru/post/1712538/


All Articles