C# graphics displays the image larger than normal?
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:
Made with code above:
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)));
精彩评论