C #: keep size despite different resolution using Graphics.DrawImage

I am trying to draw two images side by side using the C # Drawing namespace. Here is a very simple example, assuming two images of the same height:

Image[] oldImages = GetOldImages();

var newImage = new Bitmap(oldImages[0].Width + oldImages[1].Width, 800);

using (var newImageGraphics = Graphics.FromImage(newImage))
{
    newImageGraphics.DrawImage(oldImages[0], 0, 0);
    newImageGraphics.DrawImage(oldImages[1], oldImage[0].Width, 0);
    newImageGraphics.Save();
}

This works fine if the resolution of the two old images is the same.

However, if the resolutions are different from each other, the image changes, which causes problems. For example, if the first image has a different resolution, then the second image will not be set correctly.

Does anyone know how I can solve this problem easily? Ideally, I want the original height and width of the image to remain unchanged when they are drawn on a new image.

+3
2

:

Bitmap picture_1 = new Bitmap(picture_1_path);
Graphics graphics = Graphics.FromImage(picture_1);
Bitmap picture_2 = new Bitmap(picture_2_path);
picture_2.SetResolution(graphics.DpiX, graphics.DpiY);

//Then do with pictures anything
+4

.

, , , . , . , .

+1

Source: https://habr.com/ru/post/1709505/


All Articles