I stitch several images vertically and horizontally to create one larger image (in which the total width and height are the sum of the width and height of the individual images) using Bitmapand System.Drawing.Graphicsin C #. The size of individual images is 256 px by 256 px.
When I use DrawImagefrom System.Drawing.Graphics, why do I get a scaled and / or enlarged version of the original image?
Here is the source image:

When I get the image programmatically and write to a file in code, I get the following:
var result = httpClient.GetStreamAsync();
var bitmap = new Bitmap(await result);
...
using (var memory = new MemoryStream())
{
using (var fs = new FileStream(, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
bitmap.Save(memory, ImageFormat.Png);
var bytes = memory.ToArray();
await fs.WriteAsync(bytes, 0, bytes.Length);
}
}

I do not see the difference. So far, so good.
However, when I try to stitch images horizontally, I get the following:
Note. For reference, the image above is on the far right corner of the stitched image below.

, , , .
, :
. byWidth - true, .
private Bitmap MergeImages(IEnumerable<Bitmap> images, bool byWidth)
{
var enumerable = images as IList<Bitmap> ?? images.ToList();
var width = 0;
var height = 0;
foreach (var image in enumerable)
{
if (byWidth)
{
width += image.Width;
height = image.Height;
}
else
{
width = image.Width;
height += image.Height;
}
}
var bitmap = new Bitmap(width, height);
using (var g = Graphics.FromImage(bitmap))
{
var localWidth = 0;
var localHeight = 0;
foreach (var image in enumerable)
{
if (byWidth)
{
g.DrawImage(image, localWidth, 0);
localWidth += image.Width;
}
else
{
g.DrawImage(image, 0, localHeight);
localHeight += image.Height;
}
}
}
return bitmap;
}
user5398447