Xamarin Mac - 调整图像大小
Xamarin Mac - Resize Image
我正尝试像以前在 iOS-应用程序中那样调整我的 Mac-应用程序中的图像大小。问题是 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 重写它?感谢您的帮助
我的 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;
}
我正尝试像以前在 iOS-应用程序中那样调整我的 Mac-应用程序中的图像大小。问题是 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 重写它?感谢您的帮助
我的 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;
}