tags:

views:

314

answers:

2
+1  Q: 

Matrix rotateAt c#

Hello,

Im trying to rotate a image with matrix object and can't get it right

When i rotate the image i got a black spot, it's one pixel wrong and it's the same with 180 angle and 270 angle.

90 angle ex. A picture of this problem: http://www.spasm-design.com/rotate/onePixelWrong.jpg


And here is the code:

public System.Drawing.Image Rotate(System.Drawing.Image image, String angle, String direction)
{
  Int32 destW, destH;
  float destX, destY, rotate;

  destW = image.Width;
  destH = image.Height;
  destX = destY = 0;

  if (r == "90" || r == "270")
  {
    destW = image.Height;
    destH = image.Width;

    destY = (image.Width - destW) / 2;
    destX = (image.Height - destH) / 2;
  }

  rotate = (direction == "y") ? float.Parse(angle) : float.Parse("-" + angle);

  Bitmap b = new Bitmap(destW, destH, PixelFormat.Format24bppRgb);
  b.SetResolution(image.HorizontalResolution, image.VerticalResolution);

  Matrix x = new Matrix();
  x.Translate(destX, destY);
  x.RotateAt(rotate, new PointF(image.Width / 2, image.Height / 2));

  Graphics g = Graphics.FromImage(b);
  g.PageUnit = GraphicsUnit.Pixel;
  g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  g.Transform = x;
  g.DrawImage(image, 0, 0);

  g.Dispose();
  x.Dispose();

  return b;
}

if someone have a good ide why this is happening please tell me.

Have a good day!

A: 

I think you're just getting a rounding error on this line:

x.RotateAt(rotate, new PointF(image.Width / 2, image.Height / 2));

Width and Height are both int properties. Try this instead:

x.RotateAt(rotate, new PointF((float)Math.Floor(image.Width / 2),
    (float)Math.Floor(image.Height / 2)));

(Not tested, so not sure if this will work.)

Update: I don't think my above fix will work, but it may point you in the direction of the problem. If you can't fix it by adjusting the rounding, you may just need to change destX to -1 to get rid of the black line.

MusiGenesis
A: 

This works: x.RotateAt(rotate, new PointF(image.Width / 2, image.Height / 2)); this "image.Width / 2" returns float

First i find out what angle is, if it is 90 or 270 flip the image so image.width = image.height and image.height = width

If a do that i get a problem when i rotate the image for the image width can be bigger then height of the image so then i need to reset the image x,y coordinates to 0,0

So this "destY = (image.Width - destW) / 2;" calculate offset of the image to the bitmap and this "x.Translate(destX, destY);" set the image x equivalent to bitmap x

but something is going wrong for the rotation makes picture 1px to small.

so for my english but im not the best of it, i hope you can read it any why :)

for more questions please send me those and i'm going to try explain what i mean.

Broadminded