在C#中缩放和旋转图像会产生幻影边

时间:2018-02-12 01:59:12

标签: c# .net image system.drawing

我使用C#缩放和旋转具有透明背景的图像,然后使用JavaScript将其添加到Google Maps API地图,但是一旦缩放并旋转,图像的边缘变得模糊,变得很难看,特别是在水面上。

这是我原来的300x300图片:

Here is my original 300x300 image.

以下是100x100(SizeInt = 100)输出图像之一的示例:

在您对背景颜色感到困惑之前,图像会被投影到地图上,其中水的颜色与图像相似。

And here is an example of one of the output images at 100x100 (<code>SizeInt = 100</code>): Before you get confused as to the background colour, the image is projected onto a map where the water is a similar colour to the image.

您可以在图像边缘看到一些轻微的重影。

一旦它进一步缩小(按照规定的要求):

Once it's scaled down further (to it's required scale)

它几乎无法察觉......

我仍然相对新的C#,但我尝试使用JavaScript(icon: "some/SVG/file.svg")将SVG直接加载到地图上,但这会导致严重的性能问题,因为在任何给定时间地图上最多有1,000个图标,使得地图在使用SVG文件时无法使用,所以我必须使用图像文件。

图像需要清晰,边缘不要模糊。以下是用于生成图像的代码,原谅任何草率的代码,我对C#还有点新鲜:

以下是用于缩放图像的代码:

图像需要以整圆旋转(四舍五入到5度),因此循环从0到361.

private void BuildIcons(string dir)
    {

        int SizeInt = 30;

        // Get the content path
        var ContentPath = dir;

        // Get the original image
        Bitmap OriginalImage = new Bitmap(ContentPath + "base.png");

        #region Resize image

        // New size
        Size size = new Size(SizeInt, SizeInt);

        // New height/width temp vars
        int newWidth;
        int newHeight;

        // Aspect ratio preservation
        int originalWidth = OriginalImage.Width;
        int originalHeight = OriginalImage.Height;

        float percentWidth = (float)size.Width / (float)originalWidth;
        float percentHeight = (float)size.Height / (float)originalHeight;

        float percent = percentHeight < percentWidth ? percentHeight : percentWidth;

        newWidth = (int)(originalWidth * percent);
        newHeight = (int)(originalHeight * percent);

        // Generate the new image
        Image ResizedImage = new Bitmap(newWidth, newHeight);
        using (Graphics graphicsHandle = Graphics.FromImage(ResizedImage))
        {
            graphicsHandle.SmoothingMode = SmoothingMode.HighQuality;
            graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphicsHandle.PixelOffsetMode = PixelOffsetMode.HighQuality;
            graphicsHandle.DrawImage(OriginalImage, 0, 0, newWidth, newHeight);
        }

        #endregion

        int maxside = SizeInt;

        #region Image rotation

        // Generate images
        for (int i = 0; i < 361; i += 5)
        {
            // New empty bitmap to hold rotated images
            Bitmap ReturnBitmap = new Bitmap(maxside, maxside);

            // Graphics object from the empty bitmap
            // Normal
            Graphics Gfx = Graphics.FromImage(ReturnBitmap);
            Gfx.SmoothingMode = SmoothingMode.HighQuality;
            Gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
            Gfx.PixelOffsetMode = PixelOffsetMode.HighQuality;

            // Move rotation point to center of image
            Gfx.TranslateTransform(
                (float)ResizedImage.Width / 2,
                (float)ResizedImage.Height / 2
            );

            // Rotate
            Gfx.RotateTransform(i);

            // Move image back
            Gfx.TranslateTransform(
                -(float)ResizedImage.Width / 2,
                -(float)ResizedImage.Height / 2
            );

            // draw original in image onto graphics object
            Gfx.DrawImage(ResizedImage, new Point(0, 0));

            // Save the image
            ReturnBitmap.Save(ContentPath + i.ToString() + ".png");

            // Clean up
            ReturnBitmap.Dispose();
            Gfx.Dispose();
        }

        #endregion

        // Clean up
        ResizedImage.Dispose();
        OriginalImage.Dispose();
    }

有什么建议吗?

0 个答案:

没有答案