private void rotation_Click(object sender, EventArgs e) { if (curBitmap != null) { rotation rotForm = new rotation(); if (rotForm.ShowDialog() == DialogResult.OK) { int degree = Convert.ToInt32(rotForm.GetDegree); rotateImage(degree); } Invalidate(); } }
private void rotation_Click(object sender, EventArgs e) { if (curBitmap != null) { rotation rotForm = new rotation(); if (rotForm.ShowDialog() == DialogResult.OK) { Rectangle rect = new Rectangle(0, 0, curBitmap.Width, curBitmap.Height); System.Drawing.Imaging.BitmapData bmpData = curBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, curBitmap.PixelFormat); IntPtr ptr = bmpData.Scan0; int bytes = curBitmap.Width * curBitmap.Height; byte[] grayValues = new byte[bytes]; System.Runtime.InteropServices.Marshal.Copy(ptr, grayValues, 0, bytes); int degree = Convert.ToInt32(rotForm.GetDegree); double radian = degree * Math.PI / 180.0; double mySin = Math.Sin(radian); double myCos = Math.Cos(radian); int halfWidth = (int)(curBitmap.Width / 2); int halfHeight = (int)(curBitmap.Height / 2); int xr = 0; int yr = 0; int tempWidth = 0; int tempHeight = 0; byte[] tempArray = new byte[bytes]; double tempX, tempY, p, q; for (int i = 0; i < curBitmap.Height; i++) { for (int j = 0; j < curBitmap.Width; j++) { tempHeight = i - halfHeight; tempWidth = j - halfWidth; tempX = tempWidth * myCos - tempHeight * mySin; tempY = tempHeight * myCos + tempWidth * mySin; if (tempWidth > 0) { xr = (int)tempX; } else { xr = (int)(tempX - 1); } if (tempHeight > 0) { yr = (int)tempY; } else { yr = (int)(tempY - 1); } p = tempX - xr; q = tempY - yr; tempWidth = xr + halfWidth; tempHeight = yr + halfHeight; if (tempWidth < 0 || (tempWidth + 1) >= curBitmap.Width || tempHeight < 0 || (tempHeight + 1) >= curBitmap.Height) { tempArray[i * curBitmap.Width + j] = 255; } else { tempArray[i * curBitmap.Width + j] = (byte)((1.0 - q) * ((1.0 - p) * grayValues[tempHeight * curBitmap.Width + tempWidth] + p * grayValues[tempHeight * curBitmap.Width + tempWidth + 1]) + q * ((1.0 - p) * grayValues[(tempHeight + 1) * curBitmap.Width + tempWidth] + p * grayValues[(tempHeight + 1) * curBitmap.Width + 1 + tempWidth])); } } } grayValues = (byte[])tempArray.Clone(); System.Runtime.InteropServices.Marshal.Copy(grayValues, 0, ptr, bytes); curBitmap.UnlockBits(bmpData); } Invalidate(); } }