使用Mono Cairo Library使用图像填充矩形

时间:2014-11-18 15:41:53

标签: c# cairo

我正在尝试在矩形内填充图像。我能够将图像位置正确设置到矩形的最左角。但是,缩放不能按预期工作。对此有任何帮助表示赞赏。以下是我的代码。这是一张1290 * 1990尺寸的图片。

        Cairo.Rectangle imageRectangle = new Cairo.Rectangle(50, 100, width, height);

        ctx.NewPath();
        Cairo.ImageSurface imgSurface = new Cairo.ImageSurface("C:/Temp/Image.png");
        ctx.SetSource(imgSurface, topLeftPoint); //topLeft is (50,100)

        float xScale = (float)imageRectangle.Width / (float)imgSurface.Width;
        float yScale = (float)imageRectangle.Height / (float)imgSurface.Height;

        //Reposition the image to the rectangle origin
        ctx.Translate(imageRectangle.X, imageRectangle.Y);
        ctx.Scale(xScale, yScale);

        ctx.Paint();

谢谢!

1 个答案:

答案 0 :(得分:2)

我找到了解决方案。我把源设置在错误的地方。下面是正确的代码

    Cairo.Rectangle imageRectangle = new Cairo.Rectangle(50, 100, width, height);

    ctx.NewPath();
    Cairo.ImageSurface imgSurface = new Cairo.ImageSurface("C:/Temp/Image.png");


    float xScale = (float)imageRectangle.Width / (float)imgSurface.Width;
    float yScale = (float)imageRectangle.Height / (float)imgSurface.Height;

    //Reposition the image to the rectangle origin
    ctx.Translate(imageRectangle.X, imageRectangle.Y);
    ctx.Scale(xScale, yScale);

    ctx.SetSource(imgSurface); 
    ctx.Paint();

谢谢!

相关问题