2010-02-08 62 views
40

Tôi đang cố gắng xoay một bitmap 90 độ bằng cách sử dụng chức năng sau. Vấn đề với nó là nó cắt bỏ một phần của hình ảnh khi chiều cao và chiều rộng không bằng nhau.C# xoay bitmap 90 độ

Thông báo các returnBitmap width = original.height và nó height = original.width

bất cứ ai có thể giúp tôi giải quyết vấn đề này hoặc chỉ ra những gì tôi đang làm sai?

private Bitmap rotateImage90(Bitmap b) 
    { 
     Bitmap returnBitmap = new Bitmap(b.Height, b.Width); 
     Graphics g = Graphics.FromImage(returnBitmap); 
     g.TranslateTransform((float)b.Width/2, (float)b.Height/2); 
     g.RotateTransform(90); 
     g.TranslateTransform(-(float)b.Width/2, -(float)b.Height/2); 
     g.DrawImage(b, new Point(0, 0)); 
     return returnBitmap; 
    } 

Trả lời

81

gì về this:

private void RotateAndSaveImage(String input, String output) 
{ 
    //create an object that we can use to examine an image file 
    using (Image img = Image.FromFile(input)) 
    { 
     //rotate the picture by 90 degrees and re-save the picture as a Jpeg 
     img.RotateFlip(RotateFlipType.Rotate90FlipNone); 
     img.Save(output, System.Drawing.Imaging.ImageFormat.Jpeg); 
    } 
} 
+0

bitmap Tôi quay chỉ dành cho mục đích hiển thị. Tôi không bao giờ lưu nó vào một tập tin – Kevin

+3

bạn không cần phải lưu nó; rằng 'RotateFlip' sẽ thực hiện thủ thuật. Bạn có thể xóa 'using' và thêm' bitmap mới (img); ' –

+0

Bạn có thể muốn lấy một số mã từ đây để đảm bảo rằng jpeg được lưu với mức chất lượng cao hơn mặc định 50 http://stackoverflow.com/questions/1484759/quality-of-a-saved-jpg-in-c-sharp –

8

Lỗi tham gia cuộc gọi đầu tiên của bạn để TranslateTransform:

g.TranslateTransform((float)b.Width/2, (float)b.Height/2); 

này chuyển đổi cần phải được trong không gian toạ độ returnBitmap hơn b, vì vậy, điều này phải là:

g.TranslateTransform((float)b.Height/2, (float)b.Width/2); 

hoặc tương đương

g.TranslateTransform((float)returnBitmap.Width/2, (float)returnBitmap.Height/2); 

thứ hai TranslateTransform là đúng, bởi vì nó sẽ được áp dụng trước khi luân chuyển của bạn.

Tuy nhiên, bạn có thể nên sử dụng phương pháp RotateFlip đơn giản hơn, như Rubens Farias đề xuất.

1

Tôi bắt gặp và với một chút sửa đổi, tôi đã làm cho nó hoạt động. Tôi tìm thấy một số ví dụ khác và nhận thấy một cái gì đó mất tích mà tạo ra sự khác biệt cho tôi. Tôi đã phải gọi SetResolution, nếu tôi không phải hình ảnh đã kết thúc kích thước sai. Tôi cũng nhận thấy chiều cao và chiều rộng là ngược, mặc dù tôi nghĩ rằng sẽ có một số sửa đổi cho một hình ảnh không vuông anyway. Tôi nghĩ rằng tôi sẽ đăng bài này cho bất cứ ai đi qua này như tôi đã làm với cùng một vấn đề.

Đây là mã của tôi

private static void RotateAndSaveImage(string input, string output, int angle) 
{ 
    //Open the source image and create the bitmap for the rotatated image 
    using (Bitmap sourceImage = new Bitmap(input)) 
    using (Bitmap rotateImage = new Bitmap(sourceImage.Width, sourceImage.Height)) 
    { 
     //Set the resolution for the rotation image 
     rotateImage.SetResolution(sourceImage.HorizontalResolution, sourceImage.VerticalResolution); 
     //Create a graphics object 
     using (Graphics gdi = Graphics.FromImage(rotateImage)) 
     { 
      //Rotate the image 
      gdi.TranslateTransform((float)sourceImage.Width/2, (float)sourceImage.Height/2); 
      gdi.RotateTransform(angle); 
      gdi.TranslateTransform(-(float)sourceImage.Width/2, -(float)sourceImage.Height/2); 
      gdi.DrawImage(sourceImage, new System.Drawing.Point(0, 0)); 
     } 

     //Save to a file 
     rotateImage.Save(output); 
    } 
} 
Các vấn đề liên quan