Advertisement
Guest User

Untitled

a guest
May 20th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.96 KB | None | 0 0
  1.  public static Bitmap Rotate(this Bitmap sourceBitmap, double degrees)
  2.         {
  3.             //sourceBitmap = NormalizationTools.CreateNonIndexedImage(sourceBitmap);
  4.             var angle = degrees * Math.PI / 180.0;
  5.             var cos = Math.Cos(angle);
  6.             var sin = Math.Sin(angle);
  7.             var width = sourceBitmap.Width;
  8.             var height = sourceBitmap.Height;
  9.             var xOffset = width / 2;
  10.             var yOffset = height / 2;
  11.  
  12.             // copy image to byte array
  13.             var sourceData = sourceBitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly,
  14.                 PixelFormat.Format24bppRgb);
  15.  
  16.             var stride = sourceData.Stride;
  17.             var pixelBuffer = new byte[sourceData.Stride * sourceData.Height];
  18.             Marshal.Copy(sourceData.Scan0, pixelBuffer, 0, pixelBuffer.Length);
  19.             sourceBitmap.UnlockBits(sourceData);
  20.  
  21.             // create buffer for image with back background
  22.             var resultBuffer = new byte[pixelBuffer.Length];
  23.             for (int i = 0; i < resultBuffer.Length; ++i)
  24.             {
  25.                 resultBuffer[i] = 255;
  26.             }
  27.  
  28.  
  29.             // process rotation
  30.             for (int row = 0; row < height; ++row)
  31.             {
  32.                 for (int col = 0; col < width; ++col)
  33.                 {
  34.                     var index = row * stride + col * 3;
  35.                     var x = (int)Math.Round((col - xOffset) * cos - (row - yOffset) * sin) + xOffset;
  36.                     var y = (int)Math.Round((col - xOffset) * sin + (row - yOffset) * cos) + yOffset;
  37.                     var newIndex = y * stride + x * 3;
  38.  
  39.                     if (y >= 0 && y < height && x >= 0 && x < width)
  40.                     {
  41.                         if (newIndex + 5 < resultBuffer.Length)
  42.                         {
  43.                             resultBuffer[newIndex + 3] = resultBuffer[newIndex] = pixelBuffer[index];
  44.                             resultBuffer[newIndex + 4] = resultBuffer[newIndex + 1] = pixelBuffer[index + 1];
  45.                             resultBuffer[newIndex + 5] = resultBuffer[newIndex + 2] = pixelBuffer[index + 2];
  46.                         }
  47.                         else
  48.                         {
  49.                             resultBuffer[newIndex] = pixelBuffer[index];
  50.                             resultBuffer[newIndex + 1] = pixelBuffer[index + 1];
  51.                             resultBuffer[newIndex + 2] = pixelBuffer[index + 2];
  52.                         }
  53.                     }
  54.                 }
  55.             }
  56.  
  57.             // create new bitmap
  58.             var resultBitmap = new Bitmap(width, height);
  59.             var resultData = resultBitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly,
  60.                 PixelFormat.Format24bppRgb);
  61.  
  62.             Marshal.Copy(resultBuffer, 0, resultData.Scan0, resultBuffer.Length);
  63.             resultBitmap.UnlockBits(resultData);
  64.             return resultBitmap;
  65.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement