【发布时间】:2021-09-10 22:17:07
【问题描述】:
我的设置采用 .png 图像(具有透明度)并将这些图像作为图层堆叠在一起以形成一个图像。我做的第一批工作完美。由于某种原因,第二批正在缩放所有图像。
- 所有图片均为 2000 像素 x 2000 像素(我已单独检查过每张图片,还可以看到 Windows 资源管理器可以识别它们的所有尺寸。
- 所有图片都是具有透明背景的 *.png 文件。
在Merge() 函数中:我检查了width 和height(以及图像对象的相同)的输出是什么,并且每次它们都出来是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——和上一组一样