【问题标题】:Xamarin Mac - Resize ImageXamarin Mac - 调整图像大小
【发布时间】:2017-06-23 07:11:04
【问题描述】:

我正在尝试在我的 Mac 应用程序中调整图像的大小,就像我之前在 iOS 应用程序中所做的那样。问题是 NSImage 不包含 UIImage 所做的所有方法。这是我用于 iOS 的代码

public static UIImage MaxResizeImage(this UIImage sourceImage, float maxWidth, float maxHeight)
    {
        var sourceSize = sourceImage.Size;
        var maxResizeFactor = Math.Max(maxWidth / sourceSize.Width, maxHeight / sourceSize.Height);
        if (maxResizeFactor > 1) return sourceImage;
        float width = (float)(maxResizeFactor * sourceSize.Width);
        float height = (float)(maxResizeFactor * sourceSize.Height);
        UIGraphics.BeginImageContext(new SizeF(width, height));
        sourceImage.Draw(new RectangleF(0, 0, width, height));
        var resultImage = UIGraphics.GetImageFromCurrentImageContext();
        UIGraphics.EndImageContext();
        return resultImage;
    }

如何为 Mac-App 重写它?感谢您的帮助

【问题讨论】:

  • 如果没有解决请接受anwser或添加评论

标签: ios xamarin xamarin.ios nsimage xamarin.mac


【解决方案1】:

我有一个适用于我的 Mac 应用程序的工作功能,重写它以适应您的功能。尚未测试此版本,但它应该可以工作。

public static NSImage MaxResizeImage(this NSImage sourceImage, float maxWidth, float maxHeight)
{
    var sourceSize = sourceImage.Size;
    var maxResizeFactor = Math.Max(maxWidth / sourceSize.Width, maxHeight / sourceSize.Height);
    if (maxResizeFactor > 1) return sourceImage;
    float width = (float)(maxResizeFactor * sourceSize.Width);
    float height = (float)(maxResizeFactor * sourceSize.Height);
    var targetRect = new CoreGraphics.CGRect(0,0,width, height);
    var newImage = new NSImage (new CoreGraphics.CGSize (width, height));
    newImage.LockFocus ();
    sourceImage.DrawInRect(targetRect, CoreGraphics.CGRect.Empty, NSCompositingOperation.SourceOver, 1.0f);
    newImage.UnlockFocus ();

    return newImage;
}

【讨论】:

    猜你喜欢
    • 2017-05-07
    • 1970-01-01
    • 1970-01-01
    • 2019-01-05
    • 2010-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多