如何旋转位图,保持其比例尺

问题描述 投票:0回答:1

我正在尝试在保留所有像素的位置旋转位图,并设置了目标位图大小,以便以相同的比例获得它。换句话说,源中的每个像素在输出中都是一个不同的像素,并且输出位图的大小会进行调整以匹配。

我尝试了很多东西(below code here in a zip with bitmap files)。但是我得到的是输出中的部分图像,并且在某些情况下会按比例缩小。

注意:这里列出了很多解决方案,但是它们都具有本代码演示的相同问题。

using System;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace RotateBitmap
{
    class Program
    {
        static void Main(string[] args)
        {
            RotateBitmap("cows.png", "cows_rotated.png", 90);
            RotateBitmap("zoey.jpeg", "zoey_rotated.png", 45);
        }

        static void RotateBitmap(string srcFilename, string destFilename, double degrees)
        {

            srcFilename = Path.GetFullPath(Path.Combine("../..", srcFilename));
            destFilename = Path.GetFullPath(Path.Combine("../..", destFilename));

            byte[] data = File.ReadAllBytes(srcFilename);
            MemoryStream stream = new MemoryStream(data);
            Bitmap srcBitmap = new Bitmap(stream);

            double radians = Math.PI * degrees / 180;

            double sin = Math.Abs(Math.Sin(radians)), cos = Math.Abs(Math.Cos(radians));
            int srcWidth = srcBitmap.Width;
            int srcHeight = srcBitmap.Height;
            int rotatedWidth = (int)Math.Floor(srcWidth * cos + srcHeight * sin);
            int rotatedHeight = (int)Math.Floor(srcHeight * cos + srcWidth * sin);

            Bitmap rotatedImage = new Bitmap(rotatedWidth, rotatedHeight);
            Graphics g = Graphics.FromImage(rotatedImage);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            g.SmoothingMode = SmoothingMode.HighQuality;

            // Set the rotation point to the center in the matrix
            g.TranslateTransform(rotatedWidth / 2, rotatedHeight / 2);
            // Rotate
            g.RotateTransform((float)degrees);
            // Restore rotation point in the matrix
            g.TranslateTransform(-srcWidth / 2, -rotatedHeight / 2);
            // Draw the image on the bitmap
            g.DrawImage(srcBitmap, 0, 0);

            srcBitmap.Dispose();
            g.Dispose();

            stream.Close();
            stream = new MemoryStream();
            rotatedImage.Save(stream, ImageFormat.Png);
            data = stream.ToArray();
            File.WriteAllBytes(destFilename, data);
        }
    }
}

Also posted on MSDN.

bitmap system.drawing image-rotation system.drawing.graphics
1个回答
0
投票
static void RotateBitmap2( string srcFilename, string destFilename, double degrees ) { using( var srcBitmap = Bitmap.FromFile( srcFilename ) ) { using( var g1 = Graphics.FromImage( srcBitmap ) ) { var w = srcBitmap.Width; var h = srcBitmap.Height; g1.TranslateTransform( w / 2, h / 2 ); g1.RotateTransform( checked((float)degrees) ); g1.TranslateTransform( -w / 2, -h / 2 ); var a = new[] { new Point( 0, 0 ), new Point( w, 0 ), new Point( w, h ), new Point( 0, h ) }; g1.TransformPoints( CoordinateSpace.Device, CoordinateSpace.World, a ); int new_w = a.Max( p => p.X ) - a.Min( p => p.X ); int new_h = a.Max( p => p.Y ) - a.Min( p => p.Y ); using( var rotatedImage = new Bitmap( new_w, new_h, g1 ) ) { using(var g2 = Graphics.FromImage(rotatedImage)) { g2.TranslateTransform( new_w / 2, new_h / 2 ); g2.RotateTransform( checked((float)degrees) ); g2.DrawImageUnscaled( srcBitmap, -w / 2, -h / 2 ); } rotatedImage.Save( destFilename, ImageFormat.Png ); } } } }
© www.soinside.com 2019 - 2024. All rights reserved.