开发者

C# graphics displays the image larger than normal?

开发者 https://www.devze.com 2023-02-25 02:15 出处:网络
I am trying to merge two images using C Sharp\'s System.Drawing.Graphics. Here is my code: Point p = new Point(Convert.ToInt32(OffsetX), Convert.ToInt32(OffsetY));

I am trying to merge two images using C Sharp's System.Drawing.Graphics.

Here is my code:

        Point p = new Point(Convert.ToInt32(OffsetX), Convert.ToInt32(OffsetY));
        Image i = Image.FromFile("1.jpg");
        Image toDraw = Image.FromFile("2.jpg");
        using (Graphics g = Graphics.FromImage(i))
  开发者_如何学运维      {
            g.DrawImage(toDraw, p);
            g.Save();
            Directory.CreateDirectory(Path.Combine(Directory.GetCurrentDirectory(), "saved"));
            i.Save(Path.Combine("saved", "saved1.jpg"));
        }

The code works fine, but the second image is enlarged in the output from the program.

Made with Paint:

C# graphics displays the image larger than normal?

Made with code above:

C# graphics displays the image larger than normal?


Use the Graphics.DrawImage(Image, Rectangle) overload to control the size of the image. The overload you are using takes note of the Image.HorizontalResolution and VerticalResolution properties to ensure that the drawn image is (roughly) as large in inches as it was when it was created. Fix:

       g.DrawImage(toDraw, new Rectangle(p, new Size(i.Width, i.Height)));
0

精彩评论

暂无评论...
验证码 换一张
取 消