You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

叠加新图时Placeholder图像被缩放无法完整显示的代码优化求助

问题解决:占位图叠加新图时避免缩放

问题原因

你的占位图被缩放,核心原因有两个:

  • 新创建的Bitmap默认使用96DPI,如果原占位图的DPI不是96,Graphics.DrawImage会自动按DPI比例缩放图片。
  • 使用DrawImage(Image, int, int)重载时,会根据Graphics对象与图片的DPI差异自动调整绘制尺寸,导致占位图被缩放。

修复方案

修改代码时需要处理DPI一致性,同时指定精确的绘制尺寸,确保占位图和新图都按原始像素尺寸绘制:

修改后的完整代码

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

class ImageOverlay
{
    static void Main()
    {
        // Paths to placeholder images
        string[] placeholderImagePaths =
        {
            "D:\\First-img.png",
            "D:\\Second-img.png",
            "D:\\Third-img.png"
            // Add more paths here as needed
        };

        // Get path for the new image from user input
        Console.WriteLine("Enter the path to the new image:");
        string digitalArtImagePath = Console.ReadLine();

        // Load the new image
        using (var digitalArtImage = Image.FromFile(digitalArtImagePath))
        {
            foreach (string placeholderImagePath in placeholderImagePaths)
            {
                // Load the placeholder image
                using (var placeholderImage = Image.FromFile(placeholderImagePath))
                {
                    // Create composite image with same dimensions and DPI as placeholder
                    using (var compositeImage = new Bitmap(placeholderImage.Width, placeholderImage.Height))
                    {
                        compositeImage.SetResolution(placeholderImage.HorizontalResolution, placeholderImage.VerticalResolution);

                        using (var graphics = Graphics.FromImage(compositeImage))
                        {
                            // Set graphics to use pixel units and disable automatic scaling
                            graphics.PageUnit = GraphicsUnit.Pixel;
                            graphics.InterpolationMode = InterpolationMode.NearestNeighbor;

                            // Draw placeholder image with exact original dimensions
                            graphics.DrawImage(placeholderImage, new Rectangle(0, 0, placeholderImage.Width, placeholderImage.Height));

                            // Position new image without scaling (use original dimensions)
                            int digitalArtX = 100;
                            int digitalArtY = 100;
                            // Use original width/height of the new image to avoid scaling
                            int digitalArtWidth = digitalArtImage.Width;
                            int digitalArtHeight = digitalArtImage.Height;

                            // Optional: Keep high-quality settings if you need to resize later, but remove if no scaling
                            graphics.SmoothingMode = SmoothingMode.AntiAlias;
                            graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

                            // Draw new image at specified position with original size
                            graphics.DrawImage(digitalArtImage, new Rectangle(digitalArtX, digitalArtY, digitalArtWidth, digitalArtHeight));
                        }

                        // Save the composite image as a JPEG
                        string compositeImagePath = Path.Combine(Path.GetDirectoryName(digitalArtImagePath), "composite_" + Path.GetFileNameWithoutExtension(placeholderImagePath) + ".jpg");
                        compositeImage.Save(compositeImagePath, ImageFormat.Jpeg);

                        Console.WriteLine("Composite image saved: " + compositeImagePath);
                    }
                }
            }
        }
    }
}

关键修改点说明

  • 继承占位图DPI:通过compositeImage.SetResolution(placeholderImage.HorizontalResolution, placeholderImage.VerticalResolution)让合成图和占位图使用相同的DPI,避免因DPI差异导致的自动缩放。
  • 设置像素单位:添加graphics.PageUnit = GraphicsUnit.Pixel,确保所有绘制操作都按像素为单位,消除系统自动的尺寸转换。
  • 精确绘制占位图:使用DrawImage的矩形重载graphics.DrawImage(placeholderImage, new Rectangle(0, 0, placeholderImage.Width, placeholderImage.Height)),强制按原始像素尺寸绘制占位图,避免默认重载的自动缩放行为。
  • 新图无缩放绘制:去掉手动指定的digitalArtWidth和digitalArtHeight,改用digitalArtImage.Width和digitalArtImage.Height,确保新图按原始尺寸叠加。

内容的提问来源于stack exchange,提问作者G3n1t0

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 20:14:52