【问题标题】:C# Bitmap Draw Image is ScalingC#位图绘制图像正在缩放
【发布时间】:2021-09-10 22:17:07
【问题描述】:

我的设置采用 .png 图像(具有透明度)并将这些图像作为图层堆叠在一起以形成一个图像。我做的第一批工作完美。由于某种原因,第二批正在缩放所有图像。

  1. 所有图片均为 2000 像素 x 2000 像素(我已单独检查过每张图片,还可以看到 Windows 资源管理器可以识别它们的所有尺寸。
  2. 所有图片都是具有透明背景的 *.png 文件。

Merge() 函数中:我检查了widthheight(以及图像对象的相同)的输出是什么,并且每次它们都出来是2000(这是它应该的是)。

以下是实际输出与实际输出的屏幕截图。同样,我使用不同的图像(全部为 2000 像素 x 2000 像素)进行了此操作,并且效果很好。我不确定这里发生了什么。

    public static Bitmap TestIt()
    {
        List<string> list = new List<string>
        {
            Path.Combine(dir, "body1.png"),
            Path.Combine(dir, "head5.png"),
            Path.Combine(dir, "legs2.png"),
            Path.Combine(dir, "background2.png")
        };

        //Get List of Bitmaps from URLs
        List<Bitmap> bitmaps = ConvertUrlsToBitmaps(list);

        if (bitmaps.Any())
        {
            return Merge(bitmaps);
        }
        else
        {
            return null;
        }
       
    }


    private static Bitmap Merge(List<Bitmap> images)
    {
        Bitmap bitmap = null;
        var width = 0;
        var height = 0;
        
        try
        {
            // Get max width and height of the image
            foreach (var image in images)
            {
                width = image.Width > width ? image.Width : width;
                height = image.Height > height ? image.Height : height;
            }

            // merge images
            bitmap = new Bitmap(width, height);
            using (var g = Graphics.FromImage(bitmap))
            {
                foreach (var image in images)
                {
                    g.DrawImage(image, 0, 0);
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
        return bitmap;

    }


    private static List<Bitmap> ConvertUrlsToBitmaps(List<string> imageUrls)
    {
        List<Bitmap> bitmapList = new List<Bitmap>();
        try
        {
            foreach (string imgUrl in imageUrls)
            {
                Bitmap bmp = (Bitmap)Image.FromFile(imgUrl);
                bitmapList.Add(bmp);
            }            
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
        return bitmapList;
    }

【问题讨论】:

  • 使用可以告诉您图像 DPI 的工具检查图像。我敢打赌,这些与第一组不同。在文件资源管理器中右键单击图像,然后单击“属性”。在图像属性窗口中,切换到“详细信息”选项卡。在详细信息选项卡中,向下滚动到“图像”子部分,然后查找“水平分辨率”和“垂直分辨率”统计数据,它们的值应为“dpi”
  • @EmondErno 我真的以为你一针见血——但所有图像都是 96dpi——和上一组一样

标签: c# image bitmap


【解决方案1】:

好吧...我解决了。

我添加了一个 Rectangle 对象,它可以识别位置和大小。 然后我强制drawImage函数根据位置和大小进行绘制

这里是更新

private static Bitmap Merge(List<Bitmap> images)
    {
        Bitmap bitmap = null;
        var width = 0;
        var height = 0;
        
        try
        {
            // Get max width and height of the image
            foreach (var image in images)
            {
                width = image.Width > width ? image.Width : width;
                height = image.Height > height ? image.Height : height;
            }

            // merge images
            bitmap = new Bitmap(width, height);

            //*** Added a Rectangle object that will identify starting position and size
            Rectangle rect = new Rectangle(new Point(0, 0), new Size(width, height));

            using (var g = Graphics.FromImage(bitmap))
            {
                foreach (var image in images)
                {
                    //*** Force the system to draw the image at location AND size
                    g.DrawImage(image, rect);
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
        return bitmap;

    }

【讨论】:

  • 获取最大尺寸的 foreach 可以更简单: var width = images.Max(i => i.Width); var height = images.Max(i => i.Height);这不是更快,但我发现它更具可读性和优雅性。
  • 看起来您正在将图像缩放到最大的图像。根据插值设置,这可能会引入一些伪影。
猜你喜欢
  • 1970-01-01
  • 2016-10-07
  • 1970-01-01
  • 2012-06-06
  • 2017-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-29
相关资源
最近更新 更多