Skip to content

Instantly share code, notes, and snippets.

@hjerpbakk
Created January 5, 2022 21:09
Show Gist options
  • Save hjerpbakk/e485239e25a0e824afa0294e720998be to your computer and use it in GitHub Desktop.
Save hjerpbakk/e485239e25a0e824afa0294e720998be to your computer and use it in GitHub Desktop.
Scale an image to fit using C#, Xamarin and SpriteKit
var image = SKSpriteNode.FromTexture(SKTexture.FromImageNamed("image-name"));
image.Position = new CGPoint(Frame.GetMidX(), Frame.GetMidY());
var widthScaleFactor = device.DeviceSize.Width / image.Size.Width;
var heightScaleFactor = device.DeviceSize.Height / image.Size.Height;
var scaleFactor = widthScaleFactor < heightScaleFactor ? widthScaleFactor : heightScaleFactor;
image.SetScale(scaleFactor);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment