mono / libgdiplus

C-based implementation of the GDI+ API
http://www.mono-project.com/
MIT License
329 stars 171 forks source link

Can't draw images that are scaled in one direction only #729

Open jhergens opened 2 years ago

jhergens commented 2 years ago

When rendering an image where either the width or height of the source and destination rectangles are the same, no scaling will occur even if the rectangles differ in the other dimension.

The following code:

var bitmap = new Bitmap(100, 200);
using (var graphics = Graphics.FromImage(bitmap))
{
    graphics.Clear(Color.White);

    var redBitmap = new Bitmap(50, 50);
    using (var redGraphics = Graphics.FromImage(redBitmap))
    {
        redGraphics.Clear(Color.Red);
    }

    var destRect = new RectangleF(25, 25, 50, 150);
    var srcRect = new RectangleF(0, 0, 50, 50);

    graphics.DrawImage(redBitmap, destRect, srcRect, GraphicsUnit.Pixel);

    using (var pen = new Pen(Color.Black))
    {
        graphics.DrawRectangle(pen, Rectangle.Round(destRect));
    }
}

Renders like this: image-1

It is expected to render like this: image-2

We are building libgdiplus from main.

It seems the problem is the following snippet in GdipDrawImageRectRect

if (!gdip_near_zero(srcwidth - dstwidth) && !gdip_near_zero(srcheight - dstheight))
  cairo_matrix_scale (&mat, srcwidth / dstwidth, srcheight / dstheight);

Changing && to || fixes this.